Skip to content

Localization

How a user-visible string gets from a key in a locale bundle onto a pilot's screen, and what a call has to look like for the precompiler to bake the answer into the marker.

The one fact everything else follows from

A packaged card carries no locale bundle. bin/package/build_package.py copies src/rfsuite/ into the package while skipping i18n/, then puts back i18n/init.lua alone. So on an installed card i18n/init.lua asks for a <lang>.lua that is not there, the bundle falls through empty, and i18n.t(key) answers with the key itself.

Everything below is a consequence. A string a pilot reads is resolved at package time, into the marker the packager substitutes — and the text a pilot actually sees in a locale the packager had no bundle for is the fallback that went into that marker. A call the precompiler did not rewrite keeps no marker, so on the card it is a runtime lookup that finds nothing, takes its English fallback, and says English — on a build whose German was sitting in de.lua the whole time.

This is also why the failure is invisible on a desktop. In the simulator the bundle is present, the runtime lookup succeeds, and the page is correct.

The two scripts

  • .vscode/scripts/precompile_i18n.py rewrites the calls a page builds dynamically into @i18n(key|fallback)@ markers
  • .vscode/scripts/resolve_i18n_tags.py substitutes those markers from one locale bundle

The three shapes

These three are what the page-local family needs, and the whole of it sits inside the if prefix: block of precompile_i18n.py:40. They are the shapes a key a page looks up under its own namespace has to have. The full-key forms further down need neither the call's name nor a prefix, and are listed beside them so a call that already works is not renamed to fit.

1. The call is named t or pageText

The precompiler's pattern knows those two names and Common.t for the cross-page form. A wrapper called anything else is never matched, whatever else is true of the file — and it is the wrapper's call sites that get rewritten, not the wrapper. A hand-rolled tr, a pageHelpText that forwards to pageText, or a translator handed in as a parameter under a third name all leave their call sites unrewritten. A help text the row may lack is still a plain pageText(i18n, "help_key") — with no fallback, see §3.

The first argument is not used by the marker, but the pattern has to recognise it: i18n, <name>.i18n, nil, or the guarded ctx and ctx.i18n.

2. The file offers a prefix to derive

The precompiler takes the prefix from a pageT("...") or Common.pageT("...") call in the same file, from a keyPrefix local, or from a buildSimplePage second argument. Without one, the whole rewrite block is skipped for that file — which is why a help.lua with no pageT call can have translated keys and still ship English.

3. The fallback is one string literal

In this family the pattern reads the fallback out of the call, up to its closing parenthesis, so it has to be a single literal. A .. chain spread over several lines is not a string it can take, and is skipped. Neither is a value in a variable: passing one leaves the key unrewritten, which is worse than an English fallback, because the key is then lost rather than merely untranslated. A chain that fits on one line is a different case and is matched — the pattern takes the source text between the quotes, .. included, as the fallback, and for a key in neither bundle the resolver then writes that source text into the packaged string. There is no such call in the tree today. The full-key forms that take their fallback after or behave differently; see A chain or a variable after or below.

A call with no fallback at all is fine only while the key exists in en.lua. The resolver tries the chosen bundle, then en.lua, then the inline fallback (resolve_i18n_tags.py:437-452) — so a key present in en.lua but missing from the target bundle still comes out in English rather than untranslated. A key in neither bundle and with no fallback has nothing left: the marker stays a raw @i18n(...)@, shows up that way on the radio, and is reported under the resolver's unresolved keys — and since #286 the resolver exits 1 on it, so the packager refuses to write an archive at all. A build that reaches such a key produces no ZIP rather than a card full of markers.

Whether that applies to a call that does carry a fallback depends on the form, because the precompiler puts the fallback inside the marker for some shapes and leaves it outside for others. Measured on this tree, against the real en.lua, with a key in neither bundle:

Call Precompiled to Build
tr("a.b", "Fallback") "@i18n(a.b\|Fallback)@" finishes — the fallback is resolved and the pilot reads it
tr and tr("a.b") or "Fallback" "@i18n(a.b\|Fallback)@" finishes, same reason
t(i18n, "key", "Fallback") (page-local, with a prefix) "@i18n(app.pages.<page>.key\|Fallback)@" finishes, same reason
i18n.t("a.b") or "Fallback" "@i18n(a.b)@" or "Fallback" stops, exit 1 — the marker has no fallback, so the key is still needed
i18n.t("a.b") "@i18n(a.b)@" stops, exit 1

So the rule is not "always add the key". It is: a fallback that the precompiler carried into the marker is enough for the build to finish; a fallback left beside the marker is not, and for the i18n.t(...) forms the key in en.lua is what lets the build through. The fallback is what the pilot reads either way — which is the other half of why it is worth writing down.

A build that finished on a fallback still says so. The resolver counts every key that only its inline fallback answered, prints the count on its DONE line (keys in no bundle: N) and lists the keys under keys in no bundle, resolved from the call's own fallback. The exit status does not change. A key on that list reaches every locale in English: add it to en.lua and the other bundles. The resolver keeps no texts of its own behind the bundles (its ALIASES only rename a key before the lookup): a key the bundles do not have is answered by its own fallback or not at all.

The full-key forms

These sit after the prefix block, so a call in this family is rewritten with no prefix and no particular name — it spells the whole key out and is done. Renaming one of these to t or pageText would be a mistake, not a fix.

Form Where Needs
tr("a.b", "Fallback") precompile_i18n.py:202 a key containing a dot
tr and tr("key") or "Fallback" :191 nothing but the shape
(t and t("widgets.dashboard.key")) or "Fallback" :158 nothing but the shape
i18n.t("key") :213 nothing; carries no fallback
i18n and i18n.t and i18n.t("key") or "Fallback" :180 nothing but the shape
the titleKey/labelKey/valueKey pairs :80, :91, :105 a prefix (inside the block)

In this tree precompiling adds 357 markers in 54 files that have no derivable prefix. One example is ui/home.lua:1216, tr("app.msp.unsupported_title", "Unsupported MSP API"): it is named tr, its file offers no prefix, and it is German in the package.

A chain or a variable after or

The three forms that take their fallback after or (:158, :180, :191) end it at the first closing quote, with no parenthesis after it to make the pattern look further. So a .. chain there is neither skipped nor read whole, on one line or on several: the marker takes the first literal, and the rest of the chain stays behind it in the source.

local m = i18n and i18n.t and i18n.t("a.b") or "first part "
  .. "rest of it."
-- precompiled:
local m = "@i18n(a.b|first part )@"
  .. "rest of it."

On the card the pilot then reads the translated text with the English remainder appended.

A fallback held in a local keeps the lines short instead: i18n and i18n.t and i18n.t("a.b") or fallback. The or pattern does not match it, :213 rewrites the call to "@i18n(a.b)@", and or fallback stays beside the marker, as in the i18n.t("a.b") or "Fallback" row of the table above. The key must then exist in en.lua: without it the resolver reports it as unresolved and the build stops. That is the i18n.t family only: :169 and :202 both require a second argument, so with a local tr("a.b") and t("widgets.dashboard.key") match no pattern at all and stay unrewritten — the key is then lost rather than merely untranslated, which §3 calls the worse outcome. For :158 and :191 use a single flat literal, which the marker carries whole (the tr and tr("key") or "Fallback" row above). A single flat literal keeps the fallback inside the marker and the key optional, at the cost of a long line (What a single literal costs).

The house form

What every other help sheet already does, and what satisfies all three:

local function loadModule(path)
  local fullPath = "/SCRIPTS/RFSUITE/core/" .. path
  local chunk = loadScript(fullPath, "t")
  if type(chunk) ~= "function" then return nil end
  local ok, mod = pcall(chunk)
  if not ok then return nil end
  return mod
end

return function(ctx)
  local Common = loadModule("app/pages/settings/common.lua")
  local t = Common and Common.pageT("<page_prefix>") or function(_, _, fb) return fb end
  local i18n = ctx.i18n

  local someText = t(i18n, "some_key", "The English fallback.")

Common.pageT(pageKey) builds app.pages.<pageKey>., so the namespace a page's keys live in is the page prefix itself — which is why a help.lua that hard-codes its own namespace and a page that uses pageT can share a block without either moving a key.

Common is loaded inside the returned function, never at file scope: loadScript only resolves on the radio, so a file-scope load would fail in the desktop sources.

A module that borrows another page's block

keyPrefix is for a file that has no page of its own — a shared library, say — and takes the full form, because the precompiler strips the leading app.pages. back off:

local keyPrefix = "app.pages.<the_page_that_owns_the_block>"

Name the block the translation is really in, not the caller's. A shared module that takes a translator from its caller will look the key up under each caller's own prefix, and a key that lives in one block is then found by one caller and missed by every other. That is not a packaging defect: it is wrong in the source build too, and fixing it in the marker fixes it for everyone.

The CI gate, and what it does not cover

bin/i18n/check_translations.py reports three populations, all held at a baseline so that a number growing fails the build:

  • untranslated — a literal in a field a pilot reads that never reaches the translator
  • assembled — a key built at runtime, e.g. "app.pages." .. pageKey .. "." .. field
  • not rewritten — a call with a literal key that is still a call after precompile_i18n.py has run over a copy of the tree: pageText, t, or any wrapper declared as function name(i18n, key, ...) with the key as its second argument, and t or tr with the key as its first

The assembled rule reads the call shapes that take the key as their first argument (t(, tr(, i18n.t() and skips a first argument that is a bare name or an i18n value, on the reasoning that the precompiler rewrites those call sites. The not rewritten count is what checks that reasoning for the page-local family: it runs the tree's own precompiler rather than a copy of its patterns, so a wrapper under a third name, a first argument the pattern does not know, or a fallback held in a variable shows up there. Its baseline is the page titles that pass a variable fallback, pageText(i18n, "title", displayTitle), and the developer API tester's tr("key", "Fallback") calls, whose keys carry no dot and so match no full-key form.

bin/accounting/measure.lua is not part of this: it gates the dashboard and service sweep, and none of the i18n scripts.

DEVELOPMENT.md § Continuous integration lists the jobs; the precompiler's own test is .vscode/scripts/test_precompile_i18n.py.

Which language the radio runs in

The language a runtime lookup or an announcement uses is resolved by lib/system_locale.lua, resolveSystemLanguage(defaultLang), in this order:

  1. the pilot's choice under Settings → Localization, the localizations.language key of the radio-wide preferences.lua (absent means Automatic);
  2. on a packaged build, the package's language (.vscode/scripts/resolve_i18n_tags.py writes it over the @i18n_language@ marker); from source or in the simulator, the radio's own language (getGeneralSettings().language);
  3. the caller's defaultLang.

Only de and en are recognised: a radio in another language falls through to step 3, and a package built for another language answers en. The answer is kept for two seconds (200 ticks of getTime()) for callers asking with the same defaultLang, because a theme's t() asks on every call. The dashboard's preferences reload ends that early: invalidateLanguage() drops the answer and moves localeGeneration, which the theme commons and the object layer check before reusing their own memo. The reload hands over the language the settings it has just read resolve to (resolveFromPreferences, the same order with step 1 taken from those settings), so the widget's i18n context is built without reading preferences.lua a second time.

What a single literal costs

A long help text is joined up with .. to stay under the 140 characters .luacheckrc names, and the precompiler cannot read that: the page-local form skips the call, and the forms after or take the first literal only. Flat, it is one line of roughly 280. The tree already carries 992 lines over 140 under src/ (luacheck src, the longest 870) and CI does not enforce the line length (its luacheck job checks undeclared globals only), so the trade falls on correctness. Do not reach for re.DOTALL instead: it does match a joined-up call, and then bakes the .. chain into the marker as text, so the packaged fallback carries source code rather than a sentence. That is worse than the English it replaced.

Checking your work

Run the precompiler on a copy, not on your checkout. It rewrites every file it changes in place — process_file writes each one back (precompile_i18n.py:232-237) and the script takes only --root, with no dry-run. On a copy of this tree it changed 189 files and took the @i18n( count from 549 to 2883, which is the size of the job, not a sign that anything is wrong. The packager does exactly this, in its staging directory, for the same reason.

python .vscode/scripts/precompile_i18n.py --root src

A call that produced a marker is now a single string literal beginning "@i18n(; a call that did not is still a call. For the end-to-end answer, build the locale and read the ZIP:

python bin/package/build_package.py --lang de --artifact-version PR-999 --output-dir <dir>

A pilot's string is German in the packaged file, not English with a German key beside it.