From 9b01180ec981d6d0ffd7b0e84a3ddbb0a2cf1ecc Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 31 Oct 2022 22:19:18 +0100 Subject: [builder][server][proxy] complete internationalization and add polish translation --- src/hydrilla/proxy/policies/base.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'src/hydrilla/proxy/policies/base.py') diff --git a/src/hydrilla/proxy/policies/base.py b/src/hydrilla/proxy/policies/base.py index 95021cd..967e2c4 100644 --- a/src/hydrilla/proxy/policies/base.py +++ b/src/hydrilla/proxy/policies/base.py @@ -43,7 +43,7 @@ import jinja2 from immutables import Map -from ...translations import translation as make_translation +from ... import translations from ... import url_patterns from ... import common_jinja_templates from .. import state @@ -62,14 +62,9 @@ _jinja_info_env = jinja2.Environment( lstrip_blocks = True, extensions = ['jinja2.ext.i18n', 'jinja2.ext.do'] ) -_jinja_info_env.install_gettext_translations(make_translation()) # type: ignore _jinja_info_env.globals['url_patterns'] = url_patterns _jinja_info_lock = threading.Lock() -def get_info_template(template_file_name: str) -> jinja2.Template: - with _jinja_info_lock: - return _jinja_info_env.get_template(template_file_name) - _jinja_script_loader = jinja2.PackageLoader( __package__, @@ -164,6 +159,20 @@ class Policy(ABC): return (self.current_popup_settings.popup_enabled and http_messages.is_likely_a_page(request_info, response_info)) + def _get_info_template(self, template_file_name: str) -> jinja2.Template: + with _jinja_info_lock: + chosen_locale = self.haketilo_settings.locale + if chosen_locale not in translations.supported_locales: + chosen_locale = None + + if chosen_locale is None: + chosen_locale = translations.default_locale + + trans = translations.translation(chosen_locale) + _jinja_info_env.install_gettext_translations(trans) # type: ignore + return _jinja_info_env.get_template(template_file_name) + + def _csp_to_clear(self, http_info: http_messages.FullHTTPInfo) \ -> t.Union[t.Sequence[str], t.Literal['all']]: return () @@ -215,7 +224,9 @@ class Policy(ABC): popup_page = self.make_info_page(http_info) if popup_page is None: - template = get_info_template('special_page_info.html.jinja') + template = self._get_info_template( + 'special_page_info.html.jinja' + ) popup_page = template.render( url = http_info.request_info.url.orig_url ) -- cgit v1.2.3