0

Trying to get flask-babel to work in a factory method. I've got it working in a simple flask app (without a factory) but as soon as I try using an app factory the "gettext" or "_" doesnt seem to translate the text.

I found this other very similar issue and as I dont see any errors I assume I've set it up correctly: Access a Flask extension that is defined in the app factory

Setup up a demo flask app (with factory) as per below:

# __init__.py


from flask import Flask
from flask_babel import Babel, gettext
from application.webapp import server_bp

babel = Babel()


def create_app(config_filename=None):
    app = Flask(__name__)
    babel.init_app(app, "de")

    app.register_blueprint(server_bp)

    return app
# webapp.py


from flask import Flask, Blueprint

from flask_babel import gettext

server_bp = Blueprint("main", __name__)

# app = Flask(__name__)
# babel = Babel()
# babel.init_app(app, "de")


@server_bp.route("/")
# @app.route("/")
def hello_world():
    return gettext("Title of Dash App")
# wsgi.py


from application import create_app

app = create_app()

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5004)

I follow the exact steps provided by the flask_babel docs here https://python-babel.github.io/flask-babel/ and translate the

and i tried to hardcode the lang (which worked in the non-factory) in this line in the init.py file

# __init__.py

...
babel.init_app(app, "de")
...

and the

But for some reason I don't understand the translation does not work at all when using an app factory method. Any help or guidance would be sincerely appreciated :)

Tom

0 Answers0