M1.11: English message catalog for every UI string

ui/src/i18n: en.json (20 flat dotted keys) and t(key, params?) with
{name} interpolation; a missing key warns in dev and returns the key.
All template text, title/aria-label attributes, notices and frontend
error texts go through t. ESLint vue/no-bare-strings-in-template is an
error. Catalog tests: interpolation, missing key, every t() key exists,
no unused keys. Engine messages stay English (G14). Docs updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-16 06:39:42 +02:00
parent 851d438e2a
commit 7c0fb312f6
21 changed files with 273 additions and 37 deletions

22
ui/src/i18n/en.json Normal file
View file

@ -0,0 +1,22 @@
{
"app.statusBar.version": "{name} {version}",
"common.cancel": "Cancel",
"common.dismiss": "Dismiss",
"common.dontSave": "Don't Save",
"common.error.unexpectedResult": "unexpected result of {command}",
"common.save": "Save",
"common.toolWindow.hide": "Hide {title}",
"common.toolWindow.resize": "Resize {title}",
"common.toolWindow.show": "Show {title}",
"editor.closePrompt.question": "Save changes to {name} before closing?",
"editor.notice.changedOnDisk": "{name} changed on disk. Your unsaved changes are kept; saving overwrites the file.",
"editor.notice.removedOnDisk": "{name} was deleted on disk. Your text is kept; saving writes it again.",
"editor.saveFailed": "save failed",
"editor.tabs.close": "Close {name}",
"editor.tabs.label": "Editor tabs",
"editor.tabs.unsaved": "unsaved changes",
"project.empty.noFolder": "No folder is open.",
"project.openFolder": "Open Folder…",
"project.toolWindow.title": "Project",
"project.tree.loading": "Loading…"
}

57
ui/src/i18n/index.ts Normal file
View file

@ -0,0 +1,57 @@
/**
* Message catalog of the frontend.
*
* Every interface string comes from a catalog keyed by flat dotted ids
* grouped by area (`app.*`, `project.*`, `editor.*`, `common.*`). English
* (`en.json`) is the source catalog; other languages follow with G14.
* Messages that come from the engine (`EngineError.message`) are not in
* the catalog yet and stay English.
*/
import en from "./en.json";
/** A key of the English catalog. */
export type MessageKey = keyof typeof en;
/** Values for the `{name}` placeholders of a message. */
export type MessageParams = Readonly<Record<string, string | number>>;
/** The active catalog. */
const catalog: Readonly<Record<string, string>> = en;
/**
* Whether a key exists in the active catalog.
*
* @param key - the message key.
*/
export function hasMessage(key: string): boolean {
return Object.hasOwn(catalog, key);
}
/**
* Looks up a message and fills in its placeholders.
*
* A placeholder is a name in braces (`{name}`). A placeholder without a
* value in `params` stays as it is. A missing key never throws: it logs a
* warning in development builds and returns the key itself, so the gap is
* visible in the interface.
*
* @param key - the message key, for example `project.openFolder`.
* @param params - values for the placeholders.
* @returns the message text.
*/
export function t(key: string, params?: MessageParams): string {
const message = hasMessage(key) ? catalog[key] : undefined;
if (message === undefined) {
if (import.meta.env.DEV) {
console.warn(`[i18n] missing message key: ${key}`);
}
return key;
}
if (!params) {
return message;
}
return message.replace(/\{(\w+)\}/g, (placeholder, name: string) => {
const value = Object.hasOwn(params, name) ? params[name] : undefined;
return value === undefined ? placeholder : String(value);
});
}