mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
aurweb: Globalize a Translator instance, add more utility
+ Added SUPPORTED_LANGUAGES, a global constant dictionary of language => display pairs for languages we support. + Add Translator.get_translator, a function used to retrieve a translator after initializing it (if needed). Use `fallback=True` while creating languages, in case we setup a language that we don't have a translation for, it will noop the translation. This is particularly useful for "en," since we do not translate it, but doing this will allow us to go through our normal translation flow in any case. + Added typing. + Added get_request_language, a function that grabs the language for a request session, defaulting to aurweb.config [options] default_lang. + Added get_raw_translator_for_request, a function that retrieves the concrete translation object for a given language. + Added tr, a jinja2 contextfilter that can be used to inline translate strings in jinja2 templates. + Added `python-jinja` dep to .gitlab-ci.yml. This needs to be included in documentation before this set is merged in. + Introduce pytest units (test_l10n.py) in `test` along with __init__.py, which marks `test` as a test package. + Additionally, fix up notify.py to use the global translator. Also reduce its source width to <= 80 by newlining some code. + Additionally, prepare locale in .gitlab-ci.yml and add aurweb.config [options] localedir to config.dev with YOUR_AUR_ROOT like others. Signed-off-by: Kevin Morris <kevr@0cost.org> Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
This commit is contained in:
parent
21140e28a8
commit
c1e29e90ca
6 changed files with 240 additions and 123 deletions
|
@ -1,24 +1,79 @@
|
|||
import gettext
|
||||
import typing
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
from fastapi import Request
|
||||
from jinja2 import contextfilter
|
||||
|
||||
import aurweb.config
|
||||
|
||||
SUPPORTED_LANGUAGES = OrderedDict({
|
||||
"ar": "العربية",
|
||||
"ast": "Asturianu",
|
||||
"ca": "Català",
|
||||
"cs": "Český",
|
||||
"da": "Dansk",
|
||||
"de": "Deutsch",
|
||||
"el": "Ελληνικά",
|
||||
"en": "English",
|
||||
"es": "Español",
|
||||
"es_419": "Español (Latinoamérica)",
|
||||
"fi": "Suomi",
|
||||
"fr": "Français",
|
||||
"he": "עברית",
|
||||
"hr": "Hrvatski",
|
||||
"hu": "Magyar",
|
||||
"it": "Italiano",
|
||||
"ja": "日本語",
|
||||
"nb": "Norsk",
|
||||
"nl": "Nederlands",
|
||||
"pl": "Polski",
|
||||
"pt_BR": "Português (Brasil)",
|
||||
"pt_PT": "Português (Portugal)",
|
||||
"ro": "Română",
|
||||
"ru": "Русский",
|
||||
"sk": "Slovenčina",
|
||||
"sr": "Srpski",
|
||||
"tr": "Türkçe",
|
||||
"uk": "Українська",
|
||||
"zh_CN": "简体中文",
|
||||
"zh_TW": "正體中文"
|
||||
})
|
||||
|
||||
|
||||
class Translator:
|
||||
def __init__(self):
|
||||
self._localedir = aurweb.config.get('options', 'localedir')
|
||||
self._translator = {}
|
||||
|
||||
def translate(self, s, lang):
|
||||
if lang == 'en':
|
||||
return s
|
||||
def get_translator(self, lang: str):
|
||||
if lang not in self._translator:
|
||||
self._translator[lang] = gettext.translation("aurweb",
|
||||
self._localedir,
|
||||
languages=[lang])
|
||||
return self._translator[lang].gettext(s)
|
||||
languages=[lang],
|
||||
fallback=True)
|
||||
return self._translator.get(lang)
|
||||
|
||||
def translate(self, s: str, lang: str):
|
||||
return self.get_translator(lang).gettext(s)
|
||||
|
||||
|
||||
def get_translator_for_request(request):
|
||||
# Global translator object.
|
||||
translator = Translator()
|
||||
|
||||
|
||||
def get_request_language(request: Request):
|
||||
return request.cookies.get("AURLANG",
|
||||
aurweb.config.get("options", "default_lang"))
|
||||
|
||||
|
||||
def get_raw_translator_for_request(request: Request):
|
||||
lang = get_request_language(request)
|
||||
return translator.get_translator(lang)
|
||||
|
||||
|
||||
def get_translator_for_request(request: Request):
|
||||
"""
|
||||
Determine the preferred language from a FastAPI request object and build a
|
||||
translator function for it.
|
||||
|
@ -29,12 +84,16 @@ def get_translator_for_request(request):
|
|||
print(_("Hello"))
|
||||
```
|
||||
"""
|
||||
lang = request.cookies.get("AURLANG")
|
||||
if lang is None:
|
||||
lang = aurweb.config.get("options", "default_lang")
|
||||
translator = Translator()
|
||||
lang = get_request_language(request)
|
||||
|
||||
def translate(message):
|
||||
return translator.translate(message, lang)
|
||||
|
||||
return translate
|
||||
|
||||
|
||||
@contextfilter
|
||||
def tr(context: typing.Any, value: str):
|
||||
""" A translation filter; example: {{ "Hello" | tr("de") }}. """
|
||||
_ = get_translator_for_request(context.get("request"))
|
||||
return _(value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue