gettext setup not working
I had gettext working, but now it suddenly won't translate. I get find out whats going on. I am trying to use /sys/locale/no开发者_C百科_NO/LC_MESSAGES/messages.po
to translate the page to norwegian.
/public/home.php
$locale = "en_US";
if (isSet($_COOKIE['lang'])) $locale = $_COOKIE['lang'];
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "../sys/locale");
textdomain("messages");
echo "<p>locale: " . $locale . " ";
echo gettext("Home");
exit();
This prints out
locale: no_No Home
when I should be locale: no_No Hjem
I see you are fighting with norwegian. I had the problem with Debian servers too which just support nb_NO and nn_NO but not no_NO. The solution is to use the exact names of locale names as folder and setting:
Make sure you have the locales installed:
$ locale -a
C
POSIX
en_US.utf8
nb_NO.utf8
nn_NO.utf8
So the php setting for norwegian bokmål:
setlocale(LC_ALL, 'nb_NO.utf8');
putenv('LANGUAGE=nb_NO.utf8');
The path to the PO files should have the same name e.g.:
./i18n/nb_NO.utf8/LC_MESSAGES/messages.po
I guess the same solution will work for Ubuntu.
With Japanese I have to do the following:
putenv("LANG=ja_JP.UTF-8");
setlocale(LC_MESSAGES, 'ja_JP.UTF-8');
bindtextdomain('messages', '../locale');
bind_textdomain_codeset('messages', 'UTF-8');
textdomain('messages');
With other languages I find I do not always have to add the encoding suffix, it is very inconsistent. Similarly I cannot just use "ja" when "en" works fine.
setlocale
returns false unless the locale is listed in /usr/share/i18n/SUPPORTED
but translations will often work despite the error.
You might need to reconfigure locales:
Install debconf (I.e. Run apt-get update then apt-get install debconf, as root)
Run dpkg-reconfigure locales as root
This code worked for me:
//Put this in cookie for example
$lang = GetPrefLanguage($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
$language = $lang . '.utf8';
setlocale(LC_ALL, $language);
// Set language
putenv('LANG='.$language);
// Specify location of translation tables
bindtextdomain('traductions', dirname(__FILE__).'/locale');
// Choose domain
textdomain("traductions");
// Translation is looking for in ./locale/xx_XX/LC_MESSAGES/traductions.mo now
/*GET Pref language */
function GetPrefLanguage($str_http_languages)
{
$lang = substr($str_http_languages, 0, 2);
switch($lang) {
case 'fr':
return 'fr_FR';
break;
case 'de':
return 'de_DE';
break;
case 'en':
return 'en_US';
break;
default:
return 'en_US';
}
}
精彩评论