如何为特定名称的--keyword指定特定上下文?

时间:2017-09-06 13:20:47

标签: gettext xgettext

使用xgettext工具时,可以自动添加评论以帮助翻译人员了解正确的名称(如documented)。

文档建议将以下内容添加到命令行:

--keyword='proper_name:1,"This is a proper name. See the gettext manual, section Names."'

这导致正确的名称被提取到.pot文件,如下所示:

#. This is a proper name. See the gettext manual, section Names.
#: ../Foo.cpp:18
msgid "Bob"
msgstr ""

这个问题;是没有为该字符串定义特定的上下文。理想情况下,如何提取正确的名称:

#. This is a proper name. See the gettext manual, section Names.
#: ../Foo.cpp:18
msgctxt "Proper Name"
msgid "Bob"
msgstr ""

我尝试过以下但没有成功:

# Hoping that 0 would be the function name 'proper_name'.
--keyword='proper_name:0c,1,"This is a proper name. See the gettext manual, section Names."'

# Hoping that -1 would be the function name 'proper_name'.
--keyword='proper_name:-1c,1,"This is a proper name. See the gettext manual, section Names."'

# Hoping that the string would be used as the context.
--keyword='proper_name:"Proper Name"c,1,"This is a proper name. See the gettext manual, section Names."'

# Hoping that the string would be used as the context.
--keyword='proper_name:c"Proper Name",1,"This is a proper name. See the gettext manual, section Names."'

是否有办法强制将特定msgctxt用于通过关键字提取的所有字符串(例如上述示例中的proper_name)?

如果没有选项以xgettext原样实现这一点,那么我考虑使用以下内容:

--keyword='proper_name:1,"<PROPERNAME>"'

结果:

#. <PROPERNAME>
#: ../Foo.cpp:18
msgid "Bob"
msgstr ""

然后问题变成了;如何自动将生成的.pot文件中出现的所有内容转换为以下内容:

#. This is a proper name. See the gettext manual, section Names.
#: ../Foo.cpp:18
msgctxt "Proper Name"
msgid "Bob"
msgstr ""

1 个答案:

答案 0 :(得分:1)

如果要提取消息上下文,则必须将其作为参数列表的一部分。并且“Nc”中的数字部分必须是正整数。你用0,-1的所有尝试都没有结果,对不起。

您的功能的签名必须如下所示:

#define PROPER_NAME "Proper Name"
const char *proper_name(const char *ctx, const char *name);

然后像这样称呼它:

proper_name(PROPER_NAME, "Bob");

在整个代码中重复了PROPER_NAME,但这是将其纳入消息上下文的唯一方法。

可能提交功能请求?

还有一个黑客可以在不更改源代码的情况下实现相同的目标。我假设你使用的是C和标准的Makefile(但你可以用其他语言做同样的事情):

将文件POTFILES复制到POTFILES-proper-names,并向./proper_names.pot添加一行POTFILES.in

然后你必须创建proper_names.pot

xgettext --files-from=POTFILES-proper-names \
         --keyword='' \
         --keyword='proper_names:1:"Your comment ..."' \
         --output=proper_names.pox

现在只包含使用“proper_names()”创建的条目。现在添加上下文:

msg-add-content proper_names.pox "Proper Name" >proper_names.pot
rm proper_names.pot

不幸的是,没有名为“msg-add-content”的程序。抓住其中一个极好的po解析器,然后自己写一个(或者在这篇文章末尾拿我的)。

现在,像往常一样更新PACKAGE.pot。由于“proper_names.pox”是主xgettext运行的输入文件,因此添加了上下文的所有提取的专有名称都会添加到您的pot文件中(并且将使用它们的上下文)。

除了为.pot文件中的所有条目添加消息上下文的另一个脚本之外,请使用以下脚本:

#! /usr/bin/env perl

use strict;

use Locale::PO;

die "usage: $0 POFILE CONTEXT" unless @ARGV == 2;

my ($input, $context) = @ARGV;

my $entries = Locale::PO->load_file_asarray($input) or die "$input: failure";
foreach my $entry (@$entries) {
    $entry->msgctxt($context) unless '""' eq $entry->msgid;
    print $entry->dump;
}

你必须为它安装Perl库“Locale :: PO”,或者使用“sudo cpan install Locale :: PO”或者使用你的供应商可能拥有的预构建版本。