python-babel / flask-babel

i18n and l10n support for Flask based on Babel and pytz
https://python-babel.github.io/flask-babel/
Other
432 stars 159 forks source link

Is BABEL_TRANSLATION_DIRECTORIES not working? #209

Closed kingkong-cmd closed 1 year ago

kingkong-cmd commented 1 year ago

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)
kingkong-cmd commented 1 year ago

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

TkTech commented 1 year ago

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

kingkong-cmd commented 1 year ago

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 :)