python应用国际化:Babel

BabelPython 的一个国际化工具包,原本为 Edgewall.org 下的一个项目,现在已经转为由 pocoo.org 维护。

统一管理 Python 虚拟环境

yaourt -S python-virtualenvwrapper
mkdir $HOME/.virtualenvs
echo 'export WORKON_HOME=$HOME/.virtualenvs' >> ~/.bashrc
echo 'source virtualenvwrapper.sh' >> ~/.bashrc
source ~/.bashrc

建立学习 Babel 专用虚拟环境

mkvirtualenv --python=/usr/bin/python2 --no-site-packages LearnBabel

编译安装 Babel

git clone https://github.com/mitsuhiko/babel.git
cd babel
pip install pytz
python setup.py import_cldr
pip install --editable .

常用信息国际化

>>> from babel import Locale
>>> locale = Locale('en', 'US')
>>> print locale.territories['CN']
China
>>> locale = Locale('zh')
>>> print locale.territories['CN']
中国
>>> month_names = locale.months['format']['wide'].items()
>>> for idx, name in sorted(month_names):
...   print name
... 
一月
二月
三月
四月
五月
六月
七月
八月
九月
十月
十一月
十二月
>>> from datetime import date
>>> from babel.dates import format_date
>>> today = date.today()
>>> print format_date(today, locale='zh')
2014年3月9日

任意信息国际化

  • 创建python程序: hello.py

    import gettext
    gettext.bindtextdomain('messages', './hello.i18n')
    _ = gettext.gettext
    print _('Hello')
    
  • 创建Babel配置文件: hello.babel

    [python: hello.py]
    
  • 准备翻译成中文

    mkdir hello.i18n
    pybabel extract -F hello.babel . -o hello.i18n/messages.pot    # 从源代码中提取可翻译的文本到POT(PO模板)文件
    pybabel init -l zh_CN -d ./hello.i18n -i ./hello.i18n/messages.pot    # 将POT文本拷贝出一份指定语言的PO文件
    
  • 译成中文后的PO文件: ./hello.i18n/zh_CN/LC_MESSAGES/messages.po

    # Chinese (Simplified, China) translations for PROJECT.
    # Copyright (C) 2014 ORGANIZATION
    # This file is distributed under the same license as the PROJECT project.
    # FIRST AUTHOR <EMAIL@ADDRESS>, 2014.
    #
    #, fuzzy
    msgid ""
    msgstr ""
    "Project-Id-Version: PROJECT VERSION\n"
    "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
    "POT-Creation-Date: 2014-03-09 19:08+0800\n"
    "PO-Revision-Date: 2014-03-09 19:12+0800\n"
    "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
    "Language-Team: zh_Hans_CN <LL@li.org>\n"
    "Plural-Forms: nplurals=2; plural=(n != 1)\n"
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=utf-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Generated-By: Babel 2.0-dev\n"
    
    #: hello.py:1
    msgid "Hello"
    msgstr "喂"
    
    
  • 编译翻译结果

    pybabel compile -f -d ./hello.i18n    #编译PO文件为MO文件
    
  • 英文环境下运行程序

    python ./hello.py
    Hello
    
  • 中文环境下运行程序

    LC_ALL=zh_CN python ./hello.py
    喂
    
  • 更新 hello.py ,追加一句“ print _('World')”
  • 将“World”翻译成中文

    pybabel extract -F hello.babel . -o hello.i18n/messages.pot    # 从源代码中提取可翻译的文本到POT(PO模板)文件
    pybabel update -l zh_CN -d ./hello.i18n -i ./hello.i18n/messages.pot    # 从POT文本更新指定语言的PO文件
    # 开始翻译指定语言的PO文件: hello.i18n/zh_CN/LC_MESSAGES/messages.po
    pybabel compile -f -d ./hello.i18n    #编译PO文件为MO文件
    
  • 再次运行

    LC_ALL=zh_CN python ./hello.py
    喂
    世界
    
  • 程序中强制使用中文语言: hello.cn.py

    import gettext
    
    t = gettext.translation('messages', './hello.i18n', languages=['zh_CN'])
    _ = t.gettext
    
    print _('Hello')
    print _('World')
    
  • 即使是在英文环境下也输出中文

    LC_ALL=en_US python ./hello.py
    喂
    世界