okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
52ba0b781
main
1"""French (fr) catalog tooling for the desktop app (#3970). 2 3The French texts live in src/locales/fr.json, fix-fr.json, tune-fr.json and tools-fr.json, next to the other languages. 4They are edited by hand (they are the translations, not generated), so a string added to an English catalog later has no 5French yet. This script lists and merges those. From apps/host: 6 7python scripts/fr_sync.py missing [out.tsv] write every English string that has no French yet 8python scripts/fr_sync.py apply done.tsv merge translations, rewrite the four fr files in English key order 9python scripts/fr_sync.py check exit 1 if a French string is missing, empty or breaks a placeholder / tag 10 11The TSV has one string per line: <id><TAB><text>. In the text, a newline is written \\n and a backslash \\\\. 12In the "done" file a lone = means "keep the English text as it is" (class names, product names, file names). 13The id prefix picks the file: s0001 -> fr.json, fx01 -> fix-fr.json, tune01 -> tune-fr.json, tl01 -> tools-fr.json. 14Use "vous", plain short words, a next step, and a no-break space (U+00A0) before : ; ? ! and inside guillemets. 15""" 16import json 17import re 18import sys 19from pathlib import Path 20 21LOCALES = Path (__file__ ).resolve ().parent .parent / "src" / "locales" 22PAIRS = [("en.json" ,"fr.json" ), ("fix-en.json" ,"fix-fr.json" ), ("tune-en.json" ,"tune-fr.json" ), ("tools-en.json" ,"tools-fr.json" )] 23SLOT = re .compile (r"\{\d+\}" ) 24TAG = re .compile (r"</?[A-Za-z][^>]*>" ) 25 26 27def load (name ): 28path = LOCALES / name 29return json .loads (path .read_text (encoding = "utf-8" ))if path .exists ()else {} 30 31 32def file_of (identifier ): 33for en_name ,fr_name in PAIRS : 34if identifier in load (en_name ): 35return fr_name 36raise SystemExit (f"unknown id { identifier } : it is in no English catalog" ) 37 38 39def escape (text ): 40return text .replace ("\\" ,"\\\\" ).replace ("\n" ,"\\n" ) 41 42 43def unescape (text ): 44out ,i = [],0 45while i < len (text ): 46if text [i ]== "\\" and i + 1 < len (text ): 47out .append ({"n" :"\n" ,"\\" :"\\" }.get (text [i + 1 ],text [i + 1 ])) 48i += 2 49else : 50out .append (text [i ]) 51i += 1 52return "" .join (out ) 53 54 55NBSP = " " 56 57 58def typography (text ): 59"""French spacing and apostrophes, applied to every translated (not kept) string. 60l'image -> l'image with a typographic apostrophe (also before a {0} slot); a no-break space before : ; ? ! % and inside the guillemets.""" 61text = re .sub (r"(?<=[A-Za-zÀ-ſ])'(?=[A-Za-zÀ-ſ{])" ,"’" ,text ) 62text = re .sub (r"(?<=\S) ([:;?!])" ,NBSP + r"\1" ,text ) 63text = re .sub (r"(?<=[\d}]) %" ,NBSP + "%" ,text ) 64text = re .sub ("« ?" ,"«" + NBSP ,text ) 65text = re .sub (" ?»" ,NBSP + "»" ,text ) 66return text 67 68 69def problems (identifier ,source ,text ): 70found = [] 71if not text .strip (): 72found .append ("empty" ) 73expected = [slot for slot in SLOT .findall (source )if not (identifier == "s2463" and slot == "{1}" )]# the tests allow this one to drop {1} (the plural suffix) 74if sorted (expected )!= sorted (SLOT .findall (text )): 75found .append ("placeholders differ" ) 76if sorted (TAG .findall (source ))!= sorted (TAG .findall (text )): 77found .append ("markup differs" ) 78return found 79 80 81def missing (): 82todo = {} 83for en_name ,fr_name in PAIRS : 84en ,fr = load (en_name ),load (fr_name ) 85for identifier ,text in en .items (): 86if identifier not in fr : 87todo [identifier ]= text 88return todo 89 90 91def main (argv ): 92command = argv [1 ]if len (argv )> 1 else "" 93if command == "missing" : 94todo = missing () 95lines = [f" { identifier } \t { escape ( text ) } " for identifier ,text in todo .items ()] 96if len (argv )> 2 : 97Path (argv [2 ]).write_text ("\n" .join (lines )+ ("\n" if lines else "" ),encoding = "utf-8" ) 98f" { len ( lines ) } strings without French written to { argv [ 2 ] } " ) 99else : 100"\n" .join (lines )) 101f" { len ( lines ) } strings without French" ,file = sys .stderr ) 102return 0 103if command == "apply" and len (argv )> 2 : 104incoming = {} 105for number ,line in enumerate (Path (argv [2 ]).read_text (encoding = "utf-8" ).splitlines (),1 ): 106if not line .strip (): 107continue 108if "\t" not in line : 109raise SystemExit (f" { argv [ 2 ] } : { number } : no tab" ) 110identifier ,text = line .split ("\t" ,1 ) 111incoming [identifier .strip ()]= text 112bad = 0 113for en_name ,fr_name in PAIRS : 114en ,fr = load (en_name ),load (fr_name ) 115changed = False 116for identifier ,source in en .items (): 117if identifier in incoming : 118text = source if incoming [identifier ]== "=" else typography (unescape (incoming [identifier ])) 119for why in problems (identifier ,source ,text ): 120f" { identifier } : { why } : { source !r } -> { text !r } " ) 121bad += 1 122fr [identifier ]= text 123changed = True 124if changed : 125ordered = {identifier :fr [identifier ]for identifier in en if identifier in fr } 126 (LOCALES / fr_name ).write_text (json .dumps (ordered ,ensure_ascii = False ,indent = 1 if en_name != "en.json" else 2 )+ "\n" ,encoding = "utf-8" ) 127unknown = [identifier for identifier in incoming if not any (identifier in load (en_name )for en_name ,_ in PAIRS )] 128for identifier in unknown : 129f" { identifier } : not in any English catalog" ) 130bad += 1 131f"applied { len ( incoming ) } strings, { bad } problems" ) 132return 1 if bad else 0 133if command == "check" : 134bad = 0 135for en_name ,fr_name in PAIRS : 136en ,fr = load (en_name ),load (fr_name ) 137for identifier ,source in en .items (): 138if identifier not in fr : 139f" { fr_name } : { identifier } has no French yet: { source !r } " ) 140bad += 1 141continue 142for why in problems (identifier ,source ,fr [identifier ]): 143f" { fr_name } : { identifier } : { why } " ) 144bad += 1 145for identifier in fr : 146if identifier not in en : 147f" { fr_name } : { identifier } is not in { en_name } " ) 148bad += 1 149"French catalogs complete" if not bad else f" { bad } problems" ) 150return 1 if bad else 0 151__doc__ ) 152return 2 153 154 155if __name__ == "__main__" : 156raise SystemExit (main (sys .argv ))