问题
I have a pretty well-working command that extracts strings from all my .js and .html files (which are just Underscore templates). However, it doesn't seem to work for Translator comments.
For example, I have this in one of my .js files:
/// TRANSLATORS: The word "manual" stands for manual process
gettext("manual");
Using the following command:
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 should extract the comment from the .js file and put it into my .po file like this:
#. TRANSLATORS: The word "manual" stands for manual process
#: tax.js:200
msgid "manual" msgstr ""
But it doesn't. Am I doing something wrong here or are translator comments just not working in Python mode?
EDIT: I have accepted John Flatness' answer as the correct one, however I did find a workaround that enables me to still use the Python mode and extract translator comments. It's not perfect, because it actually leaves some of the syntax inside the comments:
In my tax.js
file:
/*
# This is a translator comment */
gettext("What is this?");
Run this command:
find . -iname '*.html' -o -iname '*.js' | xargs xgettext --language=Python --from-code=utf-8 --keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 -c
Result in .po file:
#. This is a translator comment */
#: tax.js:201
msgid "What is this?"
msgstr ""
As You can see, the only problems are that:
- I have to write the comment in 2 lines
- The comment terminator
*/
is left in the translator comments
This should not be much of an issue in most cases, though.
回答1:
The problem is that you're telling xgettext
that the source is Python, when it's really JavaScript.
This may make it work "well enough" for many cases, but I'd assume what's tripping things up here is that Python doesn't do one-liner comments with //
, it uses #
.
There's a project on Github that adds javascript support to the gettext tools. I'm not sure of its current status, and you'd have to build from source to use it. Otherwise, I suppose you could try other languages that xgettext
does support that have more C/C++/Java-like syntax.
回答2:
The way to get the "translator comment" without any pollution, is to add a hash #
after the javascript comment start //
, this way xgettext will interpret everything after it as a normal Python comment.
Try this:
//# This is a translation comment
console.log(_('Some String'));
And extract with xtext like:
xgettext --language=Python --from-code=utf-8 --force-po -c -o file.po file.js
This will create the PO as follows:
#. This is a translation comment
#: /path/to/file.js:2
msgid "Some String"
msgstr ""
I successfully did this using gettext 0.18.1.1-5ubuntu3 on Ubuntu 12.04.4
来源:https://stackoverflow.com/questions/12052844/extract-translator-comments-with-xgettext-from-javascript-in-python-mode