简体   繁体   中英

Gettext can't get setlocale() to work

I use gettext localization like this

$lang = $_GET['lang'];
echo $lang;
putenv("LANG=$lang"); 
setlocale(LC_ALL, $lang); 
bindtextdomain("messages", realpath("../locale")); 
bind_textdomain_codeset("messages", "utf-8"); 
textdomain("messages"); 

... 

echo _("Welcome!") etc.

I can see localization works, because the site can be seen translated in Slovak. However, no mather what is in the $lang variable, the site only is translated in Slovak or not translated at all.

Slovak means whatever .po and .mo files i have in 'sk' folder. I have tried putting different languages in the 'sk' folder, it works and I get different translations. But I cannot make it to take different folder, like 'en' or 'cs'. All other folders are ignored. What am I doing wrong?

PS setlocale() returns false, I can't figure out why but that's probably causing the trouble.

setlocale() will return false if your system does not support that locale. You can see which locales your system supports by examining the output of locale -a . The value that you pass in via the lang get var must match one of those locale names.

As Dan says, sk is probably your standard locale which is why that is used even if setlocale returns false..

Well, there is another nasty hack:

On my system only en_DK.utf-8 is install and I'm not able to install any other langs.

So, i'm selecting the language by using the domain instead:

$lang = "en";
putenv("LANG=en_DK.utf8"); 
setlocale(LC_ALL, "en_DK.utf8");
bindtextdomain($lang, realpath("./locale")); 
bind_textdomain_codeset($lang, "utf-8"); 
textdomain($lang);

The file location of the .mo files are like this:

./locale/en_DK.utf-8/LC_MESSAGES/en.mo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM