python-babel/flask-babel

Is BABEL_TRANSLATION_DIRECTORIES not working?

kingkong-cmd opened this issue · 3 comments

I'm setting a custom BABEL_TRANSLATION_DIRECTORIES variable in my config.py file which is used again when I initialize the app. But it is not used as expected, and babel defaults to using the default translations folder: 'translations'. Is this feature not working, or am I using it wrong?

config.py:

BABEL_TRANSLATION_DIRECTORIES = 'translations;app_2'
init.py:

from flask_babel import Babel, lazy_gettext as _l
from config import Config
babel = Babel()

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)
    babel.init_app(app)

For all those struggling with this in the future, I figured it out.

When the path is given with semicolon separator, there is something wrong with how it is parsed in the init function of babel. So the solution is to represent the path using double backslashes:

BABEL_TRANSLATION_DIRECTORIES = 'translations\\app_2

Edit:

Double slashes worked on windows, but gave me trouble when pushing to Heroku. Single forward slash seems to work both places.

BABEL_TRANSLATION_DIRECTORIES = 'translations/app_2

The semicolon is not a path separator, it's a separator for multiple paths. Each individual path still needs to be valid.

The semicolon is not a path separator, it's a separator for multiple paths. Each individual path still needs to be valid.

Thank you, understood. Wish someone told me yesterday :)