From 1b82bbdfc0eeaa5b654de96c5bb5a42d248efc79 Mon Sep 17 00:00:00 2001 From: Chris Mayo Date: Sun, 28 Jul 2024 18:39:03 +0100 Subject: [PATCH] Fix KeyError exception when flask-babel is installed but not used 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' --- flask_admin/babel.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/flask_admin/babel.py b/flask_admin/babel.py index 762ccb2ee..6195c3151 100644 --- a/flask_admin/babel.py +++ b/flask_admin/babel.py @@ -1,7 +1,14 @@ +from flask import current_app, has_app_context + try: from flask_babel import Domain -except ImportError: + if has_app_context(): + current_app().extensions['babel'] + else: + raise KeyError + +except (KeyError, ImportError): def gettext(string, **variables): return string % variables