Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Flask WTU kw中使用Flask Babel gettext?_Python_Flask_Flask Wtforms_Python Babel_Flask Babel - Fatal编程技术网

Python 如何在Flask WTU kw中使用Flask Babel gettext?

Python 如何在Flask WTU kw中使用Flask Babel gettext?,python,flask,flask-wtforms,python-babel,flask-babel,Python,Flask,Flask Wtforms,Python Babel,Flask Babel,我开始用Babel来制作表格和烧瓶。下面我尝试获取用户名和密码字段的占位符关键字: 这是我的法语.po文件(当然已经编译): 项目的法语(法语)翻译。 #版权所有(C)2017组织 #此文件以与项目相同的许可证分发。 #第一作者,2017年。 # msgid“” msgstr“” “项目Id版本:项目版本\n” “将Msgid错误报告给:EMAIL@ADDRESS\n“ “罐创建日期:2017-11-20 12:08+0100\n” “采购订单修订日期:2017-11-20 12:10+0100

我开始用Babel来制作表格和烧瓶。下面我尝试获取用户名和密码字段的占位符关键字:

这是我的法语.po文件(当然已经编译):

项目的法语(法语)翻译。 #版权所有(C)2017组织 #此文件以与项目相同的许可证分发。 #第一作者,2017年。 # msgid“” msgstr“” “项目Id版本:项目版本\n” “将Msgid错误报告给:EMAIL@ADDRESS\n“ “罐创建日期:2017-11-20 12:08+0100\n” “采购订单修订日期:2017-11-20 12:10+0100\n” “语言:fr\u fr\n” “语言团队:fr\u fr\n” “复数形式:nplurals=2;复数=(n>1);\n” “MIME版本:1.0\n” “内容类型:文本/普通;字符集=utf-8\n” “内容传输编码:8位\n” “生成者:Babel 2.5.1\n” “最后一个转换器:\n” “X-Generator:Poedit 2.0.4\n” #:app/forms.py:13 msgid“用户名” msgstr“使用名称” #:app/forms.py:16 msgid“密码” msgstr“通行证” #:app/templates/login.html:34 msgid“登录” msgstr“Se连接器” 不幸的是,即使我强制使用法语,这两个字段仍然使用英语。我在我的Jinja2模板中进行了登录翻译


我写错什么了吗?

我认为你应该使用lazy\u gettext()方法

我认为你应该使用lazy\u gettext()方法

它奏效了!非常感谢。对于我个人的理解:为什么我不能使用
gettext()
?原因很简单,因为表单类是在请求之外定义的。裁判-成功了!非常感谢。对于我个人的理解:为什么我不能使用
gettext()
?原因很简单,因为表单类是在请求之外定义的。参考号-
#!/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')})
# 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"