3

Python 3 を使用した Pyramid の国際化サポートの現在の状態は?

現在、Pyramid がこれに使用するパッケージ、lingua と babel は Python 3 と互換性がないようです。

https://bitbucket.org/felixschwarz/babel-py3kがありますが、公式リリースはありません。

lingua についても、lingua3k と呼ばれるものの影しか見つかりませんでしたが、どこでもリンクが壊れていて、公式リリースもありませんでした。

Pyramid i18n と Python 3 を使いこなすにはどうすればよいですか?

4

3 に答える 3

5

Py3 ステータス 2013 年 6 月 28 日

これは、たまたまこれに出くわした人のために、2013 年 3 月に投稿された 2 つの回答に対する更新された回答です。

Python 3 の Babel に関する作業は公式に公開されていません。メンテナーは、ここで解決されていないオープンチケットを持っています: http://babel.edgewall.org/ticket/209

しかし、個人のグループがトーチを手に取り、非公式の BitBucket リポジトリを開始して、Babel3 での作業を格納しています: https://bitbucket.org/babel3_developers/babel3

Pyramid もリンガを使用します。現在、easy_install/pip install lingua を試みると、失敗します。これの唯一の理由は、xlwt が公式に移植されていないことです。

lingua をインストールする場合は、手動で xlwt にパッチを適用する必要があります。

非公式の Babel3 をインストールする

現在、Python 3 での Babel の公式リリースはありません。また、pypi には何も投稿されていません (つまり、easy_install babel/babel3 または pip install babel/babel3 が正しく機能しません)。ただし、非公式リリースを使用できます。

  1. 非公式リリースを入手するには、まず mercurial (ソース管理ツール) をインストールする必要があります。これは、BitBucket からソース コードをチェックアウトするために必要です。Windows ユーザーの場合は、https ://www.mercurial-scm.org/ から入手できます。Linux/Unix の場合は、ディストリビューション ツールを使用してバイナリを取得するか、ソースからコンパイルします。
  2. コマンド プロンプトから、Babel3 ソース インストール ファイルを一時的に保存するディレクトリに移動します。hg clone https://bitbucket.org/babel3_developers/babel3 (Windows ユーザーは、hg.exe へのフル パスを入力するか、hg.exe のディレクトリをパスに追加する必要がある場合があります)
  3. これにより、setup.py ファイルを含むディレクトリがローカル マシン上に作成されます。新しいディレクトリに移動し、次のファイルを実行します: python setup.py install

Lingua のインストール

Pyramid の i18n は、lingua という名前の別の Python モジュールに依存しています。xlwt という名前の別のモジュールの依存関係に問題があります。ここでも非公式にパッチが適用されています: https://github.com/tonyroberts/xlwt

  1. https://github.com/tonyroberts/xlwt (右下の Download zip をクリック)
  2. コンテンツを一時的な場所に抽出する
  3. 結果のディレクトリ内でコマンド プロンプトをナビゲートします。
  4. 実行: python setup.py インストール
  5. 次の方法で lingua をインストールします。 pip install lingua -または- easy_install lingua
于 2013-06-28T16:25:54.387 に答える
3

I was unable to find any info about the status (current or future) of i18n helper packages for pyramid in python 3 (babel and lingua).

To solve this inconvenience, I did 4 things:

  1. Create a 2nd virtual environment with python2.7
  2. Add a specific i18n setup file called setup_i18n.py
  3. Write a simple wrapper script called translate.py which setup.py will call to do translations.
  4. Add an entry point in setup.py to point to the translate script.

Details:

  1. I created a very lean 2nd virtual and very lean env with python2.7 inside my project. Just called it env_python_2.7 and put it in the root of my project.

  2. Then in the directory where setup.py lives I created another setup file, setup_i18n.py. This file only makes requirements for just what is needed for i18n support. My setup_i18n.py file looks like:


from setuptools import setup, find_packages

requires = [
    'Babel',
    'lingua',
]

setup(name='my_project',
      packages=find_packages(),
      install_requires=requires,

      message_extractors={'.': [
          ('**.py', 'lingua_python', None),
          ('**.pt', 'lingua_xml', None),
          ]},
      )

  1. Then I created a script called translate.py and put that in the same scripts directory that initializedb.py goes into when you create a project from a template. The script is pretty general and could be made to have more specific targets, but it looks like this

import argparse
import subprocess
import sys

from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )

description = """\
A script to facilitate the translation of strings in the app.
"""
parser = argparse.ArgumentParser(description=description)
parser.add_argument('ini_file', help='Ini file to setup environment for this script')
parser.add_argument('-a', '--add', metavar='locale',
                    help='Add locale name of new language for translation (eg: en, de)')

def main(argv=sys.argv):
    args = parser.parse_args()

    setup_logging(args.ini_file)
    settings = get_appsettings(args.ini_file)

    # Python 2.7 is needed for translation strings at this time, because the
    # available translation string tools are not compatible with Python 3
    # at this time
    python27 = settings['my_project.python27_dir'] + '/bin/python'
    setup_file = settings['my_project.i18n_setup_file']

    if args.add:
        subprocess.call([python27, setup_file, 'init_catalog', '-l', args.add])

    subprocess.call([python27, setup_file, 'extract_messages'])
    subprocess.call([python27, setup_file, 'update_catalog'])
    subprocess.call([python27, setup_file, 'compile_catalog'])

if __name__ == '__main__':
    main()

  1. And finally, I modified the setup.py entry points to point to the translate script. My entry points look like:

entry_points="""\
[paste.app_factory]
main = my_project:main
[console_scripts]
my_project_db = my_project.script.initializedb:main
my_project_translate = my_project.script.translate:main
""",

Now I can i18n my app by calling 'python3 setup.py my_project_translate'

于 2013-03-11T04:28:37.270 に答える
1

Python アプリを自分のやり方で翻訳するには、通常、次の 2 つの異なる手順があります。

  • メッセージ カタログの抽出
  • 文字列の翻訳

最初の部分は Babel と lingua によって処理され、2 番目の部分は gettext (または、translationstringなどのラッパー) によって処理されます。

簡単な答え: カタログ作業には Poedit をダウンロードし、翻訳には通常の Pyramid ライブラリを使用してください。

長い答え

したがって、Babel と lingua がうまくいかない場合は、メッセージ カタログを自分で作成できます。次に、既存のファイルを使用して、すべての言語のファイルを.pot作成できます。Poedit.poを使用すると、ファイルを自動的にファイルにコンパイルできます。これらは、Python の標準ライブラリgettextを使用するだけで、依存関係なく処理できます。.po.mo

Poedit を使用してメッセージ カタログを作成します ( File->New Catalog)。すべての設定を慎重に適用してください。Poedit は文字列を抽出することもできます。ここで手作業を行う必要はないかもしれません。カタログ プロパティのソース パスソース キーワードを確認してください。

次に、すべての (抽出または手動で追加された) 文字列を翻訳します。Pyramid は次のディレクトリ構造を期待します

myproject/
    locale/
        en/
            LC_MESSAGES/
                myproject.po
                myproject.mo
        myproject.pot

ファイルはpotカタログです。enフォルダーは英語への翻訳用です。その他の言語については、ここにロケールを入力します:deドイツ語、esスペイン語など。.po空のファイルを作成して、Poedit に.potファイルから更新させることができます。

翻訳が完了したら、それらのファイルの操作に関する Pyramid のドキュメントを使用してください。他の方法でメッセージを抽出できる場合は、Babel は必要ありません。

補足として: 翻訳プロセスに使用するものはすべて Python の gettext をラップしているため、問題が発生した場合は、そのライブラリにアクセスすることもできます。有効なカタログがあれば、これはどのような場合でも機能します。

于 2013-03-08T13:30:54.680 に答える