2009-04-13 62 views
0

我想拉从我的PY文件来翻译给语境,而不是手动编辑.POT文件中的某些注释基本上我想从这个Python文件去:添加注释锅文件自动

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

此壶文件:

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

回答

2

得罪很多关于我发现后,要做到这一点的最好办法:

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

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

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

这是一个.. *位*比编译器模块搞乱更好.. – dbr 2009-04-14 20:24:28

1

我要建议的compiler模块,但是却忽略了评论:

f.py:

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

..和编译器模块:

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

AST模块在Python 2.6,似乎这样做。

不知道这是可能的,但如果你使用三引号字符串,而不是..

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

..你能可靠地解析和编译器模块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))])) 

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

其他,更简单,选择是使用正则表达式,例如:

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") 

不知道如何生成.pot文件,所以我不能要不惜一切的那部分的任何帮助..

+0

感谢您的想法,我设法找到一个更简单的方法,虽然使用xgettext。 – wodemoneke 2009-04-14 17:12:34