首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gettext忽略.mo文件

gettext忽略.mo文件
EN

Stack Overflow用户
提问于 2022-03-14 20:39:22
回答 1查看 103关注 0票数 0

我试着让文本正常工作,一切看起来都很好,但无论如何也不会翻译我的文字。我在堆栈溢出上看了大约5篇不同的文章,在谷歌上又读到了5篇--没有任何帮助。我现在拥有的是:

  1. 我从php.net获取php文件
  2. 我以这里中的po文件为例
  3. 我从这里获取了gettext二进制文件

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

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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

我得到的输出是:

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-17 07:58:55

这篇文章帮了我。解决方案是在shell中设置LANG=en,然后从这个精确的shell运行XAMPP。

最后,为了让gettext在XAMPP中工作,您需要在每次想使用XAMPP之前在XAMPP外壳上设置LANG属性。 XAMPP就在安装文件夹中,这意味着对于大多数人来说,它应该位于C:\xampp\xampp_shell.bat。运行它(最好是作为管理员),并键入以下内容: set LANG=YOUR_LANGUAGE_CODE 需要用本地化的适当语言代码替换YOUR_LANGUAGE_CODE。例如,如果我的本地化是使用简体中文,则如下所示: set LANG=zh_CN 完成之后,使用以下命令从shell中打开XAMPP控制面板。 xampp-control

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71473985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档