加载俄语翻译时出现Django Unicode错误

时间:2013-03-20 19:06:44

标签: django unicode internationalization

我正在尝试在Django网站上安装俄语翻译。

我做了什么:在'locale'文件夹中,除了我创建'ru'文件夹的其他语言之外,我创建了'LC_MESSAGES'文件夹,这就是我在'django.po'文件中放置翻译的地方。我手动创建了.po文件,因为翻译是作为Excel文件提供给我的。在那个.po文件中,我在同一个项目中复制了我在另一个.po文件中找到的标题,然后用脚本创建了'msgid'/'msgstr'对从Excel文件中提取这些信息。我检查了文件,手动修改后出现了一些错误,看起来是正确的,在进行编译时,我没有收到任何错误,并且创建了.mo文件。 然后我在设置文件中添加了'ru'作为语言。然后我重新加载了主页,在我们用来切换语言的小部件中出现了俄语。但是当我点击它时,它重新加载页面,我得到Unicode错误。

某些背景信息:该网站是英文网站,但已安装西班牙语翻译并且可以正常使用非ascii字符。

任何想法都是受欢迎的,即使不是完整的解决方案,也只是一种调试它的方式。

错误是:'ascii'编解码器无法解码位置0中的字节0xd0:序数不在范围内(128)。你传入了()

从我发现它是一个ugettext懒惰的问题,但我不明白为什么它适用于其他翻译......

我从Django获得Unicode提示:无法编码/解码的字符串是: 此字符串根据我尝试加载的页面而更改。

这是追溯:

Traceback:
File "/home/env/project/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  136.                     response.render()
File "/home/env/project/lib/python2.7/site-packages/django/template/response.py" in render
  96.             self._set_content(self.rendered_content)
File "/home/env/project/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  75.         content = template.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render
  123.             return self._render(context)
File "/home/env/project/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  57.     return self.nodelist.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  127.         return compiled_parent._render(context)
File "/home/env/project/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  57.     return self.nodelist.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  127.         return compiled_parent._render(context)
File "/home/env/project/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  57.     return self.nodelist.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  64.             result = block.nodelist.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/home/env/project/lib/python2.7/site-packages/django/templatetags/i18n.py" in render
  76.         return _render_value_in_context(output, context)
File "/home/env/project/lib/python2.7/site-packages/django/template/base.py" in _render_value_in_context
  777.     value = force_unicode(value)
File "/home/env/project/lib/python2.7/site-packages/django/utils/encoding.py" in force_unicode
  93.             raise DjangoUnicodeDecodeError(s, *e.args)

Exception Type: DjangoUnicodeDecodeError at /
Exception Value: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128). You passed in <django.utils.functional.__proxy__ object at 0x9f3f16c> (<class 'django.utils.functional.__proxy__'>)

编辑:

我试图从头开始。我删除了我手动创建的语言环境文件夹和.po文件以尝试使用'makemessages'命令,并且在执行该命令期间我收到此错误:

processing language ru
Traceback (most recent call last):
  File "./manage.py", line 15, in <module>
    execute_manager(settings)
  File "/home/env/project/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/home/env/project/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/env/project/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/env/project/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/home/env/project/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle
    return self.handle_noargs(**options)
  File "/home/env/project/lib/python2.7/site-packages/django/core/management/commands/makemessages.py", line 365, in handle_noargs
    make_messages(locale, domain, verbosity, process_all, extensions, symlinks, ignore_patterns, no_wrap, no_obsolete)
  File "/home/env/project/lib/python2.7/site-packages/django/core/management/commands/makemessages.py", line 233, in make_messages
    f.write(templatize(src, orig_file[2:]))
  File "/home/env/project/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 127, in templatize
    return _trans.templatize(src, origin)
  File "/home/env/project/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 450, in templatize
    content = u''.join(comment)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128)

显然,其他地方肯定会出现问题。我会继续寻找。

编辑:

显然,'makemessages'工具无法处理模板中的非ascii字符,无论是否标记为已翻译。我发现了很多它们,我不知道如何处理它们。一旦我找到它,我会在这里报告。

1 个答案:

答案 0 :(得分:0)

我已更新到Django 1.4.5,问题完全消失了。 .po文件生成正确,编译也顺利。