-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathutils.py
400 lines (335 loc) · 12.3 KB
/
utils.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# Standard library
import csv
import os
import re
from contextlib import contextmanager
# Third-party
import dateutil.parser
import polib
from babel import Locale
from babel.core import UnknownLocaleError
from django.conf import settings
from django.conf.locale import LANG_INFO
from django.utils import translation
# First-party/Local
from i18n import (
DEFAULT_JURISDICTION_LANGUAGES,
JURISDICTION_NAMES,
LANGMAP_DJANGO_TO_TRANSIFEX,
LANGMAP_LEGACY_TO_DJANGO,
)
CACHED_APPLICABLE_LANGS = {}
CACHED_WELL_TRANSLATED_LANGS = {}
# def get_locale_dir(locale_name):
# localedir = settings.LOCALE_PATHS[0]
# return os.path.join(localedir, locale_name, "LC_MESSAGES")
#
#
# def locales_with_directories():
# """
# Return list of locale names under our locale dir.
# """
# dir = settings.LOCALE_PATHS[0]
# return [
# item for item in os.listdir(dir)
# if os.path.isdir(os.path.join(dir, item))
# ]
LANGUAGE_JURISDICTION_MAPPING = {}
JURISDICTION_CURRENCY_LOOKUP = {
"jp": "jp",
"at": "eu",
"be": "eu",
"cy": "eu",
"ee": "eu",
"fi": "eu",
"fr": "eu",
"de": "eu",
"gr": "eu",
"ie": "eu",
"it": "eu",
"lu": "eu",
"mt": "eu",
"nl": "eu",
"pt": "eu",
"sk": "eu",
"si": "eu",
"es": "eu",
}
def get_translation_object(
domain: str, language_code: str, language_default: str
) -> translation.trans_real.DjangoTranslation:
"""
Return a DjangoTranslation object suitable to activate when we're wanting
to render templates for this language code and domain. (The domain is
typically specific to one or a few licenses that have common translations.)
This fuction requires the legal code locales path to have been added to
Django settings.LOCALE_PATHS
WARNING: this *does* make assumptions about the internals of Django's
translation system that could change on us. It doesn't seem likely,
though.
"""
# Start with a translation object for the domain for this tool.
tool_translation_object = translation.trans_real.DjangoTranslation(
language=language_code,
domain=domain,
localedirs=settings.LEGAL_CODE_LOCALE_PATH,
)
# Add a fallback to the standard Django translation for this language. This
# gets us the non-legal-code parts of the pages.
if language_code in settings.LANGUAGES_MOSTLY_TRANSLATED:
tool_translation_object.add_fallback(
translation.trans_real.translation(language_code)
)
elif language_default in settings.LANGUAGES_MOSTLY_TRANSLATED:
tool_translation_object.add_fallback(
translation.trans_real.translation(language_default)
)
else:
tool_translation_object.add_fallback(
translation.trans_real.translation(settings.LANGUAGE_CODE)
)
return tool_translation_object
@contextmanager
def active_translation(
translation_object: translation.trans_real.DjangoTranslation,
):
"""
Context manager to do stuff within its context with a particular
translation object set as the active translation. (Use
``legal_code.get_translation_object()`` to get a translation object to use
with this.)
Bypasses all the language code stuff that Django does when you use its
``activate(language_code)`` function.
The translation object should be a
django.utils.translation.trans_real.DjangoTranslation object or a subclass.
WARNING: this *does* make assumptions about the internals of Django's
translation system that could change on us. It doesn't seem likely,
though.
"""
previous_language = translation.trans_real.get_language()
translation.trans_real._active.value = translation_object
yield
# The following logic is based on django.utils.translations.override()
if previous_language is not None:
translation.activate(previous_language)
else: # pragma: no cover
translation.deactivate_all()
def save_pofile_as_pofile_and_mofile(pofile: polib.POFile, pofile_path: str):
"""Returns pofile_abspath, mofile_abspath"""
pofile.save(pofile_path)
mofilepath = re.sub(r"\.po$", ".mo", pofile_path)
pofile.save_as_mofile(mofilepath)
return (pofile_path, mofilepath)
def save_content_as_pofile_and_mofile(path: str, content: bytes):
"""Returns pofile_abspath, mofile_abspath"""
pofile = polib.pofile(pofile=content.decode(), encoding="utf-8")
return save_pofile_as_pofile_and_mofile(pofile, path)
def get_pofile_content(pofile: polib.POFile) -> str: # pragma: no cover
"""
Return the content of the pofile object - a string that contains what would
be in the po file on the disk if we saved it.
This isn't really worth its own function, except that mocking __unicode__
for tests is a pain, and it's easier to have this function so we can just
mock it.
"""
return pofile.__unicode__()
def get_pofile_path(
locale_or_legalcode: str,
language_code: str,
translation_domain: str,
data_dir=None,
):
if data_dir is None:
data_dir = settings.DATA_REPOSITORY_DIR
pofile_path = os.path.abspath(
os.path.realpath(
os.path.join(
data_dir,
locale_or_legalcode,
translation.to_locale(language_code),
"LC_MESSAGES",
f"{translation_domain}.po",
)
)
)
return pofile_path
def parse_date(date_str: str):
if date_str is None:
return None
try:
date = dateutil.parser.isoparse(date_str)
return date
except ValueError:
return None
def get_pofile_creation_date(pofile_obj: polib.POFile):
try:
po_creation_date = pofile_obj.metadata["POT-Creation-Date"]
except KeyError:
return None
return parse_date(po_creation_date)
def get_pofile_revision_date(pofile_obj: polib.POFile):
try:
po_revision_date = pofile_obj.metadata["PO-Revision-Date"]
except KeyError:
return None
return parse_date(po_revision_date)
def map_django_to_transifex_language_code(django_language_code: str) -> str:
"""
Given a Django language code, return a Transifex language code.
Django language codes are lowercase IETF language tags
Transifex language codes are POSIX Locales
"""
transifex_language_code = django_language_code
# Lookup special cases
transifex_language_code = LANGMAP_DJANGO_TO_TRANSIFEX.get(
transifex_language_code,
transifex_language_code,
)
return transifex_language_code
def map_legacy_to_django_language_code(legacy_language_code: str) -> str:
"""
Given a Legacy language code, return a Django language code.
Legacy language codes include:
- POSIX Locales (ex. Transifex language codes)
- conventential IETF language tags (instead of lowercase, ex. zh-Hans)
Django language codes are lowercase IETF language tag
"""
django_language_code = legacy_language_code
# Normalize: lowercase
django_language_code = django_language_code.lower()
# Noarmalize: use dash
django_language_code = django_language_code.replace("@", "-")
django_language_code = django_language_code.replace("_", "-")
# Lookup special cases
django_language_code = LANGMAP_LEGACY_TO_DJANGO.get(
django_language_code,
django_language_code,
)
return django_language_code
def get_default_language_for_jurisdiction_deed(jurisdiction_code):
default_language = DEFAULT_JURISDICTION_LANGUAGES.get(
jurisdiction_code, settings.LANGUAGE_CODE
)
if default_language in settings.LANGUAGES_MOSTLY_TRANSLATED:
return default_language
else:
return settings.LANGUAGE_CODE
def get_default_language_for_jurisdiction_naive(jurisdiction_code):
return DEFAULT_JURISDICTION_LANGUAGES.get(
jurisdiction_code, settings.LANGUAGE_CODE
)
def get_jurisdiction_name(category, unit, version, jurisdiction_code):
# For details on nomenclature for unported licenses, see:
# https://wiki.creativecommons.org/wiki/License_Versions
if unit in ["zero", "mark"]:
jurisdiction_code = "=p10"
elif category == "licenses" and not jurisdiction_code:
if version == "4.0":
jurisdiction_code = "=l40"
elif version == "3.0":
jurisdiction_code = "=l30"
jurisdiction_default = JURISDICTION_NAMES.get("")
jurisdiction_name = JURISDICTION_NAMES.get(
jurisdiction_code, jurisdiction_default
)
return jurisdiction_name
def get_deeds_ux_pofiles():
deed_ux_pofiles = []
for locale_name in os.listdir(settings.DEEDS_UX_LOCALE_PATH):
language_code = translation.to_language(locale_name)
pofile_path = get_pofile_path(
locale_or_legalcode="locale",
language_code=language_code,
translation_domain="django",
)
if not os.path.isfile(pofile_path): # pragma: no cover
continue
deed_ux_pofiles.append([language_code, pofile_path])
deed_ux_pofiles.sort(key=lambda x: x[0])
return deed_ux_pofiles
def load_deeds_ux_translations():
"""
Process Deed & UX translations (store information on all and track those
that meet or exceed the TRANSLATION_THRESHOLD).
"""
deeds_ux_po_file_info = {}
languages_mostly_translated = []
for language_code, pofile_path in get_deeds_ux_pofiles():
pofile_obj = polib.pofile(pofile_path)
percent_translated = pofile_obj.percent_translated()
deeds_ux_po_file_info[language_code] = {
"percent_translated": percent_translated,
"creation_date": get_pofile_creation_date(pofile_obj),
"revision_date": get_pofile_revision_date(pofile_obj),
"metadata": pofile_obj.metadata,
}
update_lang_info(language_code)
if (
percent_translated < settings.TRANSLATION_THRESHOLD
and language_code != settings.LANGUAGE_CODE
):
continue
languages_mostly_translated.append(language_code)
deeds_ux_po_file_info = dict(sorted(deeds_ux_po_file_info.items()))
# Add global settings
settings.DEEDS_UX_PO_FILE_INFO = deeds_ux_po_file_info
settings.LANGUAGES_MOSTLY_TRANSLATED = sorted(
list(set(languages_mostly_translated))
)
def update_lang_info(language_code):
"""
Normalize language information using Babel
"""
order_to_bidi = {
"left-to-right": False,
"right-to-left": True,
}
locale_name = translation.to_locale(language_code)
try:
locale = Locale.parse(locale_name)
if language_code not in LANG_INFO:
LANG_INFO[language_code] = {}
lang_info = LANG_INFO[language_code]
if not lang_info.get("name"):
lang_info["name"] = locale.get_display_name("en")
if not lang_info.get("name_local"):
lang_info["name_local"] = locale.get_display_name(locale_name)
if not lang_info.get("bidi"):
lang_info["bidi"] = order_to_bidi[locale.character_order]
except UnknownLocaleError:
pass
def write_transstats_csv(output_file):
csv_headers = [
"lang_django",
"lang_locale",
"lang_transifex",
"num_messages",
"num_trans",
"num_fuzzy",
"percent_trans",
]
with open(output_file, "w") as output_file:
# Create CSV writer
writer = csv.DictWriter(output_file, csv_headers, dialect="unix")
writer.writeheader()
# Load PO Files and write a row for each
for language_code, pofile_path in get_deeds_ux_pofiles():
locale_name = translation.to_locale(language_code)
transifex_code = map_django_to_transifex_language_code(
language_code
)
pofile_obj = polib.pofile(pofile_path)
translated = len(pofile_obj.translated_entries())
fuzzy = len(pofile_obj.fuzzy_entries())
percent_translated = pofile_obj.percent_translated()
writer.writerow(
{
"lang_django": language_code,
"lang_locale": locale_name,
"lang_transifex": transifex_code,
"num_messages": len(pofile_obj),
"num_trans": translated,
"num_fuzzy": fuzzy,
"percent_trans": percent_translated,
}
)