From 93bac8ec45ceb6bd3f3c4c58e98311afd3ea78ec Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Mon, 29 Mar 2021 15:17:17 -0700 Subject: 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 Signed-off-by: Lukas Fleischer --- test/test_l10n.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 test/test_l10n.py (limited to 'test/test_l10n.py') diff --git a/test/test_l10n.py b/test/test_l10n.py new file mode 100644 index 00000000..1a1ef3e6 --- /dev/null +++ b/test/test_l10n.py @@ -0,0 +1,44 @@ +""" Test our l10n module. """ +from aurweb import l10n + + +class FakeRequest: + """ A fake Request doppleganger; use this to change request.cookies + easily and with no side-effects. """ + + def __init__(self, *args, **kwargs): + self.cookies = kwargs.pop("cookies", dict()) + + +def test_translator(): + """ Test creating l10n translation tools. """ + de_home = l10n.translator.translate("Home", "de") + assert de_home == "Startseite" + + +def test_get_request_language(): + """ First, tests default_lang, then tests a modified AURLANG cookie. """ + request = FakeRequest() + assert l10n.get_request_language(request) == "en" + + request.cookies["AURLANG"] = "de" + assert l10n.get_request_language(request) == "de" + + +def test_get_raw_translator_for_request(): + """ Make sure that get_raw_translator_for_request is giving us + the translator we expect. """ + request = FakeRequest(cookies={"AURLANG": "de"}) + + translator = l10n.get_raw_translator_for_request(request) + assert translator.gettext("Home") == \ + l10n.translator.translate("Home", "de") + + +def test_get_translator_for_request(): + """ Make sure that get_translator_for_request is giving us back + our expected translation function. """ + request = FakeRequest(cookies={"AURLANG": "de"}) + + translate = l10n.get_translator_for_request(request) + assert translate("Home") == "Startseite" -- cgit v1.2.3-24-g4f1b