Flask babel not working on simple translation?
For me the solution was to set the BABEL_TRANSLATION_DIRECTORIES variable. It contains a string with paths separated by ';'.
In my config (only one location):
BABEL_TRANSLATION_DIRECTORIES = '/home/work/python/project/translations'
My answer is the same as fmelan's, but I use absolute path:
from flask import Flaskimport osbase_dir = os.path.abspath(os.path.dirname(__file__))app = Flask(__name__)app.config["BABEL_TRANSLATION_DIRECTORIES"] = os.path.join(base_dir, "app/translation")
Expanding from fmelan's answer: if your translations
directory is on the same place as your flask_app.py
, then you can just do:
app.config['BABEL_TRANSLATION_DIRECTORIES'] = './translations'
...and stop worrying on whether the absolute path works wherever the app is running.
For me this nicely solved a quite weird behaviour when deploying the page to PythonAnywhere.