Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xgettext从JavaScript中提取转换器注释(在Python模式下)_Javascript_Python_Gettext_Xgettext - Fatal编程技术网

使用xgettext从JavaScript中提取转换器注释(在Python模式下)

使用xgettext从JavaScript中提取转换器注释(在Python模式下),javascript,python,gettext,xgettext,Javascript,Python,Gettext,Xgettext,我有一个非常好用的命令,它可以从我所有的.js和.html文件(它们只是下划线模板)中提取字符串。然而,它似乎不适用于译者评论 例如,我在一个.js文件中有这样的内容: /// TRANSLATORS: The word "manual" stands for manual process gettext("manual"); 使用以下命令: find . -iname '*.html' -o -iname '*.js' | xargs xgettext --language=Python -

我有一个非常好用的命令,它可以从我所有的.js和.html文件(它们只是下划线模板)中提取字符串。然而,它似乎不适用于译者评论

例如,我在一个.js文件中有这样的内容:

/// TRANSLATORS: The word "manual" stands for manual process
gettext("manual");
使用以下命令:

find . -iname '*.html' -o -iname '*.js' | xargs xgettext --language=Python --from-code=utf-8 --keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 --add-comments=/ 
xgettext应该从.js文件中提取注释并将其放入my.po文件中,如下所示:

#. TRANSLATORS: The word "manual" stands for manual process
#: tax.js:200 
msgid "manual"     msgstr "" 
但事实并非如此。我是否在这里做错了什么,或者翻译器注释只是不能在Python模式下工作

编辑:我已经接受了John Flatness的正确答案,但是我确实找到了一个解决办法,使我能够继续使用Python模式并提取翻译注释。它并不完美,因为它实际上在注释中留下了一些语法:

在我的
tax.js
文件中:

/*
# This is a translator comment */
gettext("What is this?");
运行以下命令:

find . -iname '*.html' -o -iname '*.js' | xargs xgettext --language=Python --from-code=utf-8 --keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 -c
生成.po文件:

#. This is a translator comment */
#: tax.js:201
msgid "What is this?"
msgstr ""
如您所见,唯一的问题是:

  • 我必须用两行字写评论
  • 注释终止符
    */
    保留在转换器注释中

  • 不过,在大多数情况下,这应该不是什么大问题。

    问题在于您告诉
    xgettext
    源代码是Python,而实际上是JavaScript

    这可能会使它在很多情况下工作得“足够好”,但我认为这里出现的问题是Python没有使用
    /
    进行一行注释,而是使用
    .

    有一种方法可以向gettext工具添加javascript支持。我不确定它的当前状态,您必须从源代码构建才能使用它。否则,我想您可以尝试其他
    xgettext
    支持的语言,这些语言具有更多类似C/C++/Java的语法。

    获得“翻译注释”而不受任何污染的方法是在javascript注释开始
    /
    之后添加一个哈希值
    ,这样,xgettext将把它之后的所有内容解释为普通的Python注释

    试试这个:

    //# This is a translation comment
    console.log(_('Some String'));
    
    并使用xtext进行提取,如:

    xgettext --language=Python --from-code=utf-8 --force-po -c -o file.po file.js
    
    这将创建采购订单,如下所示:

    #. This is a translation comment
    #: /path/to/file.js:2
    msgid "Some String"
    msgstr ""
    

    我在Ubuntu12.04.4上使用gettext 0.18.1.1-5ubuntu3成功地做到了这一点,这非常有意义。谢谢您应该将您的解决方案移至下面的答案,因为我发现它很有用。@如果有更干净的解决方案,请参阅下面的我的答案。