0

変換するすべての値を含む csv があります...csv ファイルを読み取って .pot ファイルを生成したいのですが、読み込んで print _(value) を呼び出そうとすると、.pot が生成されません。csv から動的コンテンツを含む .pot ファイルを生成できない理由がわかりません。

これが私のコードのスニペットです:

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:
  reader = csv.reader(csvfile, delimiter=',', quotechar='"')
  for row in reader:
    if row:
        if row[0] != '':
            print _(%row[0])

このスクリプトを実行すると、すべての値が表示されますが、実行しても .pot ファイルは生成されません。

xgettext -d api_trans -o api_trans.pot api_trans.py 

文字列値を持つ変数に対して実際の文字列を指定すること (例: print _('hello')) は機能します...どんな助けも大歓迎です。

4

1 に答える 1

0

これが発生する理由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

最後の部分については完全にはわかりません。詳細については、こちらを参照してください:)

于 2013-01-07T20:56:48.940 に答える