Question

Problem:

When loading a compiled translation file in python gettext it throws the exception

 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
Was it helpful?

Solution

Solution

The gettext.py library currently doesn't parse iso-8859 characters in the header of the translation file.

Example Header with that problem (before compiled):

# 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"

In this example Mitteleuropäische Zeit will cause the problem.

How to fix
Currently the easiest way to avoid this is not to use iso-8859 characters inside the header.
To prevent pygettext.py adding the timezone in your current language to the header you can change

Line 447 - pygettext.py

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

to

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

With this modification pygettext wont add the timezone to the header. Instead it will use the current Greenwich Mean Time

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top