-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapply_locale_corrections.py
More file actions
740 lines (646 loc) · 28.4 KB
/
Copy pathapply_locale_corrections.py
File metadata and controls
740 lines (646 loc) · 28.4 KB
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#!/usr/bin/env python3
"""Apply LLM-curated regex corrections to msgstr lines of a .po file.
Usage: python3 apply_locale_corrections.py <locale>
Example: python3 apply_locale_corrections.py da_DK
Corrections fix systematic Argos MT mistakes (wrong word sense, broken spacing,
inconsistent DNS terminology). The dicts here are hand-curated based on review
of actual MT output - each entry has a reason.
"""
import json
import os
import re
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import poutil # noqa: E402
ROOT = poutil.ROOT
def _subs_da():
"""Danish: fix Argos's recurring word-sense and spacing issues."""
return [
# "wizard" -> Argos picks the magic sense; software guide is "guide"/"guider"
(r"\btroldmænd\b", "guider"),
(r"\bTroldmænd\b", "Guider"),
(r"\btroldmand\b", "guide"),
(r"\bTroldmand\b", "Guide"),
# "config file" - Argos prefers "indstillingsfil" but Danish docs use "konfigurationsfil"
(r"\bindstillingsfil\b", "konfigurationsfil"),
(r"\bIndstillingsfil\b", "Konfigurationsfil"),
(r"\bindstillingsfilen\b", "konfigurationsfilen"),
(r"\bIndstillingsfilen\b", "Konfigurationsfilen"),
# Remove stray space after hyphen in compound words (Argos bug)
(r"\bzone- ", "zone-"),
(r"\bZone- ", "Zone-"),
# "record" noun - the file settled on the English word, 334 entries to 47,
# so these Argos artifacts go straight there rather than through "post".
(r"\bOpgørelsen blev\b", "Recorden blev"),
(r"\bOpgørelser\b", "Records"),
(r"\bopgørelser\b", "records"),
# "Optag" in record context (means recording button)
(r"\bOptag oprettet\b", "Record oprettet"),
(r"\bOptag opdateret\b", "Record opdateret"),
(r"\bOptag slettet\b", "Record slettet"),
# The remaining "post" spellings, only where the source really is about a
# record. Danish "post" is also mail, and postkasse/Postfix/postsystemer
# must survive - whole-word anchoring is what keeps them out of reach.
((r"\brecords?\b", None), r"\bposter\b", "records"),
((r"\brecords?\b", None), r"\bPoster\b", "Records"),
((r"\brecords?\b", None), r"\bposten\b", "recorden"),
((r"\brecords?\b", None), r"\bPosten\b", "Recorden"),
((r"\brecords?\b", None), r"\bposttyper\b", "recordtyper"),
((r"\brecords?\b", None), r"\bposttype\b", "recordtype"),
((r"\brecords?\b", None), r"\bpostnavnet\b", "recordnavnet"),
((r"\brecords?\b", None), r"\bPostnavnet\b", "Recordnavnet"),
((r"\brecords?\b", None), r"\bpostnavn\b", "recordnavn"),
((r"\brecords?\b", None), r"\bpost\b", "record"),
((r"\brecords?\b", None), r"\bPost\b", "Record"),
# "skuespiller" (actor as in performer) -> "udfører" (the one performing the action)
(r"\bskuespilleren\b", "udføreren"),
(r"\bSkuespilleren\b", "Udføreren"),
# "returopgørelse" -> "returerklæring" (return statement in code)
(r"\breturopgørelse\b", "return-erklæring"),
# "Standard session krypteringsnøgle bruges" - grammar issue
(r"Standard session krypteringsnøgle bruges, skal du", "Standard sessionkrypteringsnøgle anvendes - du skal"),
# "save" verb - if Argos used "spare" anywhere
(r"\bSpare\b", "Gem"),
(r"\bspare\b", "gem"),
]
def _subs_el():
"""Greek: DNS terminology + UI wizard sense fixes."""
return [
# "wizard" -> Argos picks "μάγος" (magician); software guide is "οδηγός"
(r"\bμάγοι\b", "οδηγοί"),
(r"\bΜάγοι\b", "Οδηγοί"),
(r"\bμάγος\b", "οδηγός"),
(r"\bΜάγος\b", "Οδηγός"),
(r"\bμάγου\b", "οδηγού"),
(r"\bΜάγου\b", "Οδηγού"),
(r"\bμάγοι DNS\b", "οδηγοί DNS"),
(r"\bΜάγοι DNS\b", "Οδηγοί DNS"),
(r"\bDNS μάγοι\b", "DNS οδηγοί"),
(r"\bΟι μάγοι DNS\b", "Οι οδηγοί DNS"),
# "record" noun in DNS context -> "εγγραφή" not "αρχείο" (file)
(r"\bαρχεία σε αυτή τη ζώνη\b", "εγγραφές σε αυτή τη ζώνη"),
(r"\bαρχεία σε αυτήν τη ζώνη\b", "εγγραφές σε αυτήν τη ζώνη"),
# Remove stray spaces after hyphen
(r"\be- ", "e-"),
(r"\bE- ", "E-"),
]
def _subs_sl():
"""Slovenian: DNS terminology + UI wizard sense fixes."""
return [
# "wizard" -> Argos picks "čarovnik" (magician); software helper is "pomočnik"
(r"\bČarovniki\b", "Pomočniki"),
(r"\bčarovniki\b", "pomočniki"),
(r"\bČarovnik\b", "Pomočnik"),
(r"\bčarovnik\b", "pomočnik"),
(r"\bčarovnika\b", "pomočnika"),
(r"\bČarovnika\b", "Pomočnika"),
# Remove stray spaces after hyphen
(r"\be- ", "e-"),
(r"\bE- ", "E-"),
# "razsuti tovor" (bulk cargo) -> "množični" for bulk operations
(r"\brazsutega tovora\b", "množične registracije"),
(r"\bRegistracija razsutega tovora\b", "Množična registracija"),
# "kanonično naročilo" (canonical purchase order) -> "kanonični vrstni red"
(r"\bkanoničnem naročilu\b", "kanoničnem vrstnem redu"),
(r"\bkanonično naročilo\b", "kanonični vrstni red"),
# Fix "» install / «" path spacing
(r"»\s*install\s*/\s*«", "»install/«"),
]
def _subs_bg():
"""Bulgarian: DNS terminology + UI wizard sense fixes."""
return [
# "wizard" -> Argos picks "магьосник" (magician); software helper is "помощник"
(r"\bМагьосниците\b", "Помощниците"),
(r"\bмагьосниците\b", "помощниците"),
(r"\bМагьосник\b", "Помощник"),
(r"\bмагьосник\b", "помощник"),
(r"\bМагьосници\b", "Помощници"),
(r"\bмагьосници\b", "помощници"),
(r"\bмагьосника\b", "помощника"),
(r"\bМагьосника\b", "Помощника"),
]
def _subs_ga():
"""Irish: most IT terms keep English; fix obvious mistranslations."""
return [
# Argos repeated the whole translation for short single-word sources,
# e.g. "Type" -> "Cineál Cineál Cineál Cineál". Anchored to the entire
# msgstr so only these degenerate entries collapse.
(r"^(\S+)(?: \1)+$", r"\1"),
]
def _subs_mt():
"""Maltese: DNS terminology fixes."""
return []
def _subs_ms():
"""Malay: DNS terminology fixes."""
return [
# Argos duplicated words mid-sentence throughout this locale, e.g.
# "Zona Zona tidak ditemukan" and "Catatan Perubahan Catatan Catatan".
# Malay reduplication is hyphenated ("buku-buku"), never space-separated,
# so collapsing a repeated run is safe. The 3+ char floor keeps IPv6 and
# record-format examples like "xx xx" intact.
(r"\b(\w{3,})\b(?: \1\b)+", r"\1"),
# Argos split "E-mel" (email) across the hyphen
(r"\bE- mel\b", "E-mel"),
# Argos produced Indonesian vocabulary rather than Malay. These pairs have
# no competing sense in this UI, so they substitute unconditionally.
(r"\bditemukan\b", "dijumpai"),
(r"\bDitemukan\b", "Dijumpai"),
(r"\bprioritas\b", "keutamaan"),
(r"\bPrioritas\b", "Keutamaan"),
(r"\bcocok\b", "sepadan"),
(r"\bKelompok\b", "Kumpulan"),
(r"\bkelompok\b", "kumpulan"),
(r"\bberkas\b", "fail"),
(r"\bBerkas\b", "Fail"),
(r"\bsilakan\b", "sila"),
(r"\bSilakan\b", "Sila"),
(r"\bgalat\b", "ralat"),
(r"\bGalat\b", "Ralat"),
(r"\bsertifikat\b", "sijil"),
(r"\bSertifikat\b", "Sijil"),
(r"\bdinonaktifkan\b", "dilumpuhkan"),
(r"\bdiperbarui\b", "dikemas kini"),
(r"\bnomor\b", "nombor"),
(r"\bNomor\b", "Nombor"),
(r"\btombol\b", "butang"),
(r"\bpersentase\b", "peratusan"),
(r"\bPersentase\b", "Peratusan"),
(r"\bpengaturan\b", "tetapan"),
(r"\bPengaturan\b", "Tetapan"),
(r"\bkebijakan\b", "dasar"),
(r"\bKebijakan\b", "Dasar"),
(r"\bkomentar\b", "komen"),
(r"\bKomentar\b", "Komen"),
(r"\btidak valid\b", "tidak sah"),
(r"\bTidak valid\b", "Tidak sah"),
(r"\bdihapus\b", "dipadam"),
(r"\bDihapus\b", "Dipadam"),
(r"\bkarena\b", "kerana"),
(r"\bKarena\b", "Kerana"),
(r"\bmengirim\b", "menghantar"),
(r"\btipe\b", "jenis"),
(r"\bTipe\b", "Jenis"),
(r"\bkeamanan\b", "keselamatan"),
(r"\bKeamanan\b", "Keselamatan"),
(r"\bbisa\b", "boleh"),
# "rekor" is the Indonesian spelling; the DNS term is "rekod"
(r"\brekor\b", "rekod"),
(r"\bRekor\b", "Rekod"),
(r"\brekaman\b", "rekod"),
(r"\bRekaman\b", "Rekod"),
# "catatan" renders both Record and Note, so it only becomes "rekod" when
# the English source is about a record. Same for "rekaman".
((r"\brecords?\b", r"\b(note|log|comment)\b"), r"\bCatatan\b", "Rekod"),
((r"\brecords?\b", r"\b(note|log|comment)\b"), r"\bcatatan\b", "rekod"),
((r"\bpermission", None), r"\bIzin\b", "Kebenaran"),
((r"\bpermission", None), r"\bizin\b", "kebenaran"),
]
def _subs_id():
"""Indonesian: normalize terminology that drifted between synonyms."""
return [
# "rekod" is Malay. This file settled on "rekaman" for a DNS record
# (562 entries against 98), so the stray Malay forms move to it.
(r"\brekod\b", "rekaman"),
(r"\bRekod\b", "Rekaman"),
# "templat" is the Indonesian spelling and already dominates 189 to 63.
# Indonesian has no plural -s, so "templates" collapses to the same word.
# A slash counts as a word boundary, so the lookaround is what keeps the
# literal path templates/emails/custom/ from being rewritten; the \b alone
# already excludes the underscored config key default_zone_template.
(r"(?<![/\\])\btemplates?\b(?![/\\])", "templat"),
(r"(?<![/\\])\bTemplates?\b(?![/\\])", "Templat"),
# "catatan" renders both Record and Note, so it only becomes "rekaman"
# when the English source is about a record.
((r"\brecords?\b", r"\b(note|log|comment)\b"), r"\bCatatan\b", "Rekaman"),
((r"\brecords?\b", r"\b(note|log|comment)\b"), r"\bcatatan\b", "rekaman"),
]
def _subs_sr():
"""Serbian: keep API in Latin like every other acronym in this file."""
return [
# The file is Cyrillic throughout but leaves DNSSEC, PowerDNS and URL in
# Latin, and already writes API that way in 28 entries against 59. The
# acronym also has to match the api.* config keys users search for.
(r"\bАПИ\b", "API"),
]
def _subs_pt_br():
"""pt_BR refinements after pt_PT seed: BR-natural phrasing."""
return [
# "Não tem permissão" is European; BR more naturally says "Você não tem permissão"
(r"\bNão tem permissão\b", "Você não tem permissão"),
# "Utilize" -> "Use" (BR uses both; Use is more frequent in software UI)
(r"\bUtilize\b", "Use"),
(r"\butilize\b", "use"),
]
def _subs_sq():
"""Albanian: wizard sense fix (magjistar -> ndihmës)."""
return [
(r"\bmagjistar\b", "ndihmës"),
(r"\bMagjistar\b", "Ndihmës"),
(r"\bmagjistari\b", "ndihmësi"),
(r"\bMagjistari\b", "Ndihmësi"),
(r"\bmagjistarë\b", "ndihmës"),
(r"\bMagjistarë\b", "Ndihmës"),
]
def _subs_hi():
"""Hindi: wizard sense fix (jaadugar -> sahaayak)."""
return [
(r"जादूगर", "सहायक"),
]
def _subs_th():
"""Thai: wizard sense fix."""
return [
(r"พ่อมด", "ตัวช่วย"),
]
def _subs_zh_tw():
"""zh_TW vocabulary fixes: zhconv only converts characters, not Mainland vs Taiwan words."""
return [
# software/computing vocabulary differences
(r"配置文件", "設定檔"), # config file
(r"配置", "設定"), # config (general)
(r"設置", "設定"), # settings
(r"程序", "程式"), # program (computer)
(r"創建", "建立"), # create
(r"默認", "預設"), # default
(r"用戶", "使用者"), # user
(r"信息", "資訊"), # information
(r"軟件", "軟體"), # software
(r"網絡", "網路"), # network
(r"網絡接口", "網路介面"), # network interface
(r"接口", "介面"), # interface
(r"服務器", "伺服器"), # server
(r"數據庫", "資料庫"), # database
(r"數據", "資料"), # data
(r"鼠標", "滑鼠"), # mouse
(r"視頻", "影片"), # video
(r"打印", "列印"), # print
(r"幫助", "說明"), # help
(r"屏幕", "螢幕"), # screen
(r"質量", "品質"), # quality
(r"水平", "水準"), # level/standard
(r"在線", "線上"), # online
(r"離線", "離線"), # offline (same)
(r"主機", "主機"), # host (same)
(r"備份", "備份"), # backup (same)
# zhconv may keep "未找到" Mainland phrasing
(r"未找到", "找不到"),
]
def _subs_bs():
"""Bosnian refinements after hr_HR seed: BS-specific lexicon and month names.
Bosnian and Croatian are mutually intelligible; only the words that diverge in
BS standard get rewritten. The big ones for DNS-admin UI are:
- server/system terminology: HR \"poslužitelj\"/\"sustav\" -> BS \"server\"/\"sistem\"
- dot/exact: HR \"točka\"/\"točno\" -> BS \"tačka\"/\"tačno\" (t before k/n)
- months: HR siječanj/veljača/... -> BS januar/februar/...
"""
return [
# === server: HR \"poslužitelj\" -> BS \"server\" ===
(r"\bposlužiteljima\b", "serverima"),
(r"\bposlužitelje\b", "servere"),
(r"\bposlužitelju\b", "serveru"),
(r"\bposlužitelja\b", "servera"),
(r"\bposlužitelji\b", "serveri"),
(r"\bposlužiteljem\b", "serverom"),
(r"\bposlužitelj\b", "server"),
(r"\bPoslužiteljima\b", "Serverima"),
(r"\bPoslužitelje\b", "Servere"),
(r"\bPoslužitelju\b", "Serveru"),
(r"\bPoslužitelja\b", "Servera"),
(r"\bPoslužitelji\b", "Serveri"),
(r"\bPoslužitelj\b", "Server"),
# === system: HR \"sustav\" -> BS \"sistem\" ===
(r"\bsustavima\b", "sistemima"),
(r"\bsustave\b", "sisteme"),
(r"\bsustavu\b", "sistemu"),
(r"\bsustava\b", "sistema"),
(r"\bsustavi\b", "sistemi"),
(r"\bsustav\b", "sistem"),
(r"\bSustavima\b", "Sistemima"),
(r"\bSustave\b", "Sisteme"),
(r"\bSustavu\b", "Sistemu"),
(r"\bSustava\b", "Sistema"),
(r"\bSustavi\b", "Sistemi"),
(r"\bSustav\b", "Sistem"),
# === dot/point/exact: HR \"točk-\"/\"točn-\" -> BS \"tačk-\"/\"tačn-\" ===
(r"\btočka\b", "tačka"),
(r"\btočke\b", "tačke"),
(r"\btočki\b", "tački"),
(r"\btočku\b", "tačku"),
(r"\btočkom\b", "tačkom"),
(r"\btočkama\b", "tačkama"),
(r"\bTočka\b", "Tačka"),
(r"\bTočke\b", "Tačke"),
(r"\bTočku\b", "Tačku"),
(r"\btočno\b", "tačno"),
(r"\bTočno\b", "Tačno"),
(r"\btočan\b", "tačan"),
(r"\btočna\b", "tačna"),
(r"\btočni\b", "tačni"),
(r"\bTočni\b", "Tačni"),
(r"\btočnost\b", "tačnost"),
(r"\btočnosti\b", "tačnosti"),
# colon: HR \"dvotočka\" (literally \"two-dots\") -> BS \"dvotačka\"
(r"\bdvotočka\b", "dvotačka"),
(r"\bdvotočke\b", "dvotačke"),
(r"\bdvotočku\b", "dvotačku"),
(r"\bdvotočkom\b", "dvotačkom"),
# === print: HR \"tisak\" -> BS \"štampa\" ===
(r"\btisak\b", "štampa"),
(r"\bTisak\b", "Štampa"),
(r"\btiskati\b", "štampati"),
(r"\btiskanje\b", "štampanje"),
# === months: HR Slavic -> BS international ===
(r"\bsiječanj\b", "januar"),
(r"\bSiječanj\b", "Januar"),
(r"\bveljača\b", "februar"),
(r"\bVeljača\b", "Februar"),
(r"\božujak\b", "mart"),
(r"\bOžujak\b", "Mart"),
(r"\btravanj\b", "april"),
(r"\bTravanj\b", "April"),
(r"\bsvibanj\b", "maj"),
(r"\bSvibanj\b", "Maj"),
(r"\blipanj\b", "juni"),
(r"\bLipanj\b", "Juni"),
(r"\bsrpanj\b", "juli"),
(r"\bSrpanj\b", "Juli"),
(r"\bkolovoz\b", "august"),
(r"\bKolovoz\b", "August"),
(r"\brujan\b", "septembar"),
(r"\bRujan\b", "Septembar"),
(r"\blistopad\b", "oktobar"),
(r"\bListopad\b", "Oktobar"),
(r"\bstudeni\b", "novembar"),
(r"\bStudeni\b", "Novembar"),
(r"\bprosinac\b", "decembar"),
(r"\bProsinac\b", "Decembar"),
# === week: HR tjedan -> BS sedmica ===
(r"\btjedan\b", "sedmica"),
(r"\bTjedan\b", "Sedmica"),
(r"\btjedna\b", "sedmice"),
(r"\btjedno\b", "sedmično"),
(r"\bTjedno\b", "Sedmično"),
# === thousand: HR tisuća -> BS hiljada ===
(r"\btisuća\b", "hiljada"),
(r"\bTisuća\b", "Hiljada"),
(r"\btisuću\b", "hiljadu"),
(r"\btisućama\b", "hiljadama"),
# === per/according to: HR sukladno -> BS u skladu sa ===
(r"\bsukladno\b", "u skladu sa"),
(r"\bSukladno\b", "U skladu sa"),
]
def _subs_ar():
"""Arabic: wizard sense fix (sahir -> mu'aawin)."""
return [
(r"ساحر", "معالج"),
(r"السحرة", "المعالجات"),
]
def _subs_fa():
"""Farsi: wizard sense fix."""
return [
(r"جادوگر", "دستیار"),
(r"جادوگران", "دستیاران"),
]
def _subs_he():
"""Hebrew: wizard sense fix."""
return [
(r"קוסם", "אשף"),
(r"קוסמים", "אשפים"),
]
SUBS_BY_LOCALE = {
"da_DK": _subs_da(),
"el_GR": _subs_el(),
"sl_SI": _subs_sl(),
"bg_BG": _subs_bg(),
"ga_IE": _subs_ga(),
"mt_MT": _subs_mt(),
"ms_MY": _subs_ms(),
"id_ID": _subs_id(),
"pt_BR": _subs_pt_br(),
"zh_TW": _subs_zh_tw(),
"sq_AL": _subs_sq(),
"hi_IN": _subs_hi(),
"th_TH": _subs_th(),
"ar_SA": _subs_ar(),
"fa_IR": _subs_fa(),
"he_IL": _subs_he(),
"bs_BA": _subs_bs(),
"sr_RS": _subs_sr(),
}
PITFALLS = os.path.join(os.path.dirname(os.path.abspath(__file__)), "locale_pitfalls.json")
def load_pitfall_subs(locale):
"""Rules from locale_pitfalls.json in the same shape as the tables above.
The file is the mined half of this toolchain: the tables in this module are
hand-written, those are derived from the repair sweep. Kept separate so a
regenerated pitfalls file never silently rewrites a curated rule.
"""
if not os.path.exists(PITFALLS):
return []
with open(PITFALLS, "r", encoding="utf-8") as fh:
doc = json.load(fh)
subs = []
for rule in doc.get("per_locale", {}).get(locale, {}).get("rules", []):
guard = rule.get("guard") or {}
include, exclude = guard.get("include"), guard.get("exclude")
if include or exclude:
subs.append(((include, exclude), rule["pattern"], rule["replace"]))
else:
subs.append((rule["pattern"], rule["replace"]))
return subs
def report_review_only(locale):
"""Print the substitutions that are deliberately not applied."""
if not os.path.exists(PITFALLS):
return
with open(PITFALLS, "r", encoding="utf-8") as fh:
doc = json.load(fh)
items = doc.get("per_locale", {}).get(locale, {}).get("review_only", [])
if not items:
return
print(f"\n{len(items)} known {locale} problems need a human, not a substitution:")
for item in items:
print(f" {item['wrong']} -> {item['right']} ({item['source_term']})")
print(f" {item['why_not_mechanical']}")
def _matches_guard(entry, guard):
"""A guard is (include_pattern, exclude_pattern); either may be None."""
include, exclude = guard
if include and not re.search(include, entry.msgid, re.IGNORECASE):
return False
if exclude and re.search(exclude, entry.msgid, re.IGNORECASE):
return False
return True
def apply_corrections(po_path: str, subs, dry_run: bool = False):
"""Apply substitutions to the msgstr of every entry.
A rule is either a 2-tuple (pattern, replacement) applied unconditionally, or a
3-tuple (guard, pattern, replacement) where guard is (include, exclude) matched
against the English msgid. The guarded form exists because a single translated
word can render two different sources - Malay "catatan" is both Record and Note -
so a blind substitution would corrupt the entries it does not mean to touch.
With dry_run the file is left alone and every change is printed instead.
"""
entries = poutil.parse(po_path)
preview = []
changes = 0
for entry in entries:
if entry.obsolete or entry.is_header:
continue
def rewrite(text):
# An English fallback is not a translation, so rewriting single words
# inside it only produces a mixed-language hybrid. merge_messages.py
# owns those entries; leave them for a real translation pass.
if text.strip() in (entry.msgid.strip(), (entry.msgid_plural or '').strip()):
return text
for rule in subs:
if len(rule) == 3:
guard, pattern, repl = rule
if not _matches_guard(entry, guard):
continue
else:
pattern, repl = rule
text = re.sub(pattern, repl, text)
return text
if entry.plurals:
for n in sorted(entry.plurals):
new = rewrite(entry.plurals[n])
if new != entry.plurals[n]:
preview.append((entry.msgid, entry.plurals[n], new))
if not dry_run:
entry.set_plural(n, new)
changes += 1
elif entry.msgstr:
new = rewrite(entry.msgstr)
if new != entry.msgstr:
preview.append((entry.msgid, entry.msgstr, new))
if not dry_run:
entry.msgstr = new
changes += 1
if dry_run:
for msgid, old, new in preview:
print(f" {msgid[:70]!r}\n - {old}\n + {new}")
print(f"Would apply {changes} substitutions to {po_path}")
return changes
poutil.write(po_path, entries)
print(f"Applied {changes} substitutions to {po_path}")
return changes
# Config keys, table names and file names. These are code: a translated or
# de-underscored copy sends the user looking for something that does not exist.
_IDENTIFIER = re.compile(
r"\b[a-z][a-z0-9]*(?:_[a-z0-9]+){1,}\b"
r"|\b(?:dns|api|misc|db|ldap|mail|pdns)\.[a-z_.]+\b"
r"|\b[\w-]+\.(?:php|html|twig|js|css|sql|dat|json|ya?ml|conf|ini)\b"
)
def _mangled_pattern(token):
"""Match the token with its separators padded, spaced or turned into spaces."""
return re.escape(token).replace(r"_", r"[\s_]*").replace(r"\.", r"\s*\.\s*")
def restore_identifiers(po_path: str):
"""Rewrite identifiers whose separators were mangled back to the literal form.
Only touches entries where the token survives intact apart from its separators.
Where the translator replaced or truncated the token there is nothing to key
off, so those are left for a human rather than guessed at.
"""
entries = poutil.parse(po_path)
changes = 0
for entry in entries:
if entry.obsolete or entry.is_header or not entry.msgid or not entry.msgstr:
continue
if entry.msgstr.strip() == entry.msgid.strip():
continue
text = entry.msgstr
for token in sorted(set(_IDENTIFIER.findall(entry.msgid)), key=len, reverse=True):
if token in text:
continue
text = re.sub(_mangled_pattern(token), token, text, flags=re.IGNORECASE)
if text != entry.msgstr:
entry.msgstr = text
changes += 1
if changes:
poutil.write(po_path, entries)
print(f"Restored identifiers in {changes} entries in {po_path}")
return changes
# The translator glued a stray capital Z (sometimes ZZ) onto placeholders, so
# "got %d." renders as "got 64Z.". A real translation never puts a bare capital
# Z straight after a placeholder, which is what makes this safe to strip.
_PLACEHOLDER_LEAK = re.compile(r"(%(?:\d+\$)?[sdfucxXob])Z+")
def strip_placeholder_leaks(po_path: str):
"""Remove stray letters the translator appended to printf placeholders."""
entries = poutil.parse(po_path)
changes = 0
for entry in entries:
if entry.obsolete or entry.is_header or not entry.msgid:
continue
def fix(text):
# Leave it alone when the source really does have a Z there.
if not text or _PLACEHOLDER_LEAK.search(entry.msgid):
return text
return _PLACEHOLDER_LEAK.sub(r"\1", text)
if entry.plurals:
for n in sorted(entry.plurals):
new = fix(entry.plurals[n])
if new != entry.plurals[n]:
entry.set_plural(n, new)
changes += 1
elif entry.msgstr:
new = fix(entry.msgstr)
if new != entry.msgstr:
entry.msgstr = new
changes += 1
if changes:
poutil.write(po_path, entries)
print(f"Stripped placeholder leaks in {changes} entries in {po_path}")
return changes
def restore_trailing_whitespace(po_path: str):
"""Put back trailing whitespace the translator dropped.
These msgids end in ": " because the caller appends a detail string. Losing
the space runs the detail into the colon, so the source's trailing run is
authoritative and can be restored without reading the language.
"""
entries = poutil.parse(po_path)
changes = 0
for entry in entries:
if entry.obsolete or entry.is_header or not entry.msgid or not entry.msgstr:
continue
# No English-fallback guard here: appending the source's own trailing run
# cannot change what a translation says, and entries that are correct
# apart from the lost space (acronym labels like "HINFO ") need it too.
tail = entry.msgid[len(entry.msgid.rstrip()):]
if not tail or entry.msgstr.endswith(tail):
continue
entry.msgstr = entry.msgstr.rstrip() + tail
changes += 1
if changes:
poutil.write(po_path, entries)
print(f"Restored trailing whitespace in {changes} entries in {po_path}")
return changes
def main():
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} <locale> "
"[--identifiers|--placeholders|--trailing|--pitfalls] [--dry-run]",
file=sys.stderr)
sys.exit(1)
locale = sys.argv[1]
flags = sys.argv[2:]
identifiers_only = "--identifiers" in flags
placeholders_only = "--placeholders" in flags
pitfalls_only = "--pitfalls" in flags
dry_run = "--dry-run" in flags
if not (identifiers_only or placeholders_only or pitfalls_only) \
and locale not in SUBS_BY_LOCALE:
print(f"Unknown locale: {locale}", file=sys.stderr)
sys.exit(1)
po_path = os.path.join(ROOT, f"locale/{locale}/LC_MESSAGES/messages.po")
if not os.path.exists(po_path):
print(f"Not found: {po_path}", file=sys.stderr)
sys.exit(1)
if identifiers_only:
restore_identifiers(po_path)
elif placeholders_only:
strip_placeholder_leaks(po_path)
elif pitfalls_only:
subs = load_pitfall_subs(locale)
if not subs:
print(f"No pitfall rules for {locale}")
else:
apply_corrections(po_path, subs, dry_run=dry_run)
report_review_only(locale)
else:
apply_corrections(po_path, SUBS_BY_LOCALE[locale], dry_run=dry_run)
if __name__ == "__main__":
main()