feat: pass context instance when rendering templates

This commit is contained in:
Corentin 2024-05-31 18:22:00 +02:00
parent b028477371
commit 1660e6944c
1 changed files with 5 additions and 1 deletions

View File

@ -31,6 +31,9 @@ class Context:
def current_language(self) -> str | None:
return self.__current_language
def language(self) -> str | None:
return self.current_language
def add_filters(self, **filters: Any) -> None:
self.__environment.filters.update(**filters)
@ -45,6 +48,7 @@ class Context:
def load_translations(self, domain: str, locale_dir: str | Path, *languages: str) -> None:
locale_dir = str(locale_dir)
self.__environment.add_extension("jinja2.ext.i18n")
self.__environment.policies["ext.i18n.trimmed"] = True
self.__translations[_DEFAULT_LANGUAGE] = NullTranslations()
for language in languages:
self.__translations[language] = gettext.translation(
@ -114,7 +118,7 @@ class Context:
def __render(self, source: str, output_path: Path, **context: Any) -> None:
output_path.parent.mkdir(exist_ok=True, parents=True)
template = self.__environment.get_template(source)
content = template.render(**context)
content = template.render(context=self, **context)
with open(output_path, "w") as output_file:
output_file.write(content)