これが発生する理由xgettext
は、実際には Python ファイルを実行するのではなく、プレーン テキストとして読み取り、翻訳する必要がある文字列を識別しようとするためです。_(varaible)
aと notを追加している_("literal string that xgettext understands")
ため、翻訳する必要があるテキストとして認識されません。
そのために、私が考えることができる最も直接的な方法は、フィードするダミー ファイルを生成することです。このファイルxgettext
には、すべての実際の値が含まれます。csv ファイルがそれほど大きくないことを考えると、 に似た行を含みprint _("the value of the thing you want to translate")
、xgettext
. もちろん、これは最適な方法ではありませんが、解析について心配する必要がない最も簡単な方法です。
import csv
import gettext
t = gettext.translation('api_trans', '/path/to/locale/', fallback=True)
_ = t.ugettext
string_list = []
with open('/path/to/csv/file') as csvfile:
with open('/path/to/some/other/file', 'w') as out:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in reader:
if row:
if row[0] != '':
out.write("print _(%s)\n" % (repr(row[0],))
次に、実行しxgettext -d api_trans -o api_trans.pot /path/to/that/file
ます。
繰り返しますが、これは準最適です。Babelを調べて、より効果的な解決策を見つけたいと思うかもしれません。私もそれを調べます。
それが私がBabelを使って得たものです:
def extract_csv(fileobj, keywords, comment_tags, options):
"""Extract messages from XXX files.
:param fileobj: the file-like object the messages should be extracted
from
:param keywords: a list of keywords (i.e. function names) that should
be recognized as translation functions
:param comment_tags: a list of translator tags to search for and
include in the results
:param options: a dictionary of additional options (optional)
:return: an iterator over ``(lineno, funcname, message, comments)``
tuples
:rtype: ``iterator``
"""
import csv
reader = csv.reader(fileobj, delimiter=',', quotechar='"')
for row in reader:
if row and row[0] != '':
yield (lineno, ',', row[0], 'No comment')
次に、それを抽出方法として含める必要があります。最も簡単な方法は、 のパッケージにPYTHONPATH
入れ、次のように使用することです。
# Some custom extraction method
[extractors]
csv = mypackage.module:extract_csv
[csv: **.ctm]
some_option = foo
最後の部分については完全にはわかりません。詳細については、こちらを参照してください:)