我要建議的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
文件,所以我不能要不惜一切的那部分的任何幫助..
來源
2009-04-14 16:00:27
dbr
這是一個.. *位*比編譯器模塊搞亂更好.. – dbr 2009-04-14 20:24:28