【发布时间】:2017-09-24 21:08:17
【问题描述】:
我尝试查看有关在 python 项目中创建一些 i18n 机制的 python 文档。虽然我通常喜欢 python 文档,但这部分对我来说并不直观,我查看了其他资源。通过查看这些:
inventwithpython.com/blog/translate-your-python-3-program-with-the-gettext-module/
Python docs: localizing-your-application
我设法完成了下面粘贴的源代码。首先它给出了域错误,但在添加 .mo 文件后,它现在不起作用或显示任何错误。
main.py
from gettext import translation
from gettext import gettext as _
lang1 = translation('poker', '/home/myUser/myProjects/pokerProject/resources/localedir', languages=['en'])
lang2 = translation('poker', '/home/myUser/myProjects/pokerProject/resources/localedir', languages=['es'])
lang2.install()
# Code with strings in this way
print(_('This should be translated'))
.mo 文件示例
"Generated-By: pygettext.py 1.5\n"
"X-Generator: Poedit 2.0.1\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: main.py:26
msgid "This should be translated"
msgstr "Translated text"
布局(pokerProject 子树)
.
├── martintc
│ ├── __init__.py
│ └── poker
│ ├── __init__.py
│ └── model
│ ├── diceset.py
│ ├── die.py
│ ├── errors.py
│ ├── __init__.py
│ ├── main.py
│ ├── poker.pot
│ └── utils.py
└── resources
└── localedir
├── en
│ └── LC_MESSAGES
│ ├── poker.mo
│ └── poker.po
└── es
└── LC_MESSAGES
├── poker.mo
└── poker.po
【问题讨论】:
标签: python python-3.x internationalization gettext