通过 Locale::Maketext::Simple 的本地化总是回退到默认而不是 .po 条目

Localization via Locale::Maketext::Simple always falls back to default instead of .po entry

在 perl 模块中,我想使用 https://metacpan.org/pod/Locale::Maketext::Simple 将字符串转换为不同的语言。

我的 .po 文件位于 /opt/x/languages 下,例如/opt/x/languages/en.po.

在我的模块中,我使用以下 header:

use Locale::Maketext::Simple (
    Path => '/opt/x/languages',
    Style => 'maketext'
);
loc_lang('en');

.po 文件中的条目如下所示:

msgid "string [_1] to be converted"
msgstr "string [_1] is converted"

并且使用 msgfmt -c en.po 通过控制台进行的检查到目前为止没有抛出任何错误。

但是当我用 loc() 转换字符串时,比如 loc("string [_1] to be converted", "xy") 它给我输出 "string xy to be converted" 而不是我期望的 "string xy is converted" 。在我看来,.po 文件未正确加载。

如何检查在 maketext 实例化过程中找到了哪些 .po 文件?还是我把事情搞混了,有一个普遍的错误?


编辑 1: 感谢您的评论,但它仍然不起作用。

我已经使用 https://poedit.net/ 检查了文件,并使用此工具创建了相应的 .mo 文件(目前用于 de 和 en)。它们位于 .po 文件旁边(在 /opt/x/languages 内)。

为了完整起见,我的 header 看起来像这样:

# MY OWN LANGUAGE FILE (DE)
# 06-2019 by me
#
msgid ""
msgstr ""
"Project-Id-Version: 1.0.0\n"
"POT-Creation-Date: 2019-06-01 00:00+0100\n"
"PO-Revision-Date: 2019-06-02 00:00+0100\n"
"Last-Translator: thatsme <me@me.de>\n"
"Language-Team: unknown\n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.2.3\n"

msgid "string [_1] to be converted"
msgstr "string [_1] is converted"

经过更多的挖掘和测试,我终于找到了这个行为的问题,所以这是我目前的解决方案。希望这可能对其他人有所帮助,因为您很少能找到关于此主题的任何好的文档:

添加库

我添加了库https://metacpan.org/pod/Locale::Maketext::Simple as stated above, but forgot to add https://metacpan.org/pod/Locale::Maketext::Lexicon

我花了很长时间才看到,因为没有抛出异常或错误,只是……什么都没有。

在 Maketext::Simple 文档中说

If Locale::Maketext::Lexicon is not present, it implements a minimal localization function by simply interpolating [_1] with the first argument, [_2] with the second, etc.

乍一看 .po 文件加载时没有 Maketext::Lexikon,但它只是替换了占位符。

其他问题:

然后我发现所有字符串都被翻译了,除了那些带有占位符的 [_1]。我找不到原因,但我搬到了

Style => 'gettext'

并将所有 [_1][_2]... 替换为 %1%2... - 这很有魅力。