-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathi18n.py
286 lines (225 loc) · 8.83 KB
/
i18n.py
1
2
3
4
5
6
7
8
9
10
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python3
# vim: ai ts=4 sts=4 et sw=4 nu
from __future__ import annotations
import pathlib
import re
import babel
import babel.core
import babel.support
import iso639
import iso639.exceptions
ISO_LEVELS = ["1", "2b", "2t", "3", "5"]
class NotFoundError(ValueError):
pass
class UnknownLocaleError(ValueError):
"""Exception raised when the locale to used in not known"""
pass
class Translator:
"""Translate messages to a given locale"""
def setlocale(
self, root_dir: pathlib.Path, locale_name: str, locale_subdir: str = "locale"
) -> str:
"""Load translation files for for a given locale. Call this early.
root_dir: path where a `locale_subdir` folder exist
locale_name: name of the locale to load
locale_subdir: subfolder of `root_dir` containing translations ("locale" by
default)
"""
locale_dir = root_dir / locale_subdir
try:
locale = babel.Locale.parse(locale_name)
except babel.core.UnknownLocaleError as exc:
raise UnknownLocaleError("Unknown locale") from exc
self.translations = babel.support.Translations.load(locale_dir, locale)
if locale.language != "en" and not (self.translations._catalog):
raise RuntimeError(
f"Failed to find language files for {locale_name} "
f"({locale.language}) in {locale_dir}"
)
if locale.territory:
return f"{locale.language}_{locale.territory}"
else:
return locale.language
def _(self, message: str) -> str:
"""Translate a message
Nota: setlocale must have been called prior to using this function
"""
if not hasattr(self, "translations"):
raise RuntimeError(
"Translation not initialized, you must call setlocale first"
)
return self.translations.gettext(message)
DEFAULT_TRANSLATOR = Translator()
def _(text: str) -> str:
"""Translate a message
Nota: setlocale must have been called prior to using this function
"""
return DEFAULT_TRANSLATOR._(text)
def setlocale(
root_dir: pathlib.Path, locale_name: str, locale_subdir: str = "locale"
) -> str:
"""Load translation files for for a given locale. Call this early.
root_dir: path where a `locale_subdir` folder exist
locale_name: name of the locale to load
locale_subdir: subfolder of `root_dir` containing translations ("locale" by default)
"""
return DEFAULT_TRANSLATOR.setlocale(
root_dir=root_dir, locale_name=locale_name, locale_subdir=locale_subdir
)
class Lang(dict):
@property
def iso_639_1(self) -> str | None:
"""ISO-639-1 language code"""
return self["iso-639-1"]
@property
def iso_639_2b(self) -> str | None:
"""ISO-639-2b language code"""
return self["iso-639-2b"]
@property
def iso_639_2t(self) -> str | None:
"""ISO-639-2t language code"""
return self["iso-639-2t"]
@property
def iso_639_3(self) -> str | None:
"""ISO-639-3 language code"""
return self["iso-639-3"]
@property
def iso_639_5(self) -> str | None:
"""ISO-639-5 language code"""
return self["iso-639-5"]
@property
def english(self) -> str:
"""language name in English"""
return self["english"]
@property
def native(self) -> str:
"""language name in native language"""
return self["native"]
@property
def iso_types(self) -> list[str]:
"""list of supported iso types"""
return self["iso_types"]
@property
def query(self) -> list[str]:
"""Query issued for these language details"""
return self["query"]
@property
def querytype(self) -> list[str]:
"""Type of query issued to retrieve language details"""
return self["querytype"]
def get_iso_lang_data(lang: str) -> tuple[Lang, Lang | None]:
"""ISO-639-x languages details for lang. Raises NotFoundError
Returns a tuple (main_language, macro_language | None)"""
iso_types = []
try:
isolang = iso639.Lang(lang)
except (
iso639.exceptions.InvalidLanguageValue,
iso639.exceptions.DeprecatedLanguageValue,
) as exc:
raise NotFoundError("Not a valid iso language name/code") from exc
def replace_types(new_type: str) -> str:
# convert new iso_types from iso639-lang Pypi package to old iso_types from
# iso-639 package, since we were returning these values for a long time
if new_type == "pt1":
return "part1"
elif new_type == "pt2b":
return "part2b"
elif new_type == "pt2t":
return "part2t"
elif new_type == "pt3":
return "part3"
elif new_type == "pt5":
return "part5"
return new_type
for code_type in [f"pt{lang_}" for lang_ in ISO_LEVELS] + ["name"]:
# the `if` condition below is a bit hackish but it is the only way to know
# if the passed value is matching a code type or not with new python-i639
# library and we do not expect weird things to happen here
if str(getattr(isolang, code_type)).lower() == lang.lower():
iso_types.append(replace_types(code_type))
lang_data = Lang(
**{f"iso-639-{lang_}": getattr(isolang, f"pt{lang_}") for lang_ in ISO_LEVELS}
)
lang_data.update({"english": isolang.name, "iso_types": iso_types})
if isolang.macro():
return (
lang_data,
get_iso_lang_data(isolang.macro().name)[0],
) # first item in the returned tuple
return lang_data, None
def find_language_names(query: str, lang_data: Lang | None = None) -> tuple[str, str]:
"""(native, english) language names for lang with help from lang_data
Falls back to English name if available or query if not"""
if lang_data is None:
lang_data = get_language_details(query, failsafe=True)
if not lang_data:
return query, query
try:
query_locale = babel.Locale.parse(query)
if native_display_name := query_locale.get_display_name():
if english_display_name := query_locale.get_display_name("en"):
return native_display_name, english_display_name
except (babel.UnknownLocaleError, TypeError, ValueError, AttributeError):
pass
# ISO code lookup order matters (most qualified first)!
for iso_level in [f"iso-639-{lang_}" for lang_ in reversed(ISO_LEVELS)]:
try:
query_locale = babel.Locale.parse(lang_data.get(iso_level))
if native_display_name := query_locale.get_display_name():
if english_display_name := query_locale.get_display_name("en"):
return native_display_name, english_display_name
except (babel.UnknownLocaleError, TypeError, ValueError, AttributeError):
pass
default = lang_data.get("english") or query
return default, default
def update_with_macro(lang_data: Lang, macro_data: Lang | None):
"""update empty keys from lang_data with ones of macro_data"""
if macro_data:
for key, value in macro_data.items():
if key in lang_data and not lang_data.get(key):
lang_data[key] = value
return lang_data
def get_language_details(
query: str, failsafe: bool | None = False # noqa: FBT002
) -> Lang | None:
"""language details dict from query.
When query fails, either raises NotFoundError or return None, based on failsafe
"""
if query.isalpha() and (2 <= len(query) <= 3): # noqa: PLR2004
# possibility of iso-639 code
adjusted_query = query
native_query = query
query_type = "purecode"
elif all(x.isalpha() or x == "-" or x == "_" for x in query) and (
query.count("_") + query.count("-") == 1
):
# possibility of locale
adjusted_query = re.split("-|_", query)[0]
native_query = query.replace("-", "_")
query_type = "locale"
else:
# possibility of iso language name
adjusted_query = query.title().replace("Languages", "languages")
native_query = query
query_type = "languagename"
try:
lang_data, macro_data = get_iso_lang_data(adjusted_query)
except NotFoundError as exc:
if failsafe:
return None
raise exc
iso_data = update_with_macro(lang_data, macro_data)
native_name, english_name = find_language_names(native_query, iso_data)
iso_data.update(
{
"english": english_name,
"native": native_name,
"querytype": query_type,
"query": query,
}
)
return iso_data
def is_valid_iso_639_3(code: str) -> bool:
"""whether code is a valid ISO-639-3 code"""
return (get_language_details(code, failsafe=True) or {}).get("iso-639-3") == code