diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-10-31 22:19:18 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-10-31 22:19:18 +0100 |
commit | 9b01180ec981d6d0ffd7b0e84a3ddbb0a2cf1ecc (patch) | |
tree | 66ec7089fca89fcaa4f769cc74cf12e783968ea9 /src/hydrilla/proxy/web_ui | |
parent | f1638c63de7c27c2fdccb55f4b540c8fb79d3762 (diff) | |
download | haketilo-hydrilla-9b01180ec981d6d0ffd7b0e84a3ddbb0a2cf1ecc.tar.gz haketilo-hydrilla-9b01180ec981d6d0ffd7b0e84a3ddbb0a2cf1ecc.zip |
[builder][server][proxy] complete internationalization and add polish translation
Diffstat (limited to 'src/hydrilla/proxy/web_ui')
-rw-r--r-- | src/hydrilla/proxy/web_ui/root.py | 40 | ||||
-rw-r--r-- | src/hydrilla/proxy/web_ui/templates/index.html.jinja | 19 |
2 files changed, 54 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, diff --git a/src/hydrilla/proxy/web_ui/templates/index.html.jinja b/src/hydrilla/proxy/web_ui/templates/index.html.jinja index 1e498a3..fe9c5d5 100644 --- a/src/hydrilla/proxy/web_ui/templates/index.html.jinja +++ b/src/hydrilla/proxy/web_ui/templates/index.html.jinja @@ -73,6 +73,25 @@ code in a proprietary work, I am not going to enforce this in court. {{ _('web_ui.home.heading.options') }} </h4> + {{ label(_('web_ui.home.choose_language_label')) }} + + {% call unordered_list() %} + {% + for lang_name, lang_code in [ + ('english', 'en_US'), + ('polski', 'pl_PL') + ] + %} + {% call list_entry() %} + <form method="POST" class="inline"> + <input type="hidden" name="action" value="set_lang"> + <input type="hidden" name="locale" value="{{ lang_code }}"> + <button>{{ lang_name }}</button> + </form> + {% endcall %} + {% endfor %} + {% endcall %} + {% call label(_('web_ui.home.mapping_usage_mode_label')) %} {{ hkt_doc_link('packages') }} {% endcall %} |