python国际化(i18n)和中英文切换
Python通过gettext模块支持国际化(i18n),可以实现程序的多语言界面的支持,下面是我的多语言支持实现:
1. 在python安装目录下的./Tools/i18n/(windows下例 D:\Program Files\Python25\Tools\i18n)目录中找到pygettext.py运行之,生成翻译文件模版messages.pot,内容大概是这个样子:
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2007-01-24 15:05+China Standard Time\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME \n"
"Language-Team: LANGUAGE \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: ENCODING\n"
"Generated-By: pygettext.py 1.5\n"
改之,charset=gb2312 Content-Transfer-Encoding: utf8如下:
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2007-01-18 09:55+China Standard Time\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME \n"
"Language-Team: LANGUAGE \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=gb2312\n"
"Content-Transfer-Encoding: utf8\n"
"Generated-By: pygettext.py 1.5\n"
那个msgid” ” 和msgstr” “不要动
好,翻译文件已经建立了,保存之,文件名改为lang.po.
2. 建立翻译文件路径,在主文件目录下建立中文翻译路径 ./locale/cn/LC_MESSAGES/(windows下例D:\ python-prj\src\locale\cn\LC_MESSAGES,其中src目录是源文件主目录),英文翻译路径 ./locale/en/LC_MESSAGES/(windows下例D:\ python-prj\src\locale\en\LC_MESSAGES,其中src目录是源文件主目录)
3. 在主文件中引入gettext模块:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import gettext
gettext.install('lang', './locale', unicode=False)
gettext.translation('lang', './locale', languages=['cn']).install(True)
第四行中lang是翻译文件主名, ./locale是存放翻译文件的路径,第三个参数是是否使用unicode,第五行languages参数指定要使用的语言存放的子目录,这里cn表示使用./locale/cn/LC_MESSAGES/路径下的翻译文件.
注意: # -*- coding: utf-8 -*- 一定要写在前两行,写第三行都不会生效
4. 处理要翻译的字符串,例:
print “Hello world!”
print “Python is a good Language.”
改之:
print _(“Hello world!”)
print _(“Python is a good Language.”)
5. 编写并生成翻译文件,打开步骤1中编辑过的lang.po文件添加翻译文字,形成如下内容:
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2007-01-18 09:55+China Standard Time\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME \n"
"Language-Team: LANGUAGE \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=gb2312\n"
"Content-Transfer-Encoding: utf8\n"
"Generated-By: pygettext.py 1.5\n"
msgid " Hello world!"
msgstr "世界你好!"
msgid " Python is a good Language."
msgstr "Python 是门好语言."
保存之,运行./Tools/i18n/目录下的msgfmt.py,Linux下命令为: python msgfmt.py lang.po(widows下的话,将lang.po文件拷贝到./Tools/i18n/目录下,运行命令行窗口,到改目录下键入:msgfmt.py lang.po),生成lang.mo,将该文件拷贝至./locale/cn/LC_MESSAGES/目录下,将步骤1生成的lang.po文件直接生成lang.mo文件拷贝到./locale/en/LC_MESSAGES/目录下,好了一切都准备好了,命令行下运行你的主文件,看看两个print命令是不是输出了世界你好! Python 是门好语言.,如果将主文件第五行languages参数置为en,则输出是Hello world! Python is a good Language.,因为en目录下lang.mo文件未做任何的翻译,默认字符串不被替换.
上述实现的方法用在界面编程上可以实现中英文界面的切换,需要作个配置文件选择中/英文;或者在主文件开始时先获取操作系统本地语言然后根据获取结果再设置中/英文,这样就可以实现软件语言自适应;如果想实现软件语言在线切换,对于python来说就比较困难了,需要在软件中加入界面刷新机制,不推荐,可以在切换中/英文设置后软件下一次启动生效(软件如Ulipad).