"""Lists every interface string the game looks up and checks the translation tables. python Tools/loc/extract.py # writes Tools/loc/keys.tsv, reports each table's gaps python Tools/loc/extract.py --strict # exit 1 if any table misses a key or breaks a placeholder Keys are the English texts inside Loc.T("…"), Loc.F("…"), Loc.N("…"), Loc.Pack("…"), Loc.Key("…") in Assets/Scripts, plus the caption pieces of the Director's and the assistant's lines (DirectorCall.Captions splits the tagged English line into sentences; each piece is shown through Loc.T), plus Tools/loc/extra_keys.txt. Tables: Assets/Resources/Loc/.txt, "englishtranslation" per line, \\n \\t \\\\ escaped, # comments. """ import io, os, re, sys ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) SCRIPTS = os.path.join(ROOT, 'Assets', 'Scripts') TABLES = os.path.join(ROOT, 'Assets', 'Resources', 'Loc') HERE = os.path.dirname(os.path.abspath(__file__)) LANGS = ['fr', 'it', 'de', 'es', 'pt-BR', 'pl', 'tr', 'ru', 'zh-Hans', 'ja', 'ko'] CALL = re.compile(r'Loc\.(?:T|F|N|Pack|Key)\(\s*"((?:[^"\\]|\\.)*)"') SKIP_DIRS = {'Editor'} SKIP_FILES = {'Cheats.cs', 'CheatConsole.cs', 'DemoBot.cs', 'BotCapture.cs', 'Trailer.cs', 'Trailer.Game.cs', 'Trailer.Script.cs', 'Trailer.Story.cs'} def cs_unescape(s): out, i = [], 0 while i < len(s): c = s[i] if c == '\\' and i + 1 < len(s): n = s[i + 1] i += 2 if n == 'n': out.append('\n') elif n == 't': out.append('\t') elif n == 'r': out.append('\r') elif n == '0': out.append('\0') elif n == 'u': out.append(chr(int(s[i:i + 4], 16))); i += 4 else: out.append(n) continue out.append(c) i += 1 return ''.join(out) def esc(s): return s.replace('\\', '\\\\').replace('\t', '\\t').replace('\n', '\\n') def unesc(s): out, i = [], 0 while i < len(s): if s[i] == '\\' and i + 1 < len(s): n = s[i + 1]; out.append('\n' if n == 'n' else '\t' if n == 't' else n); i += 2 else: out.append(s[i]); i += 1 return ''.join(out) # ── the caption pieces, exactly as DirectorCall.Captions makes them ── TAG = re.compile(r'\[[^\]]*\]\s*') def captions(line): plain = TAG.sub('', line).strip() parts = [p for p in re.split(r'(?<=[\.\!\?])\s+', plain) if p] merged = [] for p in parts: if merged and (len(merged[-1]) < 22 or len(p) < 10) and len(merged[-1]) + len(p) < 70: merged[-1] += ' ' + p else: merged.append(p) return merged LINE = re.compile(r'\["((?:dir|asst)_[a-z0-9_]+)"\]\s*=\s*"((?:[^"\\]|\\.)*)"') def collect(): keys = {} # key -> [where] def add(k, where): keys.setdefault(k, []) if where not in keys[k]: keys[k].append(where) for d, dirs, files in os.walk(SCRIPTS): dirs[:] = [x for x in dirs if x not in SKIP_DIRS] for f in files: if not f.endswith('.cs') or f in SKIP_FILES: continue p = os.path.join(d, f) text = io.open(p, encoding='utf-8-sig').read() rel = os.path.relpath(p, SCRIPTS).replace('\\', '/') for m in CALL.finditer(text): line = text.count('\n', 0, m.start()) + 1 add(cs_unescape(m.group(1)), f'{rel}:{line}') if f in ('DirectorCall.cs', 'Assistant.cs'): for m in LINE.finditer(text): who = 'Director' if m.group(1).startswith('dir') else 'assistant' for piece in captions(cs_unescape(m.group(2))): add(piece, f'caption:{m.group(1)} ({who}, spoken in English, subtitle)') extra = os.path.join(HERE, 'extra_keys.txt') if os.path.exists(extra): for raw in io.open(extra, encoding='utf-8'): raw = raw.rstrip('\n') if raw and not raw.startswith('#'): add(unesc(raw.split('\t')[0]), 'extra_keys.txt') keys.pop('', None) return keys def notes(): n = {} for f in sorted(os.listdir(HERE)): if f.startswith('context') and f.endswith('.txt'): for raw in io.open(os.path.join(HERE, f), encoding='utf-8'): raw = raw.rstrip('\n') if '\t' in raw and not raw.startswith('#'): k, v = raw.split('\t', 1) n[unesc(k)] = v return n PH = re.compile(r'\{(\d+)(?:[,:][^}]*)?\}') def placeholders(s): return sorted(set(PH.findall(s))) def load(code): p = os.path.join(TABLES, code + '.txt') if not os.path.exists(p): return None t = {} for raw in io.open(p, encoding='utf-8-sig'): raw = raw.rstrip('\n').rstrip('\r') if not raw or raw.startswith('#') or '\t' not in raw: continue k, v = raw.split('\t', 1) t[unesc(k)] = unesc(v) return t def main(): keys = collect() nt = notes() with io.open(os.path.join(HERE, 'keys.tsv'), 'w', encoding='utf-8', newline='\n') as o: o.write('# english keywherenote (generated by extract.py, do not edit)\n') for k in sorted(keys, key=str.lower): o.write(f'{esc(k)}\t{", ".join(keys[k][:3])}\t{nt.get(k, "")}\n') print(f'{len(keys)} keys -> Tools/loc/keys.tsv') bad = False for code in LANGS: t = load(code) if t is None: print(f' {code}: no table'); bad = True; continue missing = [k for k in keys if not t.get(k)] extra = [k for k in t if k not in keys] broken = [k for k in keys if t.get(k) and placeholders(k) != placeholders(t[k])] print(f' {code}: {len(t)} rows, missing {len(missing)}, unused {len(extra)}, placeholder mismatch {len(broken)}') for k in broken[:10]: print(' PH', repr(k), '->', repr(t[k])) if '-v' in sys.argv: for k in missing[:40]: print(' MISSING', repr(k)) for k in extra[:20]: print(' UNUSED', repr(k)) if missing or broken: bad = True if '--strict' in sys.argv and bad: sys.exit(1) if __name__ == '__main__': main()