我正在使用带有 .mo 文件(使用 PoEdit)的 PHP Gettext 将法语(fr_FR.UTF-8)翻译成英语(en_US.UTF-8),它工作得很好,但并不是所有的字符串都被翻译了!我不是在谈论没有用 UTF-8 正确翻译的口音,只是没有翻译一些完整的字符串。
我已经从我的所有 PHP 文件中提取了要翻译的法语字符串 - _("stringtotranslate") - 感谢 shell 脚本:
find . -iname "*.php" | xargs xgettext -j --from-code=UTF-8 -o locale/default.pot
已正确生成所有字符串的default.pot ,没有丢失任何字符串。从那个 .pot 文件中,我为英文版创建了一个 default.po 文件。那个 .po 是 UTF-8 格式,而我需要它们的 PHP 文件也是 UTF-8 格式(没有 BOM)。
缓存没有问题:我为每个修改('default_' + filemtime)命名不同的.mo文件。
我能注意到:
- 翻译没有 utf-8 字符的短字符串。
- 长字符串或包含 utf-8 字符(法语口音)的字符串不会被翻译……但我发现了一个没有口音的短字符串示例,也没有翻译……(没有找到其他示例)。
我已经根据我的服务器(Apache 1.3.37、PHP 4.4.4、--with-gettext)设置了语言环境。语言环境是动态的,取决于在网站上点击的标志,这里是英文版:
putenv('LANGUAGE=eng');
setlocale(LC_ALL, 'en_US.UTF-8');
bindtextdomain('default', './locale');
bind_textdomain_codeset('default', 'UTF-8');
textdomain('default');
正如我所说,一些字符串正确翻译成英文,但不是每个字符串。
这是英文版网站:http: //librairiedescarres.com/2015/en/
这是 .po 文件的一部分:
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: Librairie des Carrés\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2015-08-03 14:57+0200\n"
"PO-Revision-Date: 2015-08-04 10:08+0200\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: en\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.8.2\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-SourceCharset: UTF-8\n"
#: about-us.php:10 about-us.php:31
#, php-format
msgid ""
"Initialement spécialisé dans l'histoire des sciences et des voyages, notre stock est aujourd'hui largement diversifié : "
"%slivres illustrés%s, %shistoire%s, %sagriculture%s, %sjardinage et architecture%s, %smédecine%s, %sbibliophilie%s, "
"%slivres rares et curieux du XVème au XXème siècle%s."
msgstr ""
"Initially specialised in Travel and Science, our stock is now largely diversified and includes literature and "
"%sillustrated books%s, %shistory%s, %sagriculture%s, %sgardens and architecture%s, %smedicine%s, %sbibliophile%s and "
"%srare books from the XVth to the XXth century%s."
(...)
我尝试了很多解决方案,但没有一个有效:
- 尝试其他语言环境,例如:“en_US”、“en_US.utf8”、“en_US.utf-8”...
- putenv('LANG=fra'), putenv('LC_ALL=fra')...
- 使用 Notepad++ 对生成的 .mo 文件强制使用 UTF-8 编码。
有什么解决办法吗?Apache、PHP 或 Gettext... 是否存在问题?