かぴぶろぐ

またかぴったかと思った・・・(´A`;)

pylons i18nで国際化

カテゴリ[ Python ]

基本オフィシャル通りにやれば出来るのだが、コマンドメモ
前提

  • helloworldプロジェクト作ってる前提
  • Babelが入ってること。

入ってなければ

easy_install Babel

でさっくり入れる。


setup.py

message_extractors = {'translate_demo': [
        ('**.py', 'python', None),
        ('templates/**.mako', 'mako', None),
        ('public/**', 'ignore', None)]},

上記の部分がコメントアウトされてるので外す。

次にproject_rootの中にi18nディレクトリを作成(controllersやmodelディレクトリがある場所で)

メッセージをプロジェクトから抽出する

python25 setup.py extract_messages

enの設定を作る

python25 setup.py init_catalog -l en

コンパイルしてmoファイルを作る

python25 setup.py compile_catalog

エラーとか

raise exceptions.CompileException("Could not read template using encoding of 'ascii'.  Did you forget a magic encoding comment?", self.text.decode('utf-8', 'ignore'), 0, 0, self.filename)
mako.exceptions.CompileException: Could not read template using encoding of 'ascii'.  Did you forget a magic encoding comment? at line: 0 char: 0

みたいなのでたら、各テンプレートの頭に# coding: utf-8を書く。

 

> python25 setup.py compile_catalog
running compile_catalog
Traceback (most recent call last):
  File "setup.py", line 37, in <module>
    """,
  File "/opt/apps/python2.5.2/lib/python2.5/distutils/core.py", line 151, in setup
    dist.run_commands()
  File "/opt/apps/python2.5.2/lib/python2.5/distutils/dist.py", line 974, in run_commands
    self.run_command(cmd)
  File "/opt/apps/python2.5.2/lib/python2.5/distutils/dist.py", line 994, in run_command
    cmd_obj.run()
  File "/opt/apps/python2.5.2/lib/python2.5/site-packages/Babel-0.9.4-py2.5.egg/babel/messages/frontend.py", line 142, in run
    catalog = read_po(infile, locale)
  File "/opt/apps/python2.5.2/lib/python2.5/site-packages/Babel-0.9.4-py2.5.egg/babel/messages/pofile.py", line 198, in read_po
    line = line.strip().decode(catalog.charset)
  File "/opt/apps/python2.5.2/lib/python2.5/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x82 in position 8: unexpected code byte

とか出る場合。poファイルのエンコードを確認する。だいたいutf8じゃなくて・・・という寂しいオチorz

http://kapi.jp/kapi_blog/216

2009年02月27日

関連カテゴリ Python

この記事のコメント

この記事にコメントする