The &t-link-gettext; toolset supports internationalization and localization
of SCons-based projects. The tools provided within &t-link-gettext; by
automatize generation and updates of translation files. You can manage
translations and translation templates simillary as it was done with
autotools.
Prerequisites
Setup your operating system, so you can use several languages. In following
examples we use locales en_US, de_DE,
and pl_PL.
Ensure, that you have GNU gettext
utilities installed on your system.
To edit translation files, you may install poedit editor.
Simple project
Let's start with some 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 simple SConstruct script to compile the
program.
# SConstruct
env = Environment()
hello = Program(["hello.c"])
Now we'll convert the project to multi-lingual one. I assume, that you
already have GNU gettext
utilities installed. If not, install it from 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.
We 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;
}
This way we prepared source code. Detailed recipes for such preparation can
be found at
http://www.gnu.org/software/gettext/manual/gettext.html#Sources.
The gettext("...") in above source has two purposes.
First is is recognized by 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, we use &b-link-Translate; builder and &b-link-MOFiles; builder.
First one takes a couple of 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
&b-link-MOFiles; builder compiles the PO files to binary
form. After all, we install the MO files under directory
called locale.
The complete code of
SConstruct script for multi-lingual "Hello world" will
be following:
# 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 translation files with scons po-update.
You should see the output from SCons simillar to this:
ptomulik@:$ 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 shall see following new files.
ptomulik@:$ ls *.po*
de.po en.po messages.pot pl.po
Open en.po in poedit and provide
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 command. The
output should be similar to this:
ptomulik@:$ 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 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):
ptomulik@:$ LANG=en_US.UTF-8 ./hello
Welcome to beautiful world
ptomulik@:$ LANG=de_DE.UTF-8 ./hello
Hallo Welt
ptomulik@:$ LANG=pl_PL.UTF-8 ./hello
Witaj swiecie
To demonstrate 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
ptomulik@:$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:
ptomulik@:$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 last 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) is touched (i.e. no content changes, no
creation/modification time changed and so on). Let's append another one
instruction 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 project. You'll see on your screen
ptomulik@:$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.