2

I'm working on my first Flask Babel project and hit a snag:

NameError: name '_' is not defined

I need to translate the texts and menus into different languages, later I will tackle numbers and dates. The pybabel extract and init commands work well and give no errors.

Here are my files:

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'
}

Output of 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 <[email protected]>\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 (location: /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"

I run the app locally (Linux) with the following command:

python main.py

Output of the app in the terminal:

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

So anybody has a hint why my app is not recognizing the '_(' for the translations?

Thanks in advance!

1 Answer 1

1

You have to import

from flask_babel import lazy_gettext as _
Sign up to request clarification or add additional context in comments.

2 Comments

Hi J.G., you saved my day. Thanks!
Glad I could help! And thanks for putting so much effort in creating your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.