SPB Git

Repository Public

add-translations.py 1.2 KB 22 lines python

1#!/usr/bin/env python32"""Appends French translations to fr.lproj/Localizable.strings.3Usage: scripts/add-translations.py <file.py defining a dict T: English key -> French text> [section title]4Keys already translated are left alone; removed 2.0 keys can be pruned with --prune KEY…"""5import pathlib, re, sys6root = pathlib.Path(__file__).resolve().parent.parent7path = root / "SPBrowser/Resources/fr.lproj/Localizable.strings"8text = path.read_text(encoding="utf-8")9if sys.argv[1] == "--prune":10    for key in sys.argv[2:]:11        text = re.sub(r'^"' + re.escape(key) + r'"\s*=.*;\n', "", text, flags=re.M)12    path.write_text(text, encoding="utf-8"); sys.exit(0)13scope = {}14exec(pathlib.Path(sys.argv[1]).read_text(encoding="utf-8"), scope)15esc = lambda s: s.replace("\\", "\\\\").replace('"', '\\"')16existing = set(re.findall(r'^"((?:[^"\\]|\\.)*)"\s*=', text, flags=re.M))17lines = [f'"{esc(k)}" = "{esc(v)}";' for k, v in scope["T"].items() if esc(k) not in existing]18if lines:19    title = sys.argv[2] if len(sys.argv) > 2 else "Added"20    path.write_text(text.rstrip("\n") + f"\n\n/* {title} */\n" + "\n".join(lines) + "\n", encoding="utf-8")21print(f"{len(lines)} translations added")22
Files main
Menu

Theme