|
1
|
|
|
import gettext as _gettext |
|
2
|
|
|
import sys |
|
3
|
|
|
import threading |
|
4
|
|
|
|
|
5
|
|
|
if sys.version_info >= (3, 9): |
|
6
|
|
|
from collections.abc import Callable |
|
7
|
|
|
else: |
|
8
|
|
|
from typing import Callable |
|
9
|
|
|
|
|
10
|
|
|
from pathlib import Path |
|
11
|
|
|
from typing import Union |
|
12
|
|
|
|
|
13
|
|
|
from lazy_string import LazyString |
|
14
|
|
|
|
|
15
|
|
|
from .core import get_language |
|
16
|
|
|
from .helpers import to_locale |
|
17
|
|
|
|
|
18
|
|
|
from ._utils import export |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
StringOrPath = Union[str, Path] |
|
22
|
|
|
MaybeLazyInteger = Union[int, Callable[..., int]] |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
@export |
|
26
|
|
|
class NotThreadSafeTranslations: |
|
27
|
|
|
|
|
28
|
|
|
def __init__(self, domain: str, locale_dir_path: StringOrPath): |
|
29
|
|
|
self._domain = domain |
|
30
|
|
|
self._locale_dir_path = str(locale_dir_path) |
|
31
|
|
|
self._translations = { |
|
32
|
|
|
None: _gettext.NullTranslations(), |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
def gettext(self, message: str) -> str: |
|
36
|
|
|
return self._get_translation().gettext(message) |
|
37
|
|
|
|
|
38
|
|
|
def gettext_lazy(self, message: str) -> LazyString: |
|
39
|
|
|
return LazyString( |
|
40
|
|
|
func=self.gettext, |
|
41
|
|
|
message=message, |
|
42
|
|
|
) |
|
43
|
|
|
|
|
44
|
|
|
def ngettext(self, singular: str, plural: str, n: MaybeLazyInteger) -> str: |
|
45
|
|
|
if callable(n): |
|
46
|
|
|
n = n() |
|
47
|
|
|
return self._get_translation().ngettext(singular, plural, n) |
|
48
|
|
|
|
|
49
|
|
|
def ngettext_lazy(self, singular: str, plural: str, n: MaybeLazyInteger) -> LazyString: |
|
50
|
|
|
return LazyString( |
|
51
|
|
|
func=self.ngettext, |
|
52
|
|
|
singular=singular, |
|
53
|
|
|
plural=plural, |
|
54
|
|
|
n=n, |
|
55
|
|
|
) |
|
56
|
|
|
|
|
57
|
|
|
def pgettext(self, context: str, message: str) -> str: |
|
58
|
|
|
return self._get_translation().pgettext(context, message) |
|
59
|
|
|
|
|
60
|
|
|
def pgettext_lazy(self, context: str, message: str) -> LazyString: |
|
61
|
|
|
return LazyString( |
|
62
|
|
|
func=self.pgettext, |
|
63
|
|
|
context=context, |
|
64
|
|
|
message=message, |
|
65
|
|
|
) |
|
66
|
|
|
|
|
67
|
|
|
def npgettext(self, context: str, singular: str, plural: str, n: MaybeLazyInteger) -> str: |
|
68
|
|
|
if callable(n): |
|
69
|
|
|
n = n() |
|
70
|
|
|
return self._get_translation().npgettext(context, singular, plural, n) |
|
71
|
|
|
|
|
72
|
|
|
def npgettext_lazy(self, context: str, singular: str, plural: str, n: MaybeLazyInteger) -> LazyString: |
|
73
|
|
|
return LazyString( |
|
74
|
|
|
func=self.npgettext, |
|
75
|
|
|
context=context, |
|
76
|
|
|
singular=singular, |
|
77
|
|
|
plural=plural, |
|
78
|
|
|
n=n, |
|
79
|
|
|
) |
|
80
|
|
|
|
|
81
|
|
|
def _get_translation(self) -> _gettext.NullTranslations: |
|
82
|
|
|
language = get_language() |
|
83
|
|
|
|
|
84
|
|
|
translation = self._translations.get(language) |
|
85
|
|
|
if not translation: |
|
86
|
|
|
locale = to_locale(language) |
|
87
|
|
|
translation = _gettext.translation( |
|
88
|
|
|
domain=self._domain, |
|
89
|
|
|
localedir=self._locale_dir_path, |
|
90
|
|
|
languages=[locale, ], |
|
91
|
|
|
fallback=True, |
|
92
|
|
|
) |
|
93
|
|
|
self._translations[language] = translation |
|
94
|
|
|
|
|
95
|
|
|
return translation |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
@export |
|
99
|
|
|
class Translations(NotThreadSafeTranslations): |
|
100
|
|
|
|
|
101
|
|
|
def __init__(self, domain: str, locale_dir_path: StringOrPath): |
|
102
|
|
|
super().__init__(domain=domain, locale_dir_path=locale_dir_path) |
|
103
|
|
|
self._lock = threading.RLock() |
|
104
|
|
|
|
|
105
|
|
|
def gettext(self, message: str) -> str: |
|
106
|
|
|
with self._lock: |
|
107
|
|
|
return super().gettext(message=message) |
|
108
|
|
|
|
|
109
|
|
|
def ngettext(self, singular: str, plural: str, n: MaybeLazyInteger) -> str: |
|
110
|
|
|
with self._lock: |
|
111
|
|
|
return super().ngettext(singular=singular, plural=plural, n=n) |
|
112
|
|
|
|
|
113
|
|
|
def pgettext(self, context: str, message: str) -> str: |
|
114
|
|
|
with self._lock: |
|
115
|
|
|
return super().pgettext(context=context, message=message) |
|
116
|
|
|
|
|
117
|
|
|
def npgettext(self, context: str, singular: str, plural: str, n: MaybeLazyInteger) -> str: |
|
118
|
|
|
with self._lock: |
|
119
|
|
|
return super().npgettext(context=context, singular=singular, plural=plural, n=n) |
|
120
|
|
|
|