Files
wadamesh/scripts/build/gen-lang-builtin.py
T
Kaj SchittecatandClaude Opus 5 eb439c8baa i18n: find the strings the audit could not see, and the apps it never checked
pisti87 reported a long list of text that stays English whatever the language,
and said the strings were in his language file and still did not appear (#257).
Both halves are true, and the reason is the audit.

The extractor only ever recognised TR("literal"). Three very common shapes were
therefore invisible:

  mk_row_btn("Reload tiles in view", cb)      // helper TR()s its parameter
  for (auto& r : rows) TR(r.label)            // literal lives in a local table
  TR(contactsSortOptName(m))                  // helper returns one of several

All three translate correctly at runtime, so the source looks properly wrapped.
But the literal at the call site was never emitted as a key, so it never entered
a .lang file, so no translator could ever supply it -- and adding it by hand
did nothing, because the audit's key list is what the files are checked against.
That is 51 strings across the map options sheet, the sort sheets, the contacts
filters and the home launcher.

The audit now understands all three, plus tr("...") in the Lua apps, and the
newly visible keys are in all thirteen files as placeholders so translators can
see them. 1017 keys, up from 966.

Four strings were genuinely raw and are now wrapped: the reader's idle status,
the Discover empty feed, the crash-report export button and Paste (move/copy).

Lua apps had no way to translate anything at all, so every built-in was hard
English regardless of the device language. wada.sys.tr() gives them the same
table the interface uses; airtime 1.4 is the first to use it, with the
`sys.tr or identity` fallback so it still runs on older firmware.

Two more instances of the drift this issue is really about:

  - gen-lua-builtin.py read out/firmware/apps/, which nothing writes -- the
    deploy rsyncs deploy/apps/ straight to the VPS. So the mirror was stale and
    the two apps added in beta_68 were never baked in: boards that cannot reach
    the Store shipped without them. It reads the canonical directory now, and
    regenerates from the same pre-build hook as the language table.
  - Baking a row whose translation equals its key does nothing, since TR()
    returns the key on a miss. Skipping them takes the header from 1.11 MB to
    939 KB and gives the V4 back 16 KB of flash, which matters at 89%.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-22 22:29:05 +02:00

119 lines
4.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Bake deploy/apps/lang/*.lang into a compiled-in fallback table.
Boards that can reach the Lua Store download their language as a file. Boards
that cannot (the V4: ~14 KB of free internal heap, so the net worker and the
store are unreliable there) still need translations, and before this they were
stuck on English.
This generates src/ui-touch/i18n_builtin.h from the SAME .lang files the store
serves, so the two can never drift: translators edit deploy/apps/lang/*.lang,
and both paths follow. Rows are emitted sorted by key so TR() can binary-search
them, and the table lives in .rodata — flash, not RAM.
Run from the repo root (a PlatformIO pre-build step does this automatically):
python3 scripts/build/gen-lang-builtin.py
"""
import glob, os, sys
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
LANGDIR = os.path.join(ROOT, 'deploy/apps/lang')
OUT = os.path.join(ROOT, 'src/ui-touch/i18n_builtin.h')
# Index order must match the UiLang enum in i18n.h.
CODES = ["en", "hu", "nl", "de", "fr", "es", "it",
"ru", "uk", "bg", "sr", "el", "pt-br", "ro"]
def c_escape(s):
out = []
for ch in s:
if ch == '\\': out.append('\\\\')
elif ch == '"': out.append('\\"')
elif ch == '\n': out.append('\\n')
elif ch == '\t': out.append('\\t')
elif ch == '\r': out.append('\\r')
elif ord(ch) < 0x20: out.append('\\x%02x' % ord(ch))
else: out.append(ch)
return ''.join(out)
def read_lang(path):
"""key -> value, with the file's own \\n/\\t/\\\\ escapes decoded."""
rows = {}
for line in open(path, encoding='utf-8'):
line = line.rstrip('\n').rstrip('\r')
if '\t' not in line: # header/comment lines carry no tab
continue
k, v = line.split('\t', 1)
def un(t):
o, i = [], 0
while i < len(t):
if t[i] == '\\' and i + 1 < len(t):
n = t[i + 1]
o.append('\n' if n == 'n' else '\t' if n == 't' else n)
i += 2
else:
o.append(t[i]); i += 1
return ''.join(o)
k, v = un(k), un(v)
if k and v:
rows[k] = v
return rows
def main():
langs = {}
for code in CODES[1:]: # English is the key itself
p = os.path.join(LANGDIR, code + '.lang')
if os.path.exists(p):
langs[code] = read_lang(p)
if not langs:
sys.exit('no .lang files found in ' + LANGDIR)
w = ['// Generated by scripts/build/gen-lang-builtin.py — DO NOT EDIT.',
'// Source: deploy/apps/lang/*.lang (the same files the Lua Store serves).',
'// Compiled-in fallback for boards that cannot rely on downloading a language.',
'#pragma once', '#include "i18n.h"', '']
for code in CODES[1:]:
rows = langs.get(code, {})
sym = 'kBuiltin_' + code.replace('-', '_')
w.append('static const I18nPair %s[] = {' % sym)
# A row whose translation IS the key does nothing: TR() already returns the
# key when it finds no match. The .lang files carry those deliberately, as
# placeholders telling translators the string exists, but baking them costs
# flash on exactly the board that needs the flash (the V4 sits at ~90%).
for k in sorted(rows): # sorted: TR() binary-searches
if rows[k] == k:
continue
w.append(' { "%s", "%s" },' % (c_escape(k), c_escape(rows[k])))
w.append('};')
w.append('')
w.append('static const I18nPair* const kBuiltinLang[LANG_COUNT] = {')
w.append(' nullptr,') # LANG_EN: the key is the English
for code in CODES[1:]:
sym = 'kBuiltin_' + code.replace('-', '_')
w.append(' %s,' % (sym if langs.get(code) else 'nullptr'))
w.append('};')
w.append('static const int kBuiltinLangCount[LANG_COUNT] = {')
w.append(' 0,')
for code in CODES[1:]:
sym = 'kBuiltin_' + code.replace('-', '_')
w.append(' %s,' % (('(int)(sizeof(%s)/sizeof(%s[0]))' % (sym, sym))
if langs.get(code) else '0'))
w.append('};')
w.append('')
open(OUT, 'w', encoding='utf-8').write('\n'.join(w))
total = sum(sum(1 for k, v in r.items() if v != k) for r in langs.values())
print('%s: %d languages, %d translated rows, %d bytes' %
(os.path.relpath(OUT, ROOT), len(langs), total, os.path.getsize(OUT)))
if __name__ == '__main__':
main()