使用Babel进行烧瓶WTF本地化

时间:2015-07-27 08:35:07

标签: flask flask-wtforms babel

我已经使用了Flask Babel并对我的项目进行了本地化,但是我对WTF表单有问题,当字段为空时我无法翻译显示的字段。有什么帮助吗?

这是有效的:

return jsonify({'error': gettext('Incorrect Data')}), 406

但是在处理Form类时,Babel不提取字段。像这样:

class LoginForm(Form):
    username = TextField(gettext(u'Username'), validators=[validators.Required()])
    password = PasswordField('Password', validators=[validators.Required()])

我已尝试使用/不使用'u'选项

2 个答案:

答案 0 :(得分:0)

验证器>>> KRB_AP_REQ = '\x01\x00' >>> KRB_AP_REP = '\x02\x00' >>> KRB_ERROR = '\x03\x00' >>> class Example(univ.Sequence): ... componentType = namedtype.NamedTypes( ... namedtype.NamedType('spam', univ.Integer()), ... namedtype.NamedType('eggs', univ.Any()), ... namedtype.NamedType('ham', univ.Any()), ... ) ... >>> example = Example() >>> example['spam'] = 42 >>> example['eggs'] = KRB_AP_REQ # obtain DER serialization for ANY type that follows >>> example['ham'] = encoder.encode(univ.Integer(24)) >>> print(example.prettyPrint()) Example: spam=42 eggs=0x0100 ham=0x020118 >>> substrate = encoder.encode(example) >>> data, tail = decoder.decode(substrate, asn1Spec=Example()) >>> print(data.prettyPrint()) Example: spam=42 eggs=0x0100 ham=0x020118 >>> data['eggs'].asOctets() '\x01\x00' >>> data['eggs'].asNumbers() (1, 0) >>> example['eggs'] == KRB_AP_REQ True 的消息是通过Required设置的,也可以通过babel进行翻译。有关详细信息,请参阅WTForms documentation

Required(message=error_message)

答案 1 :(得分:0)

尝试lazy_gettext('')

class LoginForm(Form):
    username = TextField(lazy_gettext(u'Username'), validators=[validators.Required()])

HTML 中:

{{form.username.label (class="form-control-label") }}