3
0

support i18n

This commit is contained in:
2022-04-07 13:26:52 +02:00
parent 99db542326
commit 71e7fe7b09
4 changed files with 98 additions and 1 deletions

View File

@ -18,6 +18,26 @@ class DispatchingJinjaLoader(quart.templating.DispatchingJinjaLoader):
def __init__(self, app: MyQuartApp) -> None:
super().__init__(app)
def get_source(
self, environment: jinja2.Environment, template: str
) -> typing.Tuple[str, typing.Optional[str], typing.Optional[typing.Callable[[], bool]]]:
"""Returns the template source from the environment.
This considers the loaders on the :attr:`app` and blueprints.
"""
langs: typing.List[str] = quart.g.langs
for loader in self._loaders():
for lang in langs:
try:
return loader.get_source(environment, os.path.join('i18n', lang, template))
except jinja2.TemplateNotFound:
continue
try:
return loader.get_source(environment, template)
except jinja2.TemplateNotFound:
continue
raise jinja2.TemplateNotFound(template)
def _loaders(self) -> typing.Generator[jinja2.BaseLoader, None, None]:
if self.app.custom_loader:
yield self.app.custom_loader