gettext 忽略 .mo 文件

gettext ignores .mo file

我正在尝试让 get text 正常工作,一切似乎都没有问题,但无论如何它都不会翻译我的文本。我在 Whosebug 上阅读了大约 5 个不同的帖子,在 google 上阅读了另外 5 个 - 没有任何帮助。这是我现在拥有的:

  1. 我从 php.net

    中获取了 php 文件
  2. 我从 here

    中获取了 po 文件示例
  3. 我从 here

    获取了 gettext 二进制文件

这些是我最终用来确保不会出现任何错误的文件:

<?php
// Set language to English
putenv('LC_ALL=en');
putenv("LANGUAGE=en");
putenv("LC_MESSAGES=en");
putenv("LANG=en"); 
setlocale(LC_ALL, 'en');

// Specify location of translation tables
echo bindtextdomain("myPHPApp", "./locale");
echo "<br>";

// Choose domain
echo textdomain("myPHPApp");
echo "<br>";

// Translation is looking for in ./locale/en/LC_MESSAGES/myPHPApp.mo now

// Print a test message
echo gettext("Welcome to My PHP Application");
echo "<br>";

// Or use the alias _() for gettext()
echo _("boolean_key");
echo "<br>";

echo _("key_with_description");
echo "<br>";


var_dump(_("key_with_description"));

echo setlocale(LC_ALL, 0);
echo "<br>";
echo getenv('LC_ALL');
echo "<br>";
echo getenv('LANGUAGE');
echo "<br>";
echo getenv('LC_MESSAGES');
echo "<br>";
echo getenv('LANG');

echo phpInfo();

?>

locale\en\LC_MESSAGES\myPHPApp.po:

msgid ""
msgstr ""
"Language: English\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: PhraseApp (phraseapp.com)\n"

msgid "boolean_key"
msgstr "--- true"

msgid "empty_string_translation"
msgstr ""

# This is the amazing description for this key!
msgid "key_with_description"
msgstr "Check it out! This key has a description! (At least in some formats)"

msgid "key_with_line-break"
msgstr "This translations contains\na line-break."

msgid "nested.deeply.key"
msgstr "Wow, this key is nested even deeper."

msgid "nested.key"
msgstr "This key is nested inside a namespace."

msgid "null_translation"
msgstr ""

msgid "pluralized_key"
msgid_plural ""
msgstr[0] "Only one pluralization found."
msgstr[1] "Wow, you have %s pluralizations!"

msgid "sample_collection\n"
msgstr "---\n- first item\n- second item\n- third item\n"

msgid "simple_key"
msgstr "simple key, simple message, so simple.2"

#, fuzzy
msgid "unverified_key"
msgstr "I need verification, please verify me! (In some formats we also export this status)"


msgfmt en\LC_MESSAGES\myPHPApp.po --output-file=en\LC_MESSAGES\myPHPApp.mo

我得到的输出是:

D:\Work\locale
myPHPApp
Welcome to My PHP Application
boolean_key
key_with_description
string(20) "key_with_description" en
en
en
en
en 

如您所见,putenv、setlocale、bindtextdomain - 所有命令都按预期工作。

我还检查了 phpInfo 是否检测到 gettext 和 mbstring 扩展名。

我也试过重启apache。

我也尝试这样做,使用 'de' 而不是 'en'。我也尝试使用 'ru' 和 'en-US'。所有人都给我相同的结果。 'en_US'(带下划线)更早中断,setlocale 忽略它并且 LC_ALL 的值保留为 LC_COLLATE=C;LC_CTYPE=Russian_Russia.1251;LC_MONETARY=C;LC_NUMERIC=C;LC_TIME=C.

我的系统上安装了英语和俄语。

这里有什么问题吗?

我正在使用 windows 7,PHP 5.6.40.

This article 帮助了我。解决方案是在 shell 中设置 LANG=en,然后从 shell.

中设置为 运行 XAMPP

Finally, to get gettext working in XAMPP, you need to set the LANG attribute on the XAMPP shell before you fire up the XAMPP Control Panel every time you want to use XAMPP.

The XAMPP shell is right inside the install folder, which means it should be at C:\xampp\xampp_shell.bat for most people. Run it (preferrably as an Administrator), and type the following:

set LANG=YOUR_LANGUAGE_CODE

YOUR_LANGUAGE_CODE needs to be replaced with the appropriate language code of your localisation. For example, if my localisation was for Simplified Chinese, it would look like this:

set LANG=zh_CN

Once done, use the following command to open the XAMPP Control Panel from the shell.

xampp-control