我正在将 Zend Framework 用于多语言站点。翻译是用 Zend_Translate_Adapter_Gettext 完成的。我正在使用 poedit 准备 .mo 文件。
问题是当我设置两个 msgid 并且一个 msgstr 与另一个 msgid 相同时:
#: application/modules/foobar/views/scripts/index.phtml:1
msgid "foo"
msgstr "bar"
#: application/modules/foobar/views/scripts/index.phtml:2
msgid "bar"
msgstr "baz"
当我使用此代码时:
<?php echo $this->translate('foo'); ?>
<?php echo $this->translate('bar'); ?>
输出是这样的:
bazbaz
我认为如果 msgstr 与不同的 msgid 重合,则将其用作 msgid 并因此再次翻译。如果我的推理有误,请纠正我。
现在,我想知道您是否遇到过类似的问题以及如何轻松绕过它。
我当前的解决方案包括更改 msgid:
#: application/modules/foobar/views/scripts/index.phtml:1
msgid "KEY_FOO"
msgstr "bar"
#: application/modules/foobar/views/scripts/index.phtml:2
msgid "KEY_BAR"
msgstr "baz"
进而:
<?php echo $this->translate('KEY_FOO'); ?>
<?php echo $this->translate('KEY_BAZ'); ?>
这不是一个令人满意的解决方案,因为我正在使用大约 10k 个文件的社区门户网站,我无法真正检查所有这些文件是否存在冲突。
如果它有任何用处:
- Zend 框架版本:1.10.8
- poedit版本:1.4.6
- 站点在 Apache 2.2.11 和 PHP 5.3 上运行
[编辑]
感谢 Gordon,我可以包含另一条数据:一个涉及 PHP 的测试gettext
。我使用了相同的测试 .mo 文件,包括 "foo"->"bar" 和 "bar"->"baz" 键值对。PHP 代码是这样的:
<?php
putenv('LC_ALL=pl_PL');
setlocale(LC_ALL, 'pl_PL');
bindtextdomain("pl", ".");
textdomain("pl");
echo gettext("foo");
echo gettext("bar");
?>
结果:
barbaz
所以这绝对不是gettext
他的错。