Skip to content

Add localization system#1

Merged
humanapp merged 2 commits into
mainfrom
humanapp/loc
Jul 23, 2026
Merged

Add localization system#1
humanapp merged 2 commits into
mainfrom
humanapp/loc

Conversation

@humanapp

Copy link
Copy Markdown
Collaborator

Add build-time localization runtime

Adds the localization foundation for the microbit-apps stack: English source
strings are both the catalog keys and the runtime fallback, and exactly one
language ships per build image.

What changed

  • loc.ts (new): the _loc namespace holds two seams assigned at startup
    by a consuming app's generated file: table (source string -> translation)
    and defaultFont. Public API:
    • ui.loc(s): table lookup with fallback to the source string; identity
      when no table is assigned, so English builds pay no per-string cost.
    • ui.locc(context, s): context-disambiguated lookup under the catalog key
      context#string, falling back to the plain-string translation, then to
      the source string. For the rare case where one English word needs
      different translations in different places.
    • ui.locf(s, args): positional {0}-style interpolation applied after
      translation, so translations can reorder placeholders.
    • ui.locFont(): the per-language default font, or bitmaps.font8.
  • pxt.json: loc.ts registered immediately after ns.ts so the
    runtime initializes before any file that calls it.
  • test.ts: runLocTest() covering the identity path, table hit and
    miss, the full locc fallback chain, reordered-placeholder interpolation,
    and locFont default/assigned behavior, with state resets between groups.
  • locales/en.json + scripts/locstrings.mjs: the source-string catalog
    (empty; the library has no user-visible strings of its own) and the
    extractor that regenerates it from ui.loc(...) call sites. Arguments must
    be string literals; anything else is reported loudly. locstrings-ignore
    on a call's line suppresses deliberate dynamic pass-throughs, and the
    file implementing the loc API itself is skipped.
  • package.json: npm run loc:strings.

Behavior

No behavior change for existing consumers: nothing in ui-core calls the new
API, and with no table assigned every function is identity/fallback.
UiAssetResolver.getText is unchanged; the two mechanisms compose (a
resolver implementation may return ui.loc(...) results).

Verification

  • mkc build green, including test files.
  • Test assertions executed in the simulator.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a build-time localization runtime to ui-core, establishing a minimal translation mechanism (source-string keys with English fallback) plus an extraction script to build the English catalog from ui.loc* call sites. This sets up the localization foundation for consuming microbit-apps packages while keeping English builds fast by omitting the translation table.

Changes:

  • Introduces ui.loc, ui.locc, ui.locf, and ui.locFont backed by an app-assigned _loc.table and _loc.defaultFont.
  • Adds a catalog extraction script (scripts/locstrings.mjs) and an empty locales/en.json, plus an npm script to regenerate the catalog.
  • Adds a localization test harness and wires loc.ts into pxt.json file ordering.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
loc.ts Adds the localization runtime API (loc, locc, locf, locFont) and _loc seams for app-provided table/font.
test.ts Adds runLocTest() coverage for identity behavior, table hits/misses, locc fallback chain, interpolation, and default/custom font.
scripts/locstrings.mjs Adds a source scanner to regenerate locales/en.json by extracting literal strings from ui.loc* call sites.
locales/en.json Adds the generated English-source catalog (currently empty).
package.json Adds npm run loc:strings to run the extractor.
pxt.json Registers loc.ts early in the build file list and rearranges existing keys.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread loc.ts
Comment thread scripts/locstrings.mjs Outdated
@humanapp
humanapp merged commit c8b6fcd into main Jul 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants