【问题标题】:Error when using flask babel in AppEngine在 AppEngine 中使用烧瓶 babel 时出错
【发布时间】:2020-10-31 15:31:21
【问题描述】:

我正在做我的第一个 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

所以有人知道为什么我的应用无法识别翻译中的“_(”?

提前致谢!

【问题讨论】:

    标签: python flask jinja2 google-app-engine-python flask-babel


    【解决方案1】:

    你必须导入

    from flask_babel import lazy_gettext as _
    

    【讨论】:

    • 嗨,J.G.,你拯救了我的一天。谢谢!
    • 很高兴我能帮上忙!感谢您在提出问题时付出了这么多努力。
    猜你喜欢
    • 2018-05-22
    • 2014-11-25
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    相关资源
    最近更新 更多