Python gettext UnicodeDecodeError

时间:2014-05-28 20:00:35

标签: python gettext

我使用python gettext模块。我使用language = en_US时很好,但是当我使用另一种语言时会引发错误(例如:vi_VN)。我做错了什么?

trans = gettext.translation(domain, os.path.join(os.getcwd(), 'locales'), [language])

我项目中的locales文件夹:

locales/
├── en_US
│   └── LC_MESSAGES
│       └── i38.po
└── vi_VN
    └── LC_MESSAGES
        ├── i38.mo
        └── i38.po

错误

Traceback (most recent call last):
  File "root.py", line 25, in <module>
    trans = gettext.translation(domain, os.path.join(os.getcwd(), 'locales'), [language])
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/gettext.py", line 410, in translation
    t = _translations.setdefault(key, class_(fp))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/gettext.py", line 160, in __init__
    self._parse(fp)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/gettext.py", line 300, in _parse
    catalog[str(msg, charset)] = str(tmsg, charset)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 5: ordinal not in range(128)

2 个答案:

答案 0 :(得分:0)

添加文件

msgid ""
msgstr ""
"Project-Id-Version: django\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-01-19 16:49+0100\n"
"PO-Revision-Date: 2017-09-23 18:54+0000\n"
"Last-Translator: Primož Verdnik <primoz.verdnik@gmail.com>\n"
"Language-Team: Slovenian (http://www.transifex.com/django/django/language/"
"sl/)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: sl\n"
"Plural-Forms: nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n"
"%100==4 ? 2 : 3);\n"

答案 1 :(得分:0)

确保使用 makemessages 命令创建 .po 文件