如何在 Flask-WTForms render_kw 中使用 Flask-Babel gettext?
How to use Flask-Babel gettext in Flask-WTForms render_kw?
我开始将 Babel 与 WTForms 和 Flask 一起使用。下面我尝试为我的 Username 和 Password 字段获取占位符关键字:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask_babel import gettext
from flask_wtf import FlaskForm
from wtforms import PasswordField, StringField
from wtforms.validators import DataRequired
class LoginForm(FlaskForm):
username = StringField(label='username',
validators=[DataRequired()],
render_kw={"placeholder": gettext('Username')})
password = PasswordField(label='password',
validators=[DataRequired()],
render_kw={"placeholder": gettext('Password')})
这是我的法语 .po 文件(当然已经编译):
# French (France) translations for PROJECT.
# Copyright (C) 2017 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2017.
#
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2017-11-20 12:08+0100\n"
"PO-Revision-Date: 2017-11-20 12:10+0100\n"
"Language: fr_FR\n"
"Language-Team: fr_FR <LL@li.org>\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.5.1\n"
"Last-Translator: \n"
"X-Generator: Poedit 2.0.4\n"
#: app/forms.py:13
msgid "Username"
msgstr "Nom d’utilisateur"
#: app/forms.py:16
msgid "Password"
msgstr "Mot de passe"
#: app/templates/login.html:34
msgid "Login"
msgstr "Se connecter"
不幸的是,即使我强制将语言环境设置为法语,这两个字段仍保留为英语。我让它在我的 Jinja2 模板中用于 Login 翻译。
我是不是写错了什么?
我认为你应该使用 lazy_gettext() 方法
我开始将 Babel 与 WTForms 和 Flask 一起使用。下面我尝试为我的 Username 和 Password 字段获取占位符关键字:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask_babel import gettext
from flask_wtf import FlaskForm
from wtforms import PasswordField, StringField
from wtforms.validators import DataRequired
class LoginForm(FlaskForm):
username = StringField(label='username',
validators=[DataRequired()],
render_kw={"placeholder": gettext('Username')})
password = PasswordField(label='password',
validators=[DataRequired()],
render_kw={"placeholder": gettext('Password')})
这是我的法语 .po 文件(当然已经编译):
# French (France) translations for PROJECT.
# Copyright (C) 2017 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2017.
#
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2017-11-20 12:08+0100\n"
"PO-Revision-Date: 2017-11-20 12:10+0100\n"
"Language: fr_FR\n"
"Language-Team: fr_FR <LL@li.org>\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.5.1\n"
"Last-Translator: \n"
"X-Generator: Poedit 2.0.4\n"
#: app/forms.py:13
msgid "Username"
msgstr "Nom d’utilisateur"
#: app/forms.py:16
msgid "Password"
msgstr "Mot de passe"
#: app/templates/login.html:34
msgid "Login"
msgstr "Se connecter"
不幸的是,即使我强制将语言环境设置为法语,这两个字段仍保留为英语。我让它在我的 Jinja2 模板中用于 Login 翻译。
我是不是写错了什么?
我认为你应该使用 lazy_gettext() 方法