l10n.py 2.52 KB
Newer Older
Lukas Fleischer's avatar
Lukas Fleischer committed
1
import gettext
2
3
4
5
6
7
import typing

from collections import OrderedDict

from fastapi import Request
from jinja2 import contextfilter
Lukas Fleischer's avatar
Lukas Fleischer committed
8

9
10
import aurweb.config

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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": "正體中文"
})

Lukas Fleischer's avatar
Lukas Fleischer committed
44
45
46

class Translator:
    def __init__(self):
47
        self._localedir = aurweb.config.get('options', 'localedir')
Lukas Fleischer's avatar
Lukas Fleischer committed
48
49
        self._translator = {}

50
    def get_translator(self, lang: str):
Lukas Fleischer's avatar
Lukas Fleischer committed
51
        if lang not in self._translator:
52
            self._translator[lang] = gettext.translation("aurweb",
53
                                                         self._localedir,
54
55
56
57
58
59
60
61
62
63
                                                         languages=[lang],
                                                         fallback=True)
        return self._translator.get(lang)

    def translate(self, s: str, lang: str):
        return self.get_translator(lang).gettext(s)


# Global translator object.
translator = Translator()
64
65


66
67
68
69
70
71
72
73
74
75
76
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):
77
78
79
80
81
82
83
84
85
86
    """
    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"))
    ```
    """
87
    lang = get_request_language(request)
88
89
90
91
92

    def translate(message):
        return translator.translate(message, lang)

    return translate
93
94
95
96
97
98
99


@contextfilter
def tr(context: typing.Any, value: str):
    """ A translation filter; example: {{ "Hello" | tr("de") }}. """
    _ = get_translator_for_request(context.get("request"))
    return _(value)