0

我开始将 Babel 与 WTForms 和 Flask 一起使用。下面我尝试为我的用户名密码字段获取占位符关键字:

#!/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 模板中的登录翻译。

我是不是写错了什么?

4

1 回答 1

2

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

于 2017-11-20T14:03:10.357 回答