2009-04-13 57 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