Nik*_*ntz 3 python unicode localization gettext wtforms
我几乎得到了我的验证消息本地化,因为你可以看到它适用于英语和瑞典语:
英语:

瑞典:

但是当我切换到葡萄牙语时,我收到以下错误消息:
Traceback (most recent call last):
File "/media/Lexar/montao/lib/webapp2/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/media/Lexar/montao/montaoproject/main.py", line 1749, in post
current_user=self.current_user,
File "/media/Lexar/montao/montaoproject/main.py", line 466, in render_jinja
self.response.out.write(template.render(data))
File "/media/Lexar/montao/montaoproject/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "/media/Lexar/montao/montaoproject/templates/insert_jinja.html", line 249, in top-level template code
<ul class="errors">{% for error in form.name.errors %}<li>{{ error }}</li>{% endfor %}</ul>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我想我以前有这个错误信息,我不知道如何处理它.请你帮助我好吗?为什么会出现此错误消息?
我的表单类的代码是
class AdForm(Form):
my_choices = [('1', _('VEHICLES')), ('2', _('Cars')), ('3', _('Bicycles'))]
name = TextField(_('Name'), [validators.Length(min=4, max=50,
message=_(u'Name is required') )])
title = TextField(_('title'), [validators.Required()])
text = TextAreaField(_('Text'), widget=TextArea())
phonenumber = TextField(_('Phone number'))
phoneview = BooleanField(_('Display phone number on site'))
price = TextField(_('Price'))
password = PasswordField(_('Password'))
email = TextField(_('Email'))
category = SelectField(choices = my_choices, default = '1')
Run Code Online (Sandbox Code Playgroud)
我的.po文件中的翻译部分是
msgid "Name is required"
msgstr "É necessário o nome"
Run Code Online (Sandbox Code Playgroud)
我的python文件就是这样开始的
#!/usr/bin/python
# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)
而AFAIK我已经尽力设置unicode和utf-8.
感谢您的帮助
如果您希望能够在翻译中使用unicode字符,则需要使用ugettext_lazy实用程序函数而不是gettext_lazy.
正如函数名提示的那样,主要区别在于,当gettext_lazy不是时,ugettext_lazy是unicode(顺便说一下它没那么有用).
当你在它的时候,你可以/应该尽可能使用unicode而不是默认字符串,也就是说,将输入转换为unicode ASAP,并尽可能晚地将输出编码为相关的编码.
| 归档时间: |
|
| 查看次数: |
1563 次 |
| 最近记录: |