Add localization system#1
Merged
Merged
Conversation
There was a problem hiding this comment.
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, andui.locFontbacked by an app-assigned_loc.tableand_loc.defaultFont. - Adds a catalog extraction script (
scripts/locstrings.mjs) and an emptylocales/en.json, plus an npm script to regenerate the catalog. - Adds a localization test harness and wires
loc.tsintopxt.jsonfile 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_locnamespace holds two seams assigned at startupby 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; identitywhen no table is assigned, so English builds pay no per-string cost.
ui.locc(context, s): context-disambiguated lookup under the catalog keycontext#string, falling back to the plain-string translation, then tothe 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 aftertranslation, so translations can reorder placeholders.
ui.locFont(): the per-language default font, orbitmaps.font8.pxt.json:loc.tsregistered immediately afterns.tsso theruntime initializes before any file that calls it.
test.ts:runLocTest()covering the identity path, table hit andmiss, the full
loccfallback chain, reordered-placeholder interpolation,and
locFontdefault/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 mustbe string literals; anything else is reported loudly.
locstrings-ignoreon 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.getTextis unchanged; the two mechanisms compose (aresolver implementation may return
ui.loc(...)results).Verification
mkc buildgreen, including test files.