diff options
Diffstat (limited to 'src/hydrilla/proxy/policies/base.py')
-rw-r--r-- | src/hydrilla/proxy/policies/base.py | 25 |
1 files changed, 18 insertions, 7 deletions
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 ) |