Skip to content

Conversation

cjmayo
Copy link
Contributor

@cjmayo cjmayo commented Jul 28, 2024

flask-babel's gettext() expects a Babel object to have been created.

Otherwise an exception is raised.

  File "/usr/lib/python3.12/site-packages/flask_babel/__init__.py", line 46, in get_babel
    return app.extensions['babel']
           ~~~~~~~~~~~~~~^^^^^^^^^
KeyError: 'babel'

Not completely convinced by this, but at least it illustrates the issue.

flask-babel's gettext() expects a Babel object to have been created.

Otherwise an exception is raised.

  File "/usr/lib/python3.12/site-packages/flask_babel/__init__.py", line 46, in get_babel
    return app.extensions['babel']
           ~~~~~~~~~~~~~~^^^^^^^^^
KeyError: 'babel'
@cjmayo cjmayo marked this pull request as draft July 28, 2024 17:45
@cjmayo
Copy link
Contributor Author

cjmayo commented Jul 28, 2024

Indeed, it breaks applications where flask-babel is used.

@samuelhwilliams
Copy link
Contributor

Yeah I've been chewing over this one for a little while in the back of my head. It's a bit gnarly because it mutates the entire flask_admin.babel module based on what's in place at the time of the first import.

I think it would be better to refactor this so that the state of the flask app, flask_admin instance, etc aren't relevant at module import time. But haven't yet dug into this enough to work out what that work look like and/or how far the repercussions would spread.

@ElLorans
Copy link
Contributor

ElLorans commented Jun 2, 2025

This would also fix #2609 . I would try to do what flask-security did https://github.com/pallets-eco/flask-security/blob/main/flask_security/babel.py by adding a has_babel_ext() function to prevent the KeyError.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants