2017-09-24 65 views
0

我试着看看关于在python项目上制作一些i18n机制的python文档。虽然我通常喜欢python文档,但这一部分对我而言并不直观,而且我看了另一个资源。通过观察这些:Python i18n与gettext不工作

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 

回答

0

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

我的代码有行:

from gettext import gettext as _ 

,并没有需要它。事实上,当我删除这条线时,一切都很顺利。

我不明白为什么,但是这个解决了它。我不知道这是否会工作,当我将有另一班国际字符串