<p>我本打算建议使用<code>compiler</code>模块,但它忽略了注释:</p>
<p>f.py公司:</p>
<pre><code># For Translators: some useful info about the sentence below
_("Some string blah blah")
</code></pre>
<p>…以及编译器模块:</p>
^{pr2}$
<p>python2.6中的<a href="http://www.python.org/doc/2.5.2/lib/module-compiler.ast.html" rel="nofollow noreferrer">AST</a>模块似乎也是这样做的。在</p>
<p>不确定是否可行,但如果您使用三重引号字符串。。在</p>
<pre><code>"""For Translators: some useful info about the sentence below"""
_("Some string blah blah")
</code></pre>
<p>…您可以使用编译器模块可靠地解析Python文件:</p>
<pre><code>>>> 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))]))
</code></pre>
<p>我试图编写一个mode complete脚本来提取docstring—它是不完整的,但似乎抓住了大多数docstring:<a href="http://pastie.org/446156" rel="nofollow noreferrer">http://pastie.org/446156</a>(或<a href="http://github.com/dbr/so_scripts/tree/0bd66a21695a390cfa45f9ee26d7bed4eac10e5c/parse_py" rel="nofollow noreferrer">github.com/dbr/so_scripts</a>)</p>
<p>另一种更简单的选择是使用正则表达式,例如:</p>
<pre><code>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]
</code></pre>
<p>..输出:</p>
<pre><code>Line Number: 1
Message: ['For Translators: some useful info about the sentence below']
Line: _("Some string blah blah")
</code></pre>
<p>不确定<code>.pot</code>文件是如何生成的,因此我对该部分没有任何帮助。。在</p>