繁体   English   中英

PHP bindtextdomain 失败

[英]PHP bindtextdomain fails

我正在尝试在运行 PHP 7.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!

locale -a 返回(除其他外)

ja_JP
ja_JP.utf8
japanese

知道有什么问题吗?

正如评论中所讨论的, 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"

(正如您已经完成的那样)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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