PHP: setlocale in Windows 7

2020-02-05 11:34发布

问题:

I have problem with setlocale function in PHP 5.3 in Windows 7.

I need to set Russian locale with UTF-8 encoding. I saw some solutions where this is Russian_Russia.65001, but the function with this type of locale returns FALSE.

All solutions was designed for using in Windows XP where list of locales was in Control Panel. In Windows 7 this feature was removed.

回答1:

It's not possible to use UTF-8 locales on windows systems.:

If you provide a code page like UTF-7 or UTF-8, setlocale will fail, returning NULL.

Only windows code page Russian_Russia.1251 will work (see Moodle's table of locales).

You may still try set both and see which one comes trough:

if ($newLocale = setLocale(LC_CTYPE, 'ru_RU.UTF-8', 'Russian_Russia.1251'))
{
   echo 'Locale is now set to: ' . $newLocale;
}


回答2:

This worked for me on Windows 7

putenv('LANG=fr_CA.UTF-8');
setlocale(LC_ALL,'fr_CA.UTF-8');
bindtextdomain('translation','./language');
bind_textdomain_codeset('translation','UTF-8');
textdomain('translation');

Path to .po/.mo files

./language -> fr_CA.UTF-8 -> LC_MESSAGES -> translation.po
./language -> fr_CA.UTF-8 -> LC_MESSAGES -> translation.mo