2017-09-24 63 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 _ 

,並沒有需要它。事實上,當我刪除這條線時,一切都很順利。

我不明白爲什麼,但是這個解決了它。我不知道這是否會工作,當我將有另一班國際字符串