Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python gettext UnicodeDecodeError

Tags:

python

gettext

I use python gettext module. It is fine when I use language=en_US, but it raises an error when I use another language (e.g:vi_VN). Have I done something wrong?

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

The locales folder in my project:

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

Error

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)
like image 866
hungneox Avatar asked Nov 11 '22 07:11

hungneox


1 Answers

Add in file

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 <[email protected]>\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"
like image 99
user11614811 Avatar answered Nov 15 '22 06:11

user11614811