Files
splinter/tools/check_i18n.py
T
Konrad e128d679e4 splinter: write and edit show a diff
A mutate tool that says 'wrote 23 bytes' tells the operator nothing. write and
edit now put a one-hunk diff into the observation - common prefix and suffix as
context - so the model sees what it did and the transcript shows it as its own
'anderung:' entry.

One hunk, not a general diff algorithm: a tool changes one place, and the point
is to see which. Verified against kontron: asking for an edit produced

  @@ -1,2 +1,2 @@
   zeile eins
  -zeile zwei
  +zeile zwei geaendert

and the file on disk really was changed.
2026-11-08 12:32:24 +00:00

110 lines
4.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""Every key used in the code must exist in every catalogue.
A UI that is half translated is worse than one that is not translated at all:
the operator cannot tell a missing string from a deliberate one. This walks the
sources for sp_t()/sp_td() keys and compares them with share/locale/*.msg.
"""
import pathlib
import re
import sys
ROOT = pathlib.Path(__file__).resolve().parent.parent
KEY = re.compile(r'sp_td?\(\s*"([^"]+)"')
def used_keys():
keys = set()
for path in list(ROOT.glob("src/*.c")) + list(ROOT.glob("plugins/*/*.c")):
keys.update(KEY.findall(path.read_text(encoding="utf-8", errors="replace")))
return keys
def catalogue(path):
out = {}
for line in path.read_text(encoding="utf-8").splitlines():
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
k, v = line.split("=", 1)
out[k.strip()] = v.strip()
return out
# Anything drawn on the screen must come from a catalogue. These are the calls
# that put text into a curses window; a literal there is a string the operator
# can never translate.
DRAW = re.compile(r'\b(mvwaddstr|mvwaddnstr|mvwprintw|waddstr|wprintw|winsstr)\s*\(')
LITERAL_ARG = re.compile(r',\s*"([^"\\]*(?:\\.[^"\\]*)*)"')
def hardcoded():
bad = []
for path in list(ROOT.glob("src/*.c")):
for no, line in enumerate(path.read_text(encoding="utf-8",
errors="replace").splitlines(), 1):
if not DRAW.search(line) or "sp_t" in line:
continue
m = LITERAL_ARG.search(line)
if m is not None:
bad.append(f"{path.name}:{no}: {m.group(1)!r}")
return bad
# Roles are keys too: add_entry() takes an identifier, the label comes from the
# catalogue. An unknown identifier would ship as untranslated text.
ROLE_CALL = re.compile(r'add_entry\(\s*&?[A-Za-z_>.-]*\s*,\s*"([a-z_]+)"')
ALLOWED_ROLES = {"you", "agent", "thinking", "tool", "error", "shell",
"splinter", "diff"}
def unknown_roles():
bad = []
for path in ROOT.glob("src/*.c"):
for no, line in enumerate(path.read_text(encoding="utf-8",
errors="replace").splitlines(), 1):
for role in ROLE_CALL.findall(line):
if role not in ALLOWED_ROLES:
bad.append(f"{path.name}:{no}: role {role!r} has no label")
return bad
def main():
used = used_keys()
# role.<x> is built by role_label(), never written literally
used.add("role.you"); used.add("role.agent"); used.add("role.thinking")
used.add("role.tool"); used.add("role.error"); used.add("role.shell")
used.add("role.splinter"); used.add("role.diff")
bad = 0
cats = sorted(ROOT.glob("share/locale/*.msg"))
if not cats:
print("check_i18n: no catalogues in share/locale", file=sys.stderr)
return 1
for path in cats:
cat = catalogue(path)
missing = sorted(k for k in used if k not in cat)
if missing:
bad = 1
print(f"{path.name}: missing {len(missing)} key(s): "
+ ", ".join(missing), file=sys.stderr)
# the English catalogue is the reference: everything in it must be used
en = catalogue(ROOT / "share/locale/en.msg")
unused = sorted(k for k in en if k not in used)
if unused:
print("en.msg: keys nothing uses: " + ", ".join(unused), file=sys.stderr)
roles = unknown_roles()
if roles:
bad = 1
print("unknown role identifiers:", file=sys.stderr)
for item in roles:
print(" " + item, file=sys.stderr)
lit = hardcoded()
if lit:
bad = 1
print("hardcoded text on screen (use sp_t/sp_td):", file=sys.stderr)
for item in lit:
print(" " + item, file=sys.stderr)
if bad == 0:
print(f"check_i18n: ok ({len(used)} keys in {len(cats)} catalogues)")
return bad
if __name__ == "__main__":
sys.exit(main())