如何本地化我的 WTForms 验证消息?

发布于 2024-12-20 16:53:38 字数 2097 浏览 3 评论 0原文

我几乎已经本地化了我的验证消息,您可以看到它适用于英语和瑞典语:

英语:

在此处输入图像描述

瑞典语:

在此处输入图像描述

但是当我切换到葡萄牙语时,我收到以下错误消息:

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)

我想我收到了此错误消息之前我不知道怎么办来处理它。请你帮助我好吗?为什么会出现这个错误信息?

我的表单类的代码是

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')

我的 .po 文件中的翻译部分是

msgid "Name is required"
msgstr "É necessário o nome"

我的 python 文件是这样开始的

#!/usr/bin/python
# -*- coding: utf-8 -*-

并且据我所知,我已经将所有内容设置为 unicode 和 utf-8。

谢谢你的帮助

I nearly got my validation message localized as you can see it works for English and Swedish:

English:

enter image description here

Swedish:

enter image description here

But when I switch to Portuguese I get the following error message:

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)

I think I've had this error message before and I don't know how to handle it. Could you please help me? Why does this error message appear?

The code for my form class is

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')

The translation part in my .po file is

msgid "Name is required"
msgstr "É necessário o nome"

My python file begins like this

#!/usr/bin/python
# -*- coding: utf-8 -*-

And AFAIK I've set everything I can to unicode and utf-8.

Thank you for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2024-12-27 16:53:38

如果您希望能够在翻译中使用 unicode 字符,则需要使用 ugettext_lazy 实用函数,而不是 gettext_lazy。
正如函数名称所暗示的那样,主要区别在于 ugettext_lazy 是 unicode,而 gettext_lazy 不是(这使得它没有那么有用)。

当您这样做时,您可以/应该尽可能使用 unicode 而不是默认字符串,也就是说,尽快将输入转换为 unicode,并尽可能晚地将输出编码为相关编码。

If you want to be able to use unicode characters in your translations, you need to use the ugettext_lazy utility function and not gettext_lazy.
The main difference, as the function name hints, is that ugettext_lazy is unicode when gettext_lazy is not (which makes it not that useful by the way).

While you're at it, you could / should use unicode instead of default strings whenever possible, that is, convert input to unicode ASAP, and encode output as late as possible to the relevant encoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文