简体   繁体   English

在Windows 7家用版和PHP 7.0.8上使用gettext进行国际化失败

[英]Internationalization with gettext on Windows 7 home and PHP 7.0.8 fails

At work we have decided to translate our site into 3 additional languages and I have been looking into how to do this. 在工作中,我们决定将网站翻译成3种其他语言,我一直在研究如何做到这一点。 I've spent the better part of the day searching google and stackoverflow, trying every different solution but I can't make it work. 我一天中大部分时间都在搜索google和stackoverflow,尝试了每种不同的解决方案,但我无法使其正常工作。

I'm starting to fear that it is impossible to do in Windows 7 Home edition (My development machine). 我开始担心在Windows 7家庭版(我的开发机)中是不可能做到的。

// C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.mo
// C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.po
msgid ""
msgstr ""
"Project-Id-Version: iArbete 1.0.0\n"
"Report-Msgid-Bugs-To: ******** \n"
"Last-Translator: ********\n"
"Language: en_GB\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "iArbete"
msgstr "workIng"


// C:\Apache24\htdocs\index.php
if(isset($_GET["locale"]))
{
    $locale = $_GET["locale"];
}
else
{
    $locale = 'en_GB';
}

putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
echo getenv("LC_ALL");

$domain = 'messages';
bindtextdomain($domain, './locale');
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');

echo _("iArbete");

echo _("iArbete"); Should translate to "workIng" but it does not. 应该翻译为“ workIng”,但不是。 Or am I not getting the concept? 还是我不明白这个概念?

--- More information -------------------------------------------------------- - - 更多信息 - - - - - - - - - - - - - - - - - - - - - - - -----------

I've made some changes and recompiled the mo file and this is how it looks now. 我进行了一些更改并重新编译了mo文件,这就是现在的样子。

// C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.mo
// C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.po
msgid ""
msgstr ""
"Project-Id-Version: iArbete 1.0.0\n"
"Report-Msgid-Bugs-To: ******** \n"
"Last-Translator: ********\n"
"Language: en_GB\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "workIng"
msgstr "iArbete"

msgid "Menu"
msgstr "Meny"


// C:\Apache24\htdocs\index.php
if(isset($_GET["locale"]))
{
    $locale = $_GET["locale"];
}
else
{
    $locale = 'sv_SE';
    $locale = 'en_GB';
//  $locale = 'ar_AE';
}

putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
//  echo getenv("LC_ALL");

$domain = 'messages';
bindtextdomain($domain, './locale');
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');

echo _("workIng");
echo _("Menu");

msgid "workIng" is translated to "iArbete" msgid "Menu" is not translated to "Meny" msgid "workIng"未翻译为“ iArbete” msgid "Menu"未翻译为“ Meny”

What is going on? 到底是怎么回事? It feels so weird that only the first word get translated. 感觉很奇怪,只翻译了第一个单词。 So I rearranged the words in the po and recompiled but still "workIng" is the only word that gets translated?? 因此,我重新整理了po中的单词并重新编译,但仍然只有workIng是被翻译的单词?

I've uploades my files to google drive here if anyone would like to have a look. 我有我的uploades文件,以推动谷歌在这里如果有人想看看。

You'll need to convert your PO file into a MO file using msgfmt : 您需要使用msgfmt将PO文件转换为MO文件:

The letters PO in '.po' files means Portable Object, to distinguish it from '.mo' files, where MO stands for Machine Object. “ .po”文件中的字母PO表示可移植对象,以将其与“ .mo”文件区分开,其中MO代表机器对象。 [...] [...]

PO files are meant to be read and edited by humans, and associate each original, translatable string of a given package with its translation in a particular target language. PO文件旨在供人类阅读和编辑,并将给定程序包的每个原始可翻译字符串与特定目标语言的译文相关联。 [...] [...]

MO files are meant to be read by programs, and are binary in nature. MO文件应由程序读取,并且本质上是二进制文件。 [...] [...]

Once the PO file is complete and dependable, the 'msgfmt' program is used for turning the PO file into a machine-oriented format, which may yield efficient retrieval of translations by the programs of the package, whenever needed at runtime (see MO Files ). 一旦PO文件完整且可靠,便可以使用“ msgfmt”程序将PO文件转换为面向机器的格式,这可以在运行时需要时通过程序包的程序高效检索翻译(请参见MO Files)。 )。 See msgfmt Invocation , for more information about all modes of execution for the 'msgfmt' program. 有关'msgfmt'程序的所有执行模式的更多信息,请参见msgfmt调用

— The GNU gettext manual, sections 1.4 and 1.5 — GNU gettext手册, 第1.4和1.5节

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

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