【问题标题】:Python i18n with gettext not working带有gettext的Python i18n不起作用
【发布时间】: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 文件后,它现在不起作用或显示任何错误。

ma​​in.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


    【解决方案1】:

    我发现了问题和一个临时但不令人满意的解决方案。

    我的代码有一行:

    from gettext import gettext as _
    

    这不是必需的。事实上,当我删除这行时,一切都很顺利。

    我不太明白为什么,但这解决了它。我不知道当我将有另一个带有 i18n 字符串的课程时这是否会起作用

    【讨论】:

    猜你喜欢
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多