Changes

Jump to: navigation, search

BTC640/Text

2,811 bytes added, 17:24, 5 January 2012
Part 2
== Part 2 ==
 
You'll need to use matrix (or your own Linux box) for this part. Here we're going to create a small program that prints hello world, and we'll translate that string into french. The point of the exercise is to see the entire process - from writing the code to running it.
 
Our program is here. Note that since we don't have permissions to write to the system directories we're going to put the translations in a local directory (locale).
 
<pre>
#include <libintl.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
setlocale( LC_ALL, "" );
//bindtextdomain( "hello", "/usr/share/locale" );
bindtextdomain( "hello", "./locale" );
textdomain( "hello" );
 
printf( gettext( "Hello, world!\n" ) );
 
return 0;
}
</pre>
 
Compile this using cc hello.c -o hello (note that no special compile flags are needed).
 
Now extract the translatable strings from your program into a template (hello.pot) file:
 
<pre>xgettext -d hello -s -o hello.pot hello.c</pre>
 
Edit hello.pot (it's a text file) and replace the values for all the default-looking strings with your own. Delete the "fuzzy" line. Change the charset to UTF-8.
 
This template file is what you would send off to a french-speaking translator, who will create fr.po out of it. But since we don't have a french-speaking translator - we're just going to do it ourselves.
 
<pre>cp hello.pot fr.po</pre>
 
Edit the file fr.po and set the msgstr for the hello world string to something different. It doesn't need to be french, just type something different in there. You can try a string from another language with more exotic characters if you like.
 
Create the local locale directory I mentioned above. Normally you wouldn't do this, it's only for this exercise:
 
<pre>mkdir -p locale/fr_CA/LC_MESSAGES</pre>
 
And compile your new french translation file:
 
<pre>msgfmt -c -v -o locale/fr_CA/LC_MESSAGES/hello.mo fr.po</pre>
 
If at this point you get any warnings - fix them, and rerun the msgfmt command.\
 
Run <pre>LC_ALL=fr_CA.UTF-8 ./hello</pre> and if you did everything right - you should see the translated string instead of the "Hello, world!" which you get from running ./hello without setting any variables.
 
Basically this means that your software will look different based on an environment variable (LC_ALL) so by changing one environment variable you can switch the language of all the software that's been translated into that language. Sadly our lab opensuse installations don't have language packs installed for anything, so you can't see it in action.
 
Submit the following for this part:
* You french po file with the translation filled in.
* Run the following and make a screenshot of the terminal window:
<pre>
msgfmt -c -v -o locale/fr_CA/LC_MESSAGES/hello.mo fr.po
./hello
LC_ALL=fr_CA.UTF-8 ./hello
</pre>

Navigation menu