在 AppEngine 中使用 flask babel 时出错
Error when using flask babel in AppEngine
我正在处理我的第一个 Flask Babel 项目并遇到了障碍:
NameError: name '_' is not defined
我需要将文本和菜单翻译成不同的语言,稍后我会处理数字和日期。 pybabel extract 和 init 命令运行良好,没有错误。
这是我的文件:
main.py
import datetime
from flask import Flask, render_template
from flask import session, redirect, url_for, escape, request
from flask_babel import Babel, gettext
from google.cloud import datastore
datastore_client = datastore.Client()
app = Flask(__name__)
app.config.from_pyfile('config.py')
babel = Babel(app)
@babel.localeselector
def get_locale():
# return request.accept_languages.best_match(app.config['LANGUAGES'].keys()
# In the app we'll ask the user what he prefers.
return 'es' # Let's force Spanish for testing purposes
message = _("This site is for development purposes only. Please contact us for more
information.")
footer = _("Test Text #1")
username = "test-user"
@app.route('/')
def root():
return render_template('main.html', username=user, message=message)
config.py
# add to your app.config or config.py file
LANGUAGES = {
'en': 'English',
'es': 'Español'
}
messages.pot
的输出
# Translations template for PROJECT.
# Copyright (C) 2020 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2020.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2020-07-07 23:09+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.6.0\n"
#: /home/xxx/AppEngine/fpp-dev-01/main.py:25
msgid ""
"This site is for development purposes only. Please contact us for more "
"information."
msgstr ""
#: /home/xxx/AppEngine/fpp-dev-01/main.py:26
msgid "Test Text #1"
msgstr ""
#: /home/xxx/AppEngine/fpp-dev-01/templates/fppbase.html:46
msgid "Settings"
msgstr ""
messages.po(位置:/home/xxx/Appengine/fpp-dev-01/translations/es/LC_MESSAGES)
#: /home/xxx/AppEngine/fpp-dev-01/main.py:25
msgid ""
"This site is for development purposes only. Please contact us for more "
"information."
msgstr "Este sitio es solamente para fines de desarrollo. Por favor contáctenos para
más información"
#: /home/xxx/AppEngine/fpp-dev-01/main.py:26
msgid "Test Text #1"
msgstr ""
#: /home/xxx/AppEngine/fpp-dev-01/templates/fppbase.html:46
msgid "Settings"
msgstr "Ajustes"
我在本地 运行 应用程序 (Linux) 使用以下命令:
python main.py
应用程序在终端中的输出:
Traceback (most recent call last):
File "main.py", line 20, in <module>
message = _("This site is for development purposes only. Please contact us for
more information.")
NameError: name '_' is not defined
有人知道为什么我的应用无法识别翻译的“_(”吗?
提前致谢!
你必须导入
from flask_babel import lazy_gettext as _
我正在处理我的第一个 Flask Babel 项目并遇到了障碍:
NameError: name '_' is not defined
我需要将文本和菜单翻译成不同的语言,稍后我会处理数字和日期。 pybabel extract 和 init 命令运行良好,没有错误。
这是我的文件:
main.py
import datetime
from flask import Flask, render_template
from flask import session, redirect, url_for, escape, request
from flask_babel import Babel, gettext
from google.cloud import datastore
datastore_client = datastore.Client()
app = Flask(__name__)
app.config.from_pyfile('config.py')
babel = Babel(app)
@babel.localeselector
def get_locale():
# return request.accept_languages.best_match(app.config['LANGUAGES'].keys()
# In the app we'll ask the user what he prefers.
return 'es' # Let's force Spanish for testing purposes
message = _("This site is for development purposes only. Please contact us for more
information.")
footer = _("Test Text #1")
username = "test-user"
@app.route('/')
def root():
return render_template('main.html', username=user, message=message)
config.py
# add to your app.config or config.py file
LANGUAGES = {
'en': 'English',
'es': 'Español'
}
messages.pot
的输出# Translations template for PROJECT.
# Copyright (C) 2020 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2020.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2020-07-07 23:09+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.6.0\n"
#: /home/xxx/AppEngine/fpp-dev-01/main.py:25
msgid ""
"This site is for development purposes only. Please contact us for more "
"information."
msgstr ""
#: /home/xxx/AppEngine/fpp-dev-01/main.py:26
msgid "Test Text #1"
msgstr ""
#: /home/xxx/AppEngine/fpp-dev-01/templates/fppbase.html:46
msgid "Settings"
msgstr ""
messages.po(位置:/home/xxx/Appengine/fpp-dev-01/translations/es/LC_MESSAGES)
#: /home/xxx/AppEngine/fpp-dev-01/main.py:25
msgid ""
"This site is for development purposes only. Please contact us for more "
"information."
msgstr "Este sitio es solamente para fines de desarrollo. Por favor contáctenos para
más información"
#: /home/xxx/AppEngine/fpp-dev-01/main.py:26
msgid "Test Text #1"
msgstr ""
#: /home/xxx/AppEngine/fpp-dev-01/templates/fppbase.html:46
msgid "Settings"
msgstr "Ajustes"
我在本地 运行 应用程序 (Linux) 使用以下命令:
python main.py
应用程序在终端中的输出:
Traceback (most recent call last):
File "main.py", line 20, in <module>
message = _("This site is for development purposes only. Please contact us for
more information.")
NameError: name '_' is not defined
有人知道为什么我的应用无法识别翻译的“_(”吗?
提前致谢!
你必须导入
from flask_babel import lazy_gettext as _