jinja2: add 'tn' filter, a numerical translation

The possibly plural version of `tr`, `tn` provides a way to translate
strings into singular or plural form based on a given integer
being 1 or not 1.

Example use:

```
{{ 1 | tn("%d package found.", "%d packages found.") | format(1) }}
```

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-08-20 16:29:04 -07:00
parent 1c26ce52a5
commit 45fbf214b4
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
3 changed files with 32 additions and 1 deletions

View file

@ -36,3 +36,15 @@ def test_get_translator_for_request():
translate = l10n.get_translator_for_request(request)
assert translate("Home") == "Startseite"
def test_tn_filter():
request = Request()
request.cookies["AURLANG"] = "en"
context = {"language": "en", "request": request}
translated = l10n.tn(context, 1, "%d package found.", "%d packages found.")
assert translated == "%d package found."
translated = l10n.tn(context, 2, "%d package found.", "%d packages found.")
assert translated == "%d packages found."