这片文章是参考别人的。源地址不已经丢失。
感觉python的本地化有点麻烦。
操作步骤是
1:编写代码
2:xgettext生成pot文件。这里包含对应的msgid与msgstr
3:msgfmt格式化pot文件,生成mo文件
因为在mac下面。需要安装gettext模块
brew install gettext
项目目录
locale
|--zn_CN
|--LC_MESSAGES
main.py
1:编写代码
import gettext
localepath = '%s/locale' % os.path.join(os.path.dirname(__file__))
t=gettext.translation('messages',localedir=localepath,languages=['zh_CN'])
_=t.ugettext
print _('This message is in the script.')
2:生成pot文件
/usr/local/Cellar/gettext/0.18.3.1/bin/xgettext -o locales/zh_CN/LC_MESSAGES/messages.po main.py
3:格式化(这里没有写上生成路径,生成完以后拷贝到对应的LC_MESSAGES目录下)
/usr/local/Cellar/gettext/0.18.3.1/bin/msgfmt locale/zn_CN/LC_MESSAGES/messages.po