我正在使用Django的國際化功能爲webapp生成翻譯字符串。調用Django i18n makemessages命令時出現UnicodeError錯誤
一個問題是情況下產生我嘗試打電話makemessages
,和現有的語言.po
文件包含特殊字符(如$
,£
等)。
如果其中一個存在,makemessages會嘗試加載現有的.po
文件並對其進行解碼。這樣做時,我得到一個錯誤:
Traceback (most recent call last):
File "manage.py", line 18, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemessages.py", line 325, in handle
self.write_po_file(potfile, locale)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemessages.py", line 458, in write_po_file
msgs, errors, status = gettext_popen_wrapper(args)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemessages.py", line 51, in gettext_popen_wrapper
stdout = stdout.decode(stdout_encoding)
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa2' in position 2105: ordinal not in range(128)
我試圖挖回來經過這裏的追蹤,但我在茫然,發生了什麼。
它好像Django的嘗試將現有.po
文件解碼爲UTF8
,但隨後重新編碼它的時候,它的使用ASCII
編解碼器。
任何有關錯誤的見解都將受到大力讚賞。
編輯:
- 操作系統:Ubuntu的15.10和OS X 10.11.6
- 的Python:2.7.10和2.7.11
- Django的:1.8.14
- 六:1.10.0
我試過重新安裝按照建議,Django/Six,但錯誤仍然存在。
Ubuntu的localedef --list-archive
:
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
內容類型有問題的翻譯文件:
"Content-Type: text/plain; charset=UTF-8\n"
http://stackoverflow.com/questions/22106777/unicode-issue-with-makemessages-all-django-1-6-2-python-3-3 – trinchet
試試這個:'LC_CTYPE = en_US.UTF-8 python manage.py makemessages';它工作嗎? –
@ nobe4:當你用'LC_CTYPE = en_US.UTF-8 python manage.py makemessages'運行它時它有什麼區別嗎?而且,順便說一下,你的操作系統和版本是什麼? –