【问题标题】:How to use Flask-Babel gettext in Flask-WTForms render_kw?如何在 Flask-WTForms render_kw 中使用 Flask-Babel gettext?
【发布时间】:2017-11-20 11:23:26
【问题描述】:

我开始使用带有 WTForms 和 Flask 的 Babel。下面我尝试为我的 UsernamePassword 字段获取占位符关键字:

#!/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 翻译。

我写错了吗?

【问题讨论】:

    标签: python flask flask-wtforms python-babel flask-babel


    【解决方案1】:

    我认为你应该使用lazy_gettext() 方法

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 2015-04-22
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多