Let's start with a very simple project, the "Hello world" program for example
/* hello.c */ #include <stdio.h> int main(int argc, char* argv[]) { printf("Hello world\n"); return 0; }Prepare a SConstruct to compile the program as usual.
# SConstruct env = Environment() hello = Program(["hello.c"])
Now we'll convert the project to a multi-lingual one. If you don't already have GNU gettext utilities installed, install them from your preffered package repository, or download from http://ftp.gnu.org/gnu/gettext/. For the purpose of this example, you should have following three locales installed on your system: en_US, de_DE and pl_PL. On debian, for example, you may enable certain locales through dpkg-reconfigure locales.
First prepare the hello.c program for internationalization. Change the previous code so it reads as follows:
/* hello.c */ #include <stdio.h> #include <libintl.h> #include <locale.h> int main(int argc, char* argv[]) { bindtextdomain("hello", "locale"); setlocale(LC_ALL, ""); textdomain("hello"); printf(gettext("Hello world\n")); return 0; }Detailed recipes for such conversion can be found at http://www.gnu.org/software/gettext/manual/gettext.html#Sources. The
gettext("...")
has two purposes.
First, it marks messages for the xgettext(1) program, which
we will use to extract from the sources the messages for localization.
Second, it calls the gettext library internals to
translate the message at runtime.
Now we shall instruct SCons how to generate and maintain translation files.
For that, use the Translate
builder and MOFiles
builder.
The first one takes source files, extracts internationalized
messages from them, creates so-called POT file
(translation template), and then creates PO translation
files, one for each requested language. Later, during the development
lifecycle, the builder keeps all these files up-to date. The
MOFiles
builder compiles the PO files to binary
form. Then install the MO files under directory
called locale.
The completed SConstruct is as follows:
# SConstruct env = Environment( tools = ['default', 'gettext'] ) hello = env.Program(["hello.c"]) env['XGETTEXTFLAGS'] = [ '--package-name=%s' % 'hello', '--package-version=%s' % '1.0', ] po = env.Translate(["pl","en", "de"], ["hello.c"], POAUTOINIT = 1) mo = env.MOFiles(po) InstallAs(["locale/en/LC_MESSAGES/hello.mo"], ["en.mo"]) InstallAs(["locale/pl/LC_MESSAGES/hello.mo"], ["pl.mo"]) InstallAs(["locale/de/LC_MESSAGES/hello.mo"], ["de.mo"])
Generate the translation files with scons po-update. You should see the output from SCons simillar to this:
user@host:$ scons po-update scons: Reading SConscript files ... scons: done reading SConscript files. scons: Building targets ... Entering '/home/ptomulik/projects/tmp' xgettext --package-name=hello --package-version=1.0 -o - hello.c Leaving '/home/ptomulik/projects/tmp' Writting 'messages.pot' (new file) msginit --no-translator -l pl -i messages.pot -o pl.po Created pl.po. msginit --no-translator -l en -i messages.pot -o en.po Created en.po. msginit --no-translator -l de -i messages.pot -o de.po Created de.po. scons: done building targets.
If everything is right, you should see following new files.
user@host:$ ls *.po* de.po en.po messages.pot pl.po
Open en.po in poedit and provide the English translation to message "Hello world\n". Do the same for de.po (deutsch) and pl.po (polish). Let the translations be, for example:
en: "Welcome to beautiful world!\n"
de: "Hallo Welt!\n"
pl: "Witaj swiecie!\n"
Now compile the project by executing scons. The output should be similar to this:
user@host:$ scons scons: Reading SConscript files ... scons: done reading SConscript files. scons: Building targets ... msgfmt -c -o de.mo de.po msgfmt -c -o en.mo en.po gcc -o hello.o -c hello.c gcc -o hello hello.o Install file: "de.mo" as "locale/de/LC_MESSAGES/hello.mo" Install file: "en.mo" as "locale/en/LC_MESSAGES/hello.mo" msgfmt -c -o pl.mo pl.po Install file: "pl.mo" as "locale/pl/LC_MESSAGES/hello.mo" scons: done building targets.SCons automatically compiled the PO files to binary format MO, and the InstallAs lines installed these files under locale folder.
Your program should be now ready. You may try it as follows (linux):
user@host:$ LANG=en_US.UTF-8 ./hello Welcome to beautiful world
user@host:$ LANG=de_DE.UTF-8 ./hello Hallo Welt
user@host:$ LANG=pl_PL.UTF-8 ./hello Witaj swiecie
To demonstrate the further life of translation files, let's change Polish translation (poedit pl.po) to "Witaj drogi swiecie\n". Run scons to see how scons reacts to this
user@host:$scons scons: Reading SConscript files ... scons: done reading SConscript files. scons: Building targets ... msgfmt -c -o pl.mo pl.po Install file: "pl.mo" as "locale/pl/LC_MESSAGES/hello.mo" scons: done building targets.
Now, open hello.c and add another one printf line with new message.
/* hello.c */ #include <stdio.h> #include <libintl.h> #include <locale.h> int main(int argc, char* argv[]) { bindtextdomain("hello", "locale"); setlocale(LC_ALL, ""); textdomain("hello"); printf(gettext("Hello world\n")); printf(gettext("and good bye\n")); return 0; }
Compile project with scons. This time, the msgmerge(1) program is used by SCons to update PO file. The output from compilation is like:
user@host:$scons scons: Reading SConscript files ... scons: done reading SConscript files. scons: Building targets ... Entering '/home/ptomulik/projects/tmp' xgettext --package-name=hello --package-version=1.0 -o - hello.c Leaving '/home/ptomulik/projects/tmp' Writting 'messages.pot' (messages in file were outdated) msgmerge --update de.po messages.pot ... done. msgfmt -c -o de.mo de.po msgmerge --update en.po messages.pot ... done. msgfmt -c -o en.mo en.po gcc -o hello.o -c hello.c gcc -o hello hello.o Install file: "de.mo" as "locale/de/LC_MESSAGES/hello.mo" Install file: "en.mo" as "locale/en/LC_MESSAGES/hello.mo" msgmerge --update pl.po messages.pot ... done. msgfmt -c -o pl.mo pl.po Install file: "pl.mo" as "locale/pl/LC_MESSAGES/hello.mo" scons: done building targets.
The next example demonstrates what happens if we change the source code in such way that the internationalized messages do not change. The answer is that none of translation files (POT, PO) are touched (i.e. no content changes, no creation/modification time changed and so on). Let's append another line to the program (after the last printf), so its code becomes:
/* hello.c */ #include <stdio.h> #include <libintl.h> #include <locale.h> int main(int argc, char* argv[]) { bindtextdomain("hello", "locale"); setlocale(LC_ALL, ""); textdomain("hello"); printf(gettext("Hello world\n")); printf(gettext("and good bye\n")); printf("----------------\n"); return a; }Compile the project. You'll see on your screen
user@host:$scons scons: Reading SConscript files ... scons: done reading SConscript files. scons: Building targets ... Entering '/home/ptomulik/projects/tmp' xgettext --package-name=hello --package-version=1.0 -o - hello.c Leaving '/home/ptomulik/projects/tmp' Not writting 'messages.pot' (messages in file found to be up-to-date) gcc -o hello.o -c hello.c gcc -o hello hello.o scons: done building targets.As you see, the internationalized messages ditn't change, so the POT and the rest of translation files have not even been touched.