summaryrefslogtreecommitdiffstats
path: root/aurweb/l10n.py
diff options
context:
space:
mode:
authorFrédéric Mangano-Tarumi <fmang@mg0.fr>2020-07-20 16:25:28 +0200
committerLukas Fleischer <lfleischer@archlinux.org>2021-02-20 17:24:30 +0100
commit239988def7479cba6407901ee4671b6794d0b2ef (patch)
treedeea8ef041909ef7e0ebbb6bb5b34a8176ad1500 /aurweb/l10n.py
parente323156947a93ba65a99f927ed2d99c738c34f2b (diff)
downloadaur-239988def7479cba6407901ee4671b6794d0b2ef.tar.gz
aur-239988def7479cba6407901ee4671b6794d0b2ef.tar.xz
Build a translation facility for FastAPI
Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
Diffstat (limited to 'aurweb/l10n.py')
-rw-r--r--aurweb/l10n.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/aurweb/l10n.py b/aurweb/l10n.py
index 51b56abb..a476ecd8 100644
--- a/aurweb/l10n.py
+++ b/aurweb/l10n.py
@@ -16,3 +16,25 @@ class Translator:
self._localedir,
languages=[lang])
return self._translator[lang].gettext(s)
+
+
+def get_translator_for_request(request):
+ """
+ Determine the preferred language from a FastAPI request object and build a
+ translator function for it.
+
+ Example:
+ ```python
+ _ = 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()
+
+ def translate(message):
+ return translator.translate(message, lang)
+
+ return translate