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?
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):
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:
int main(int argc, char **argv, char **envp)
{
char **e;
char *langcode;
for (langcode = NULL, e = envp; e != NULL; e++)
{
if (strstr(*e, "LANG") != NULL)
{
langcode = strchr(*e, '=') + 1;
break;
}
}
printf("Language: %s\n", langcode);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With