Python gettext无法加载转换文件

时间:2013-12-06 11:51:01

标签: python gettext

问题:

在python gettext中加载已编译的翻译文件时会抛出异常

 Traceback (most recent call last):
  File "xxxx.py", line 6, in <module>
    lang_en = gettext.translation(__appname__,LanguageDirectory,languages=['en'])
  File "C:\Python33\lib\gettext.py", line 410, in translation
    t = _translations.setdefault(key, class_(fp))
  File "C:\Python33\lib\gettext.py", line 160, in __init__
    self._parse(fp)
  File "C:\Python33\lib\gettext.py", line 265, in _parse
    item = b_item.decode().strip()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position xxx: invalid continuation byte

1 个答案:

答案 0 :(得分:2)

<强>解决方案

gettext.py库目前不解析翻译文件标题中的iso-8859字符。

带有该问题的示例标题(在编译之前):

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2013-12-06 11:22\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE+Mitteleuropäische Zeit\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: ENCODING\n"
"Generated-By: pygettext.py 1.5\n"

在此示例中,Mitteleuropäische Zeit会导致问题。

如何修复
目前避免这种情况的最简单方法是不在标题内使用iso-8859字符 要防止pygettext.py将您当前语言的时区添加到标题中,您可以更改

第447行 - pygettext.py

timestamp = time.strftime('%Y-%m-%d %H:%M-%Z')

timestamp = time.strftime('%Y-%m-%d %H:%M', time.gmtime())

通过此修改,pygettext不会将时区添加到标题中。相反,它将使用当前的Greenwich Mean Time