我试着查看python关于在python项目上创建一些i18n机制的内容。虽然我通常喜欢python文档,但这一节在我看来并不直观,我看了另一种资源。通过查看这些:
inventwithpython.com/blog/translate-your-python-3-program-with-the-gettext-module/
我成功地完成了下面粘贴的源代码。最初它给出了一个域错误,但是在添加了.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发布于 2017-09-24 21:23:32
我发现了这个问题,并找到了一个暂时但不令人满意的解决办法。
我的代码是这样的:
from gettext import gettext as _也不需要它。事实上,当我删除这一行的时候,一切都很完美。
我不太明白为什么,但这解决了问题。我不知道当我有另一个带有i18n字符串的类时,这是否有效
https://stackoverflow.com/questions/46394865
复制相似问题