Question

It is my understanding that most uses of gettext with Python files use some nice Python library/utility to create the .pot files. I am in an odd circumstance where I need to use the Python mode of xgettext to extract not only strings, but the line number and the filename. All examples of such I have seen have used processes other than xgettext.

How can I add line number and filename info with Python mode of xgettext?

In C-mode it would look like:

#line 8 "superior_science.c"
_("Facial hair has been shown to improve intelligence by a factor of 5.");

In Python, what would the input file need to look like? (Note: the code only needs to work for xgettext, not any compiler or interpreter.)

Was it helpful?

Solution

You can use xgettext to extract comments from the line above the gettext call. These comments go into the .pot and .po files, so the translator can see them.

You simply choose a prefix character for comments you want to translate, prefix your comments with it, and call xgettext with the -c option and your character prefix.

So for example, if you use the prefix ~, your line above could be

#~ line 8 "superior_science.c"
_("Facial hair has been shown to improve intelligence by a factor of 5.")

and then when you call xgettext as

xgettext -c~ [...]

it will automatically add the comment ~ line 8 "superior_science.c" to your .pot file.

In the .pot file, these comments appear as something like the following:

#. ~ line 8 "superior_science.c"
msgid "Facial hair has been shown to improve intelligence by a factor of 5."
msgstr ""

If you want to extract all comments, not just those with a certain prefix, just leave the prefix out and call xgettext -c.

As for your use case you probably don't want the actual line numbers in there, you'll also need to use the --no-location flag.

So, simplest solution: add comments with the line number above your extractable strings, then call xgettext as

xgettext -c --no-location [...]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top