在使用xgettext工具时,可以自动添加注释,以帮助翻译人员找到正确的名称(如记录在案)。
文档建议在命令行中添加以下内容:
--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 ""发布于 2017-09-15 08:01:53
如果要提取消息上下文,则必须将其作为参数列表的一部分。"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,并向POTFILES.in添加一行./proper_names.pot。
那么您必须创建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”的程序。抓住无数的解析器中的一个,自己写一个(或者在这篇文章的末尾写我的)。
现在,像往常一样更新您的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”,或者使用您的供应商可能有的预构建版本。
https://stackoverflow.com/questions/46076421
复制相似问题