【问题标题】:PHP bindtextdomain failsPHP bindtextdomain 失败
【发布时间】:2019-01-18 10:20:07
【问题描述】:

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

知道可能出了什么问题吗?

【问题讨论】:

  • 您是否尝试过$lang = 'ja_JP'ja_JP.UTF-8(并在所有设置和目录名称中使用此变量)?
  • 您是否通过存储库管理在您的系统上安装了日语语言包?之后重新配置(创建)语言环境?
  • 如编辑所示,即使系统上安装了“japanese”别名,我也不确定gettext的PHP实现是否接受这个。
  • 谢谢,ja_JP.UTF-8 有效!

标签: php internationalization gettext


【解决方案1】:

正如 cmets 中所讨论的,gettext 扩展依赖于包含语言和区域代码的标准区域设置说明符,即 ja_JP 表示“日本语”或使用指定的编码 ja_JP.utf-8 .即使有像japanese 这样的别名,gettext 的PHP 实现也不接受这个。请注意,必须在您的系统上安装和配置语言环境。

语言和地区说明符可以在IANA language-subtag-registry中找到

此代码已经适用于日语:

$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"

(就像你已经做过的那样)。

【讨论】:

  • 这段代码解决了我的问题。谢谢。 bind_textdomain_codeset($domain, $codeset)
猜你喜欢
  • 1970-01-01
  • 2019-06-22
  • 2017-04-04
  • 2016-11-13
  • 2012-12-29
  • 2018-04-25
  • 2012-09-24
  • 2012-12-11
  • 2010-11-10
相关资源
最近更新 更多