diff options
Diffstat (limited to 'src/jwebmail/__init__.py')
-rw-r--r-- | src/jwebmail/__init__.py | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/src/jwebmail/__init__.py b/src/jwebmail/__init__.py index ba0b6af..f3ba58e 100644 --- a/src/jwebmail/__init__.py +++ b/src/jwebmail/__init__.py @@ -3,7 +3,8 @@ import pwd import sys from os import environ -from flask import Flask, g +from babel import parse_locale +from flask import Flask, abort, g from flask_babel import Babel, get_locale from flask_login import LoginManager, login_required from jinja2 import ChainableUndefined @@ -27,9 +28,11 @@ from .webmail import ( if sys.version_info >= (3, 11): from tomllib import load as toml_load + toml_read_file = dict(load=toml_load, text=False) else: from toml import load as toml_load + toml_read_file = dict(load=toml_load, text=True) __version__ = "2.0.0.dev2" @@ -50,8 +53,6 @@ def validate_config(app): conf["JWEBMAIL"]["READ_MAILS"].setdefault("AUTHENTICATOR", "qmail-authuser") ) - conf["JWEBMAIL"].setdefault("DEFAULT_LANGUAGE", "de") - def create_app(): app = Flask(__name__) @@ -67,9 +68,7 @@ def create_app(): validate_config(app) - DEFAULT_LANGUAGE = app.config["JWEBMAIL"]["DEFAULT_LANGUAGE"] - - Babel(app, locale_selector=lambda: g.get("lang_code", DEFAULT_LANGUAGE)) + Babel(app, locale_selector=lambda: g.get("lang_code")) app.cli.add_command(compile_css_command) @@ -94,13 +93,19 @@ def create_app(): if "lang_code" in values: return if app.url_map.is_endpoint_expecting(endpoint, "lang_code"): - values["lang_code"] = g.get("lang_code", DEFAULT_LANGUAGE) + values["lang_code"] = g.get("lang_code", app.config["BABEL_DEFAULT_LOCALE"]) @app.url_value_preprocessor def pull_lang_code(endpoint, values): - g.lang_code = ( - values.pop("lang_code", DEFAULT_LANGUAGE) if values else DEFAULT_LANGUAGE - ) + if values and values.get("lang_code"): + try: + pl = parse_locale(values["lang_code"]) + if pl[0] in ["en", "de"]: + g.lang_code = values.pop("lang_code") + else: + abort(404) + except ValueError: + abort(404) return app |