Multilingual applications

1 view
Skip to first unread message

Sergey Basalaev

unread,
Sep 19, 2013, 8:29:05 AM9/19/13
to Alchemy OS
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/

Kyle Alexander Buan

unread,
Sep 19, 2013, 6:35:36 PM9/19/13
to alche...@googlegroups.com
Good! Can language be manually set by program without using env?
> --
> You received this message because you are subscribed to the Google Groups
> "Alchemy OS discussion group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to alchemy-os+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>

wiktorek140

unread,
Sep 19, 2013, 8:47:57 PM9/19/13
to alche...@googlegroups.com
Yes. Throw function "setlocale()". But if u want hange language in any part of program u must use:
"
setlocale("de")
settextdomain("hello")
"
It is only simple. U can set your program name and language. I think it's only way to make program multilanguages in program, without env

Kyle Alexander Buan

unread,
Sep 20, 2013, 4:37:43 AM9/20/13
to alche...@googlegroups.com
Okay, thanks! :)

Sergey Basalaev

unread,
Sep 20, 2013, 5:59:02 AM9/20/13
to Alchemy OS
Yes, Wiktor is absolutely right.

I can add that the algorithm Alchemy uses to determine language
is the following:

1) Language set explicitely by setlocale(). Use this to change
language from within program.
2) If there is environment variable LANGUAGE - it is used. Use this
to launch external program in a different language.
3) If there is file /cfg/locale, language is read from that file. Use it to
set default language for all applications.
4) If nothing of above is applicable, alchemy reads system property
"microedition.locale" to set program language.
Reply all
Reply to author
Forward
0 new messages