自动为pot文件添加注释

时间:2023-01-13 20:32:20

I want to pull certain comments from my py files that give context to translations, rather than manually editing the .pot file basically i want to go from this python file:

我想从我的py文件中提取某些注释,为翻译提供上下文,而不是手动编辑.pot文件,基本上我想从这个python文件:

# For Translators: some useful info about the sentence below
_("Some string blah blah")

to this pot file:

到这个锅文件:

# For Translators: some useful info about the sentence below
#: something.py:1
msgid "Some string blah blah"
msgstr ""

2 个解决方案

#1


After much pissing about I found the best way to do this:

经过多次小便,我发现了最好的方法:

#. Translators:
# Blah blah blah
_("String")

Then search for comments with a . like so:

然后用a搜索评论。像这样:

xgettext --language=Python --keyword=_ --add-comments=. --output=test.pot *.py

#2


I was going to suggest the compiler module, but it ignores comments:

我打算建议编译器模块,但它忽略了注释:

f.py:

# For Translators: some useful info about the sentence below
_("Some string blah blah")

..and the compiler module:

..和编译器模块:

>>> import compiler
>>> m = compiler.parseFile("f.py")
>>> m
Module(None, Stmt([Discard(CallFunc(Name('_'), [Const('Some string blah blah')], None, None))]))

The AST module in Python 2.6 seems to do the same.

Python 2.6中的AST模块似乎也是如此。

Not sure if it's possible, but if you use triple-quoted strings instead..

不确定是否可能,但如果你使用三引号字符串代替..

"""For Translators: some useful info about the sentence below"""
_("Some string blah blah")

..you can reliably parse the Python file with the compiler module:

..你可以使用编译器模块可靠地解析Python文件:

>>> m = compiler.parseFile("f.py")
>>> m
Module('For Translators: some useful info about the sentence below', Stmt([Discard(CallFunc(Name('_'), [Const('Some string blah blah')], None, None))]))

I made an attempt at writing a mode complete script to extract docstrings - it's incomplete, but seems to grab most docstrings: http://pastie.org/446156 (or on github.com/dbr/so_scripts)

我试图编写一个模式完整的脚本来提取文档字符串 - 它不完整,但似乎抓住了大多数文档字符串:http://pastie.org/446156(或在github.com/dbr/so_scripts上)

The other, much simpler, option would be to use regular expressions, for example:

另一个更简单的选择是使用正则表达式,例如:

f = """# For Translators: some useful info about the sentence below
_("Some string blah blah")
""".split("\n")

import re

for i, line in enumerate(f):
    m = re.findall("\S*# (For Translators: .*)$", line)
    if len(m) > 0 and i != len(f):
        print "Line Number:", i+1
        print "Message:", m
        print "Line:", f[i + 1]

..outputs:

Line Number: 1
Message: ['For Translators: some useful info about the sentence below']
Line: _("Some string blah blah")

Not sure how the .pot file is generated, so I can't be any help at-all with that part..

不确定.pot文件是如何生成的,因此我无法对该部分提供任何帮助。

#1


After much pissing about I found the best way to do this:

经过多次小便,我发现了最好的方法:

#. Translators:
# Blah blah blah
_("String")

Then search for comments with a . like so:

然后用a搜索评论。像这样:

xgettext --language=Python --keyword=_ --add-comments=. --output=test.pot *.py

#2


I was going to suggest the compiler module, but it ignores comments:

我打算建议编译器模块,但它忽略了注释:

f.py:

# For Translators: some useful info about the sentence below
_("Some string blah blah")

..and the compiler module:

..和编译器模块:

>>> import compiler
>>> m = compiler.parseFile("f.py")
>>> m
Module(None, Stmt([Discard(CallFunc(Name('_'), [Const('Some string blah blah')], None, None))]))

The AST module in Python 2.6 seems to do the same.

Python 2.6中的AST模块似乎也是如此。

Not sure if it's possible, but if you use triple-quoted strings instead..

不确定是否可能,但如果你使用三引号字符串代替..

"""For Translators: some useful info about the sentence below"""
_("Some string blah blah")

..you can reliably parse the Python file with the compiler module:

..你可以使用编译器模块可靠地解析Python文件:

>>> m = compiler.parseFile("f.py")
>>> m
Module('For Translators: some useful info about the sentence below', Stmt([Discard(CallFunc(Name('_'), [Const('Some string blah blah')], None, None))]))

I made an attempt at writing a mode complete script to extract docstrings - it's incomplete, but seems to grab most docstrings: http://pastie.org/446156 (or on github.com/dbr/so_scripts)

我试图编写一个模式完整的脚本来提取文档字符串 - 它不完整,但似乎抓住了大多数文档字符串:http://pastie.org/446156(或在github.com/dbr/so_scripts上)

The other, much simpler, option would be to use regular expressions, for example:

另一个更简单的选择是使用正则表达式,例如:

f = """# For Translators: some useful info about the sentence below
_("Some string blah blah")
""".split("\n")

import re

for i, line in enumerate(f):
    m = re.findall("\S*# (For Translators: .*)$", line)
    if len(m) > 0 and i != len(f):
        print "Line Number:", i+1
        print "Message:", m
        print "Line:", f[i + 1]

..outputs:

Line Number: 1
Message: ['For Translators: some useful info about the sentence below']
Line: _("Some string blah blah")

Not sure how the .pot file is generated, so I can't be any help at-all with that part..

不确定.pot文件是如何生成的,因此我无法对该部分提供任何帮助。