The first Alchemy application 'hello' now speaks 49 languages!
If you install latest hello package (version 3) and run it from the
terminal it will most probably print a message in your language.
You can also run it in different language changing the environment
variable LANGUAGE:
> env LANGUAGE=de hello
Hallo, welt!
To be honest, multilingual support was in Alchemy OS since release
2.1 but it was undocumented. Now with all needed tools written and
living example you can add multilingual support to your application!
Step 1: preparing source code
All you need is to do is to add header for internationalization (i18n for short)
use "i18n"
This is core header, no external libraries needed. Then just change every
"string" into _("string"). Function _( ) is the magic. Finally, you need to
tell your program which message catalog to use. To do that add this line
as the first line in main() function:
settextdomain("MyProgramName")
That's all. You can compile and run your application now. Of course, everything
will be in the original language because we have not created actual translations
yet.
Step 2: preparing translations
For that you will need to install i18n-tools package. This package currently
contains two utilities.
First of them, msgextract, used to extract all translatable strings from
your sources.
msgextract source1.e source2.e ... -o messages.txt
This will generate file messages.txt with contents like:
"String 1" = ""
"String 2" = ""
You need to put translations to your language after the '=' sign.
You can use this file as template and prepare translations to several
languages: de.txt, fr.txt, ru.txt ...
Step 3: compiling and installing translations
Finally, you need to turn prepared message catalogs into machine-readable
format and install them into appropriate locations.
Compiling is done by msgfmt utility, which is also in i18n-tools package
msgfmt ru.txt MyProgramName.lc
Here MyProgramName is exactly the string you used for settextdomain()
The default location for translations is /res/locale/<locale-code>/. For example,
Russian message catalogs will be put in /res/locale/ru/
install MyProgramName.lc /res/locale/ru/