我试图在运行PHP7.1的CentOS服务器上设置PHP国际化
这是我的目录结构:
/home/project/public_html/locale/japanese/LC_MESSAGES/messages.po
/home/project/public_html/locale/japanese/LC_MESSAGES/messages.mo
/home/project/public_html/index.php
messages.po包含这一行(除其他外):
"Language: japanese\n"
"Content-Type: text/plain; charset=UTF-8\n"
我有以下代码:
$check_putenv = putenv("LC_ALL=japanese");
if (!$check_putenv) {
echo "Warning: putenv LC_ALL failed!\n";
}
$check_putenv2 = putenv("LANGUAGE=japanese");
if (!$check_putenv2) {
echo "Warning: putenv LANGUAGE failed!\n";
}
$check_locale = setlocale(LC_MESSAGES, 'japanese');
if (!$check_locale) {
echo "Warning: Failed to set locale japanese!\n";
}
$check_bind = bindtextdomain("messages", "/home/project/public_html/locale");
if (!$check_bind) {
echo "Warning: Failed to bind text domain!\n";
}
$check_textdomain = textdomain("messages");
if ($check_textdomain !== "messages") {
echo "Warning: Failed to set text domain!\n";
}
输出是
Warning: Failed to bind text domain!
地区-a返回(除其他外)
ja_JP
ja_JP.utf8
japanese
知道有什么不对吗?
发布于 2019-01-18 04:31:33
正如注释中所讨论的那样,gettext扩展依赖于包含语言和区域代码的标准区域设置说明符,即“日语在日本”的ja_JP
或指定的编码ja_JP.utf-8
。即使有类似japanese
的别名,gettext的PHP实现也不接受这一点。请注意,必须在系统上安装和配置区域设置。
语言和区域说明符可以在IANA语言.子标签-注册表中找到
此代码已经适用于日语:
$dir = $_SERVER['DOCUMENT_ROOT'] . '/locale';
$domain = 'messages';
$locale = 'ja_JP.utf8';
$codeset = 'UTF-8';
setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $dir);
textdomain($domain);
bind_textdomain_codeset($domain, $codeset);
记住也要将目录重命名为locale/ja_JP.utf8
。确保您的.po文件以正确的编码方式存储,即本例中的UTF-8
,并包含行
"Content-Type: text/plain; charset=UTF-8\n"
(就像你已经做的那样)。
https://stackoverflow.com/questions/54251911
复制相似问题