调用Django i18n makemessages命令

时间:2016-11-21 16:21:36

标签: python django utf-8 translation

我正在使用Django的国际化功能为webapp生成翻译字符串。

我尝试拨打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"

2 个答案:

答案 0 :(得分:3)

请注意,这是与评论中提到的this similar question不同的异常位置。

在我看来,这种情况发生的唯一方法就是如果你的django安装有一个修改,或者python 2.7版本中有一个错误。

你的筹码是:

> msgs, errors, status = gettext_popen_wrapper(args)
> stdout = stdout.decode(stdout_encoding)

gettext_popen_wrapper(在 django 1.8 上,这是我认为你正在使用的,你可以确认吗?)和popen_wrapper创建stdout(之后)删除注释/文档字符串并重新定义以获得清晰度,请参阅github上的popen_wrappergettext_popen_wrapper以获取纯粹的代码:

def popen_wrapper(args, os_err_exc_type=CommandError, universal_newlines=True):
    try:
        p = Popen(args, shell=False, stdout=PIPE, stderr=PIPE,
                close_fds=os.name != 'nt', universal_newlines=universal_newlines)
    except OSError as e:
        strerror = force_text(e.strerror, DEFAULT_LOCALE_ENCODING,
                              strings_only=True)
        six.reraise(os_err_exc_type, os_err_exc_type('Error executing %s: %s' %
                    (args[0], strerror)), sys.exc_info()[2])
    # NB: subprocess.Popen.communicate() should return two bytes 
    # (i.e. str in python 2) objects
    output, errors = p.communicate()
    return (
        output,
        force_text(errors, DEFAULT_LOCALE_ENCODING, strings_only=True),
        p.returncode
    )

def gettext_popen_wrapper(args, 
                          os_err_exc_type=CommandError, 
                          stdout_encoding="utf-8"):
    manual_io_wrapper = six.PY3 and stdout_encoding != DEFAULT_LOCALE_ENCODING

    stdout, stderr, status_code = popen_wrapper(
        args, os_err_exc_type=os_err_exc_type,
        universal_newlines=not manual_io_wrapper)

    if manual_io_wrapper:
        stdout = io.TextIOWrapper(io.BytesIO(stdout), encoding=stdout_encoding).read()
    if six.PY2:
        # EXCEPTION HIT ON THE FOLLOWING LINE
        stdout = stdout.decode(stdout_encoding)
    return stdout, stderr, status_code

因此,当我们调用stdout时,str应该是一个普通的stdout.decode()对象(即需要解码的一堆字节)。但是,如果是这种情况,为什么 en 编码中的例外?我们只需要编码对象是否已经是unicode对象,即它是unicode类型。果然,如果我们添加行

stdout = stdout.decode('utf-8')

之前

stdout = stdout.decode(stdout_encoding)

然后现在decode方法首先使用default encoding of ascii尝试encode unicode stdout,这会导致您看到的异常。我将manual_io_wrapper设置为True也会出现同样的错误,导致stdout = io.TextWrapper(...)行也发生(也会产生unicode),但这不应该是{{} 1}}因为你在python 2而不是3。

所以我认为:

  • 您的Truedjango安装不正确,或者已经过编辑。请尝试重新安装它们。
  • 您在six中遇到了一个错误,由于某种原因,它返回的是subprocess.Popen.communicate()而不是unicode(我相信如果universal_newlines are turned on可以使用python 3。您可以通过重新安装python或升级到更高版本来获得里程。

我的主要观点是,我不认为这是一个环境问题。知道任何后续行动会很有趣:

  • 你在哪个平台
  • 你正在使用的是什么python 2.7
  • 你正在使用什么django。

答案 1 :(得分:0)

在下一行中,stdout不是字节str,而是unicode,并且在unicode的隐式编码期间您会遇到异常。

stdout = stdout.decode('utf-8')

这是因为decode()应该在字节str上执行,而当我们尝试在decode上调用unicode时,在python 2.7中,那里将在encode之前使用unicode隐式调用decode,此encode调用将使用默认charset ascii在python中。

unicode.encode() --> byte   # results in str
byte.decode() --> unicode   # results in unicode
unicode.decode() --> unicode.encode().decode()  # implicit encode call

因此,开始调查导致标准输出为unicode的内容。

感谢。