gettextを使うとPythonのスクリプトを多言語対応することができます。
gettextを使った多言語対応のサンプル
gettext-sample.py
#!/usr/bin/env python
# coding: utf-8
import gettext
_ = gettext.translation( \
domain='message',
localedir='locale',
languages=['ja'],
fallback=True).ugettext
print(_(u'hello, world!'))
message.po
# SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR ORGANIZATION # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. # msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "POT-Creation-Date: 2013-03-22 00:57+JST\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <[email protected]>\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Generated-By: pygettext.py 1.5\n" #: gettext-sample.py:12 msgid "hello, world!" msgstr "こんにちは世界!"
ファイルの配置
. |-- gettext-sample.py |-- locale `-- ja `-- LC_MESSAGES `-- message.mo 3 directories, 2 files
スクリプトの実行結果
$ python gettext-sample.py こんにちは世界