For example, in this simplest hello world program:
#include <iostream>
int main()
{
std::cout<<"Hello World!"<<std::endl;
return 0;
}
I'd like to see French if user's environment LANG
is set to fr_FR
, it might looks like:
$ ./a.out
Hello World!
$ LANG=fr_FR.utf8
$ ./a.out
Bonjour tout le monde!
Is there a guideline of how to archieve this in Linux?
You probably won't want to do this in simple program, but if your program is large enough, you can use GNU gettext, which can be found at http://www.gnu.org/software/gettext/ . Then, you supply your program with the text files, and then use printf (_("Some string\n")) to output localized string.
Just found a tutorial that I can easily follow: http://fedoraproject.org/wiki/How_to_do_I18N_through_gettext
Here is my new code
Then creates
pot
fileGenerates
mo
fileHere is the output
Two questions here.
You can easily make your program internationalized/localized using the Gettext library.
You can check the user's environment variables using either the standard function `getenv()’:
const char *langcode = getenv("LANG");
or some implementations (I believe Linux and Mac OS X included) support a 3-argument main function:
For c++, if you can use Qt library, it has a good support for localization. For details take a look into their page for the internalization.
Next pseudo-example shows how to load the translation files, and set the locale :
The key is to use "resources" (one per-language, configured to be read at runtime) vs. hard-coding strings. GUI frameworks like Qt and GTK+ make this (relatively) easy.
Here's a link to the Pango" library used by GTK+ (but not, emphatically, exclusive to GTK+):
Here's a tutorial on using Pango:
And here's a tutorial on "gettext()" (which, I believe, Pango uses):