aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/web_ui/root.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/hydrilla/proxy/web_ui/root.py')
-rw-r--r--src/hydrilla/proxy/web_ui/root.py40
1 files changed, 35 insertions, 5 deletions
diff --git a/src/hydrilla/proxy/web_ui/root.py b/src/hydrilla/proxy/web_ui/root.py
index 4915e51..6f9e349 100644
--- a/src/hydrilla/proxy/web_ui/root.py
+++ b/src/hydrilla/proxy/web_ui/root.py
@@ -60,6 +60,30 @@ from . import prompts
from . import _app
+def choose_locale() -> None:
+ app = t.cast(WebUIAppImpl, flask.current_app)
+
+ user_chosen_locale = get_settings().locale
+ if user_chosen_locale not in translations.supported_locales:
+ user_chosen_locale = None
+
+ if user_chosen_locale is None:
+ best_locale_match = flask.request.accept_languages.best_match(
+ translations.supported_locales,
+ default = translations.default_locale
+ )
+ if best_locale_match is None:
+ app._haketilo_request_locale = translations.default_locale
+ else:
+ app._haketilo_request_locale = best_locale_match
+ else:
+ app._haketilo_request_locale = user_chosen_locale
+
+ trans = translations.translation(app._haketilo_request_locale)
+
+ app.jinja_env.install_gettext_translations(trans)
+
+
def authenticate_by_referrer() -> t.Optional[werkzeug.Response]:
if flask.request.method == 'GET':
return None
@@ -90,6 +114,8 @@ class WebUIAppImpl(_app.WebUIApp):
_haketilo_blueprints: t.ClassVar[t.Sequence[flask.Blueprint]]
_haketilo_ui_domain: t.ClassVar[_app.UIDomain]
+ _haketilo_request_locale: str
+
def __init__(self):
super().__init__(__name__)
@@ -122,6 +148,7 @@ class WebUIAppImpl(_app.WebUIApp):
self.jinja_env.globals['doc_base_filename'] = 'doc_base.html.jinja'
self.before_request(authenticate_by_referrer)
+ self.before_request(choose_locale)
for bp in self._haketilo_blueprints:
self.register_blueprint(bp)
@@ -150,7 +177,11 @@ def home_post() -> werkzeug.Response:
state = _app.get_haketilo_state()
- if action == 'use_enabled':
+ if action == 'set_lang':
+ new_locale = flask.request.form['locale']
+ assert new_locale in translations.supported_locales
+ state.update_settings(locale=new_locale)
+ elif action == 'use_enabled':
state.update_settings(mapping_use_mode=st.MappingUseMode.WHEN_ENABLED)
elif action == 'use_auto':
state.update_settings(mapping_use_mode=st.MappingUseMode.AUTO)
@@ -188,7 +219,9 @@ def home_doc(page: str) -> str:
if page not in self_doc.page_names:
flask.abort(404)
- locale = translations.select_best_locale(self_doc.available_locales)
+ locale = t.cast(WebUIAppImpl, flask.current_app)._haketilo_request_locale
+ if locale not in self_doc.available_locales:
+ locale = translations.default_locale
return flask.render_template(
f'{locale}/{page}.html.jinja',
@@ -241,9 +274,6 @@ def process_request(
with app._haketilo_app_lock:
app._haketilo_state = state
- best_translations = translations.translation()
- app.jinja_env.install_gettext_translations(best_translations)
-
flask_response = app.test_client().open(
path = path,
base_url = request_info.url.url_without_path,