这片文章是参考别人的。源地址不已经丢失。
感觉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.py3:格式化(这里没有写上生成路径,生成完以后拷贝到对应的LC_MESSAGES目录下)
/usr/local/Cellar/gettext/0.18.3.1/bin/msgfmt locale/zn_CN/LC_MESSAGES/messages.po

本文探讨了使用Python进行本地化的过程,包括编写代码、生成pot文件和格式化过程。重点介绍了mac环境下安装gettext模块的方法及目录结构设置,并通过实例展示了如何在Python脚本中实现本地化。
1859

被折叠的 条评论
为什么被折叠?



