【问题标题】:How to assign particular context to --keyword for proper_name?如何将特定上下文分配给--keyword for proper_name?
【发布时间】:2017-09-06 13:20:47
【问题描述】:

使用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 转换为以下内容:

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

【问题讨论】:

    标签: gettext xgettext


    【解决方案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,但这是将其放入消息上下文的唯一方法。

    也许提交功能请求?

    还有一个 hack 可以在不更改源代码的情况下达到同样的效果。我假设您使用的是 C 和标准 Makefile(但您可以用其他语言做同样的事情):

    将文件POTFILES 复制到POTFILES-proper-names 并添加一行./proper_names.potPOTFILES.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-parsers,然后自己编写一个(或者在本文末尾使用我的)。

    现在,照常更新您的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”或使用供应商可能拥有的预构建版本。

    【讨论】:

      猜你喜欢
      • 2014-02-04
      • 2020-10-30
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多