-
Notifications
You must be signed in to change notification settings - Fork 22
/
locale_man.py
571 lines (475 loc) · 13.1 KB
/
locale_man.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#
# Copyright (C) Saeed Rasooli <saeed.gnu@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/agpl.txt>.
from scal3 import logger
log = logger.get()
import typing
if typing.TYPE_CHECKING:
import subprocess
import gettext
import os
import string
from collections.abc import Callable
from contextlib import suppress
from os.path import (
isabs,
isfile,
join,
splitext,
)
import mytz
from scal3.cal_types import calTypes
from scal3.json_utils import (
jsonToData,
loadModuleJsonConf,
saveModuleJsonConf,
)
from scal3.path import (
APP_NAME,
confDir,
pixDir,
sourceDir,
)
from scal3.s_object import JsonSObj
from scal3.utils import StrOrderedDict, toStr
# ----------------------------------------------------------
mytz.defaultTZ = mytz.UTC
localTz = mytz.gettz()
localTzStr = str(localTz)
# ----------------------------------------------------------
confPath = join(confDir, "locale.json")
confParams = (
"lang",
"enableNumLocale",
)
def loadConf() -> None:
loadModuleJsonConf(__name__)
def saveConf() -> None:
saveModuleJsonConf(__name__)
# ----------------------------------------------------------
langDir = join(sourceDir, "conf", "lang")
# localeDir = "/usr/share/locale"
# point FIXME
digits = {
"en": ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"),
"ar": ("٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"),
"fa": ("۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"),
"ur": ("۔", "١", "٢", "٣", "۴", "۵", "٦", "٧", "٨", "٩"),
"hi": ("०", "१", "२", "३", "४", "५", "६", "७", "८", "९"),
"th": ("๐", "๑", "๒", "๓", "๔", "๕", "๖", "๗", "๘", "๙"),
}
ascii_alphabet = string.ascii_letters
alphabet = ""
def getLangDigits(
langShortArg: str,
) -> tuple[str, str, str, str, str, str, str, str, str, str]:
d = digits.get(langShortArg)
if d is not None:
return d
return digits["en"]
# ar: Aarbic ar_* Arabic-indic Arabic Contries
# fa: Persian fa_IR Eastern (Extended) Arabic-indic Iran & Afghanintan
# ur: Urdu ur_PK (Eastern) Arabic-indic Pakistan (& Afghanintan??)
# hi: Hindi hi_IN Devenagari India
# th: Thai th_TH # ---------- Thailand
# Urdu digits is a combination of Arabic and Persian digits,
# except for Zero that is named ARABIC-INDIC DIGIT ZERO in unicode database
LRM = "\u200e" # left to right mark
# RLM = "\u200f" # right to left mark
ZWNJ = "\u200c" # zero width non-joiner
ZWJ = "\u200d" # zero width joiner
sysLangDefault = os.environ.get("LANG", "")
langDefault = ""
lang = ""
langActive = ""
# langActive==lang except when lang==""
# (in that case, langActive will be taken from system)
# langActive will not get changed while the program is running
# (because it needs to restart program to apply new language)
langSh = "" # short language name, for example "en", "fa", "fr", ...
rtl = False # right to left
langHasUppercase = True
enableNumLocale = True
# ----------------------------------------------------------
loadConf()
# ----------------------------------------------------------
def tr(s: "str | int", *a, **ka) -> str:
"""String translator function."""
return numEncode(s, *a, **ka) if isinstance(s, int) else str(s)
class LangData(JsonSObj):
params = (
"code",
"name",
"nativeName",
"fileName", # shortName
"flag", # flagFile
"rtl",
"hasUppercase",
"timeZoneList",
)
def __init__(self, _file: str) -> None:
self.file = _file # json file path
# ----
self.code = ""
self.name = ""
self.nativeName = ""
self.fileName = ""
self.flag = ""
self.rtl = False
self.hasUppercase = True
self.transPath = ""
# --
self.timeZoneList: "list[str]" = []
def setData(self, data: dict):
JsonSObj.setData(self, data)
# -----
for param in (
"code",
"name",
"nativeName",
):
if not getattr(self, param):
raise ValueError(
f'missing or empty parameter "{param}"'
f' in language file "{self.file}"',
)
# -----
if not isabs(self.flag):
self.flag = join(pixDir, "flags", self.flag)
# -----
transPath = ""
if self.fileName:
path = join(sourceDir, "locale.d", self.fileName + ".mo")
# log.debug(f"{path=}")
if isfile(path):
transPath = path
else:
# log.debug(f"-------- File {path!r} does not exists")
for prefix in ("/usr", "/usr/local"):
path = join(
prefix,
"share",
"locale",
self.fileName,
"LC_MESSAGES",
f"{APP_NAME}.mo",
)
if isfile(path):
transPath = path
break
# log.debug(code, transPath)
self.transPath = transPath
# ----------------------------------------------------------
langDict = StrOrderedDict()
try:
with open(join(langDir, "default"), encoding="utf-8") as fp:
langDefault = fp.read().strip()
except Exception as e:
log.error(f"failed to read default lang file: {e}")
langFileList = []
with open(join(langDir, "list"), encoding="utf-8") as fp:
for line in fp:
line = line.strip() # noqa: PLW2901
if line.startswith("#"):
continue
langFileList.append(line)
for fname in langFileList:
fname_nox, ext = splitext(fname)
ext = ext.lower()
if ext != ".json":
continue
fpath = join(langDir, fname)
try:
with open(fpath, encoding="utf-8") as fp:
data = jsonToData(fp.read())
except Exception:
log.error(f"failed to load json file {fpath}")
raise
langObj = LangData(fpath)
langObj.setData(data)
langDict[langObj.code] = langObj
# ---
if localTzStr in langObj.timeZoneList:
langDefault = langObj.code
# maybe sort by "code" or "nativeName"
langDict.sort("name")
def popen_output(cmd: "list[str] | str") -> str:
import subprocess
return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] # noqa: S603
def getLocaleFirstWeekDay() -> int:
return int(popen_output(["locale", "first_weekday"])) - 1
def prepareLanguage() -> str:
global lang, langActive, langSh, rtl, langHasUppercase
if lang == "": # noqa: PLC1901
# langActive = locale.setlocale(locale.LC_ALL, "")
langActive = sysLangDefault
if langActive not in langDict.keyList:
langActive = langDefault
# os.environ["LANG"] = langActive
elif lang in langDict.keyList:
# try:
# lang = locale.setlocale(locale.LC_ALL, locale.normalize(lang))
# except locale.Error:
# lang = lang.lower()
# lines = popen_output("locale -a").split("\n") # FIXME
# for line in lines:
# if line.lower().starts(lang)
# locale.setlocale(locale.LC_ALL, lang) # lang = locale.setlocale(...
langActive = lang
os.environ["LANG"] = lang
else: # not lang in langDict.keyList
# locale.setlocale(locale.LC_ALL, langDefault) # lang = locale.setlocale(...
lang = langDefault
langActive = langDefault
os.environ["LANG"] = langDefault
langSh = langActive.split("_")[0]
# sysRtl = (popen_output(["locale", "cal_direction"])=="3\n") # FIXME
rtl = langDict[langActive].rtl
langHasUppercase = langDict[langActive].hasUppercase
return langSh
def loadTranslator() -> Callable:
global tr
transObj = None
langObj = langDict[langActive]
if langObj.transPath:
try:
with open(langObj.transPath, "rb") as fp:
transObj = gettext.GNUTranslations(fp)
except Exception:
log.exception("")
if transObj:
def tr(s, *a, nums=False, ctx=None, default=None, **ka):
orig = s
if isinstance(s, int | float):
s = numEncode(s, *a, **ka)
else:
# pgettext is added in Python 3.8
# even the word "context" does not exist in docs of 3.7
# https://docs.python.org/3.7/library/gettext.html
if ctx and hasattr(transObj, "pgettext"):
s = toStr(transObj.pgettext(ctx, s))
else:
s = toStr(transObj.gettext(s))
if default is not None and s == orig:
s = default
if a:
s = s % a
if ka:
s = s % ka
if nums:
s = textNumEncode(s)
return s
else:
def tr(s, *_a, **_ka):
return str(s)
return tr
def rtlSgn() -> int:
return 1 if rtl else -1
def getMonthName(
calType: int,
month: int,
year: "int | None" = None,
abbreviate: bool = False,
) -> str:
module, ok = calTypes[calType]
if not ok:
raise RuntimeError(f"cal type '{calType}' not found")
if abbreviate:
return module.getMonthNameAb(tr, month, year)
return tr(module.getMonthName(month, year))
def getNumSep() -> str:
return tr(".", ctx="number formatting") if enableNumLocale else "."
def getDigits() -> tuple[str, str, str, str, str, str, str, str, str, str]:
if enableNumLocale:
d = digits.get(langSh)
if d is not None:
return d
return digits["en"]
def getAlphabet() -> str:
global alphabet
if alphabet:
return alphabet
key = "__alphabet__"
value = tr(key)
if value == key:
alphabet = ascii_alphabet
else:
alphabet = value.replace(" ", "")
return alphabet
def getAvailableDigitKeys() -> set[str]:
keys = set(digits["en"])
if langSh != "en":
locDigits = digits.get(langSh)
if locDigits is not None:
keys.update(locDigits)
return keys
def numEncode(
num: int,
localeMode: "str | int | None" = None,
fillZero: int = 0,
negEnd: bool = False,
) -> str:
if not enableNumLocale:
localeMode = "en"
if localeMode is None:
localeMode = langSh
elif isinstance(localeMode, int): # noqa: SIM102
if langSh != "en":
module, ok = calTypes[localeMode]
if not ok:
raise RuntimeError(f"cal type '{localeMode}' not found")
try:
localeMode = module.origLang
except AttributeError:
localeMode = langSh
if localeMode == "en" or localeMode not in digits:
if fillZero:
return str(num).zfill(fillZero)
return str(num)
neg = num < 0
dig = getLangDigits(localeMode)
res = ""
for c in str(abs(num)):
if c == ".":
res += getNumSep()
else:
res += dig[int(c)]
if fillZero > 0:
res = res.rjust(fillZero, dig[0])
if neg:
if negEnd:
res = res + "-"
else:
res = "-" + res
return res
def textNumEncode(
st: str,
localeMode: "str | int | None" = None,
changeSpecialChars: bool = True,
changeDot: bool = False,
) -> str:
if not enableNumLocale:
localeMode = "en"
if localeMode is None:
localeMode = langSh
elif isinstance(localeMode, int): # noqa: SIM102
if langSh != "en":
module, ok = calTypes[localeMode]
if not ok:
raise RuntimeError(f"cal type '{localeMode}' not found")
try:
localeMode = module.origLang
except AttributeError:
localeMode = langSh
dig = getLangDigits(localeMode)
res = ""
for c in toStr(st):
try:
i = int(c)
except ValueError: # noqa: PERF203
if enableNumLocale:
if c == ".":
if changeDot:
c = tr(c, ctx="number formatting") # noqa: PLW2901
elif c in {",", "_"}:
if changeSpecialChars:
c = tr(c) # noqa: PLW2901
elif c == "%": # noqa: SIM102
if changeSpecialChars:
c = tr(c, ctx="number formatting") # noqa: PLW2901
res += c
else:
res += dig[i]
return res
def floatEncode(
st: str,
localeMode: "str | int | None" = None,
):
return textNumEncode(
st,
localeMode,
changeSpecialChars=False,
changeDot=True,
)
def numDecode(numSt: str) -> int:
numSt = numSt.strip()
with suppress(ValueError):
return int(numSt)
numSt = toStr(numSt)
tryLangs = list(digits.keys())
if langSh in digits:
tryLangs.remove(langSh)
tryLangs.insert(0, langSh)
for tryLang in tryLangs:
tryLangDigits = digits[tryLang]
numEn = ""
for dig in numSt:
if dig == "-":
numEn += dig
else:
try:
numEn += str(tryLangDigits.index(dig))
except ValueError:
log.error(f"error in decoding num char {dig}")
# raise e
break
else:
return int(numEn)
raise ValueError(f"invalid locale number {numSt}")
# converts "۱۲:۰۰, ۱۳" to "12:00, 13"
def textNumDecode(text: str) -> str:
text = toStr(text)
textEn = ""
langDigits = getLangDigits(langSh)
for ch in text:
try:
textEn += str(langDigits.index(ch))
except ValueError: # noqa: PERF203
if ch == tr(".", ctx="number formatting"):
textEn += "."
elif ch == tr(","):
textEn += ","
elif ch == tr("_"):
textEn += "_"
else:
# "-" is specially important
textEn += ch
return textEn
def dateLocale(year: int, month: int, day: int) -> str:
return (
numEncode(year, fillZero=4)
+ "/"
+ numEncode(month, fillZero=2)
+ "/"
+ numEncode(day, fillZero=2)
)
def cutText(text: str, n: int) -> str:
text = toStr(text)
newText = text[:n]
if len(text) > n and text[n] not in list(string.printable) + [ZWNJ]:
with suppress(UnicodeDecodeError):
newText += ZWJ
return newText
def addLRM(text: str) -> str:
return LRM + toStr(text)
def popenDefaultLang(*args, **kwargs) -> "subprocess.Popen":
from subprocess import Popen
os.environ["LANG"] = sysLangDefault
p = Popen(*args, **kwargs) # noqa: S603
os.environ["LANG"] = lang
return p
# ----------------------------------------------
prepareLanguage()
loadTranslator()