i18n: dependency-free message runtime, catalog checks, plugin strings, native strings

- runtime/i18n.ts: ICU-lite messages (placeholders as text, Intl.PluralRules plurals with exact
  forms, selectordinal, select, number/date/time), fallback chains (pt-BR -> pt -> en, ar-x-fm ->
  ar), X and legacy code aliases, RTL by locale, lazy catalogs, live onChange. Apostrophes are
  literal except right before { } # or another apostrophe.
- runtime/i18nCheck.ts: rules for contributed catalogs (shape, keys, syntax, placeholder parity,
  select options, markup/control characters, lengths). An argument English only uses to choose a
  plural form may be dropped; missing plural forms are warnings; meta "allowEmpty" fragments.
- runtime/pluginI18n.ts: api.i18n bound per plugin (register/t/locale/dir/onChange/number/date/
  relative/list); the UI kit's own words follow the app's language. pageLang.ts reports the
  site's <html lang>. shell/messages.ts: shell-model words.
- waybetter-host i18n.rs: NativeStrings (validated plain text, caps, one-pass {name} fill, page
  payload by prefix), system_locales via sys-locale; TabState.lang.
- JSON Schemas for catalogs and translator notes; npm exports waybetter/i18n, /i18n/check,
  /i18n/plugin.
This commit is contained in:
CRTkafa 2026-09-29 02:53:48 +00:00
parent 4a294b993a
commit 929abd3278
18 changed files with 1461 additions and 27 deletions

View file

@ -0,0 +1,29 @@
// The shell models' own words (account labels, the update chip) in English, with `{name}`
// placeholders. They live in the framework catalogs (`waybetter/locales/<bcp47>.json`, `wb.*`), so
// every wayBetter app shows them in its language: pass a `text(key)` translator to the models; it
// returns the translated message (placeholders not yet filled) or undefined for English.
export const SHELL_MESSAGES = {
'wb.account.notSignedIn': 'Not signed in yet',
'wb.account.signedOut': '{handle} · signed out',
'wb.account.newName': 'New account',
'wb.update.readyLabel': 'Update ready · restart',
'wb.update.readyTitle': 'Restart to update to {product} {version}. Tabs are restored.',
'wb.update.installingLabel': 'Updating…',
'wb.update.installingTitle': 'Installing {product} {version}',
'wb.update.problemLabel': 'Update problem',
'wb.update.problemTitle': 'The last update did not start correctly. Download the current version.',
} as const;
export type ShellKey = keyof typeof SHELL_MESSAGES;
/** A translator for the shell models: the message for `key` (unformatted) or undefined for English. */
export type ShellText = (key: ShellKey) => string | undefined;
/** The message for `key` with `{name}` placeholders filled in (values inserted as text). */
export function shellText(key: ShellKey, params: Record<string, string> = {}, text?: ShellText): string {
let message: string = SHELL_MESSAGES[key];
try {
const translated = text?.(key);
if (typeof translated === 'string' && translated) message = translated;
} catch { /* English */ }
return message.replace(/\{(\w+)\}/g, (whole, name: string) => (name in params ? params[name]! : whole));
}

View file

@ -1,6 +1,8 @@
// Pure model for account profiles (no Tauri imports, so it is unit-tested under node).
// Mirrors `ProfileSettings` in waybetter-host `profiles.rs`; Rust validates again on save.
import { shellText, type ShellText } from './messages.ts';
/** The login inside a profile, as native code last saw it (read-only here; see profiles.rs). */
export type Account = { handle: string; name: string; avatar?: string; signedIn: boolean };
export type Profile = { id: string; name: string; color: string; account?: Account };
@ -60,11 +62,12 @@ export function normalizeAccount(value: unknown, rules: AccountRules): Account |
}
/** What the switcher shows for a profile: the login once seen, the profile name before. */
export function accountLabel(profile: Profile, rules: Pick<AccountRules, 'handlePrefix'>): { title: string; detail: string; state: 'signed-in' | 'signed-out' | 'new' } {
if (!profile.account) return { title: profile.name, detail: 'Not signed in yet', state: 'new' };
export function accountLabel(profile: Profile, rules: Pick<AccountRules, 'handlePrefix'>, text?: ShellText): { title: string; detail: string; state: 'signed-in' | 'signed-out' | 'new' } {
if (!profile.account) return { title: profile.name, detail: shellText('wb.account.notSignedIn', {}, text), state: 'new' };
const handle = `${rules.handlePrefix}${profile.account.handle}`;
return {
title: profile.account.name,
detail: `${rules.handlePrefix}${profile.account.handle}${profile.account.signedIn ? '' : ' · signed out'}`,
detail: profile.account.signedIn ? handle : shellText('wb.account.signedOut', { handle }, text),
state: profile.account.signedIn ? 'signed-in' : 'signed-out',
};
}
@ -80,8 +83,8 @@ export function nextColor(settings: ProfileSettings): string {
/** Adds a profile and makes it active. Unlimited count (Rust caps absurd sizes). Its name follows the
* account once someone signs in there; until then it is a placeholder. */
export function addProfile(settings: ProfileSettings, name = '', id = newProfileId()): ProfileSettings {
const clean = cleanName(name) ?? 'New account';
export function addProfile(settings: ProfileSettings, name = '', id = newProfileId(), text?: ShellText): ProfileSettings {
const clean = cleanName(name) ?? shellText('wb.account.newName', {}, text);
if (!validProfileId(id) || settings.profiles.some(p => p.id === id)) throw new Error('VALIDATION: invalid profile id');
return { profiles: [...settings.profiles, { id, name: clean, color: nextColor(settings) }], active: id };
}

View file

@ -1,5 +1,7 @@
// Pure model for the titlebar update chip (no Tauri imports, so it is unit-tested under node).
import { shellText, type ShellText } from './messages.ts';
/** Mirrors `UpdateStatus` in waybetter-host `update.rs` (serde tag "state"). */
export type UpdateStatus =
| { state: 'idle' | 'checking' | 'upToDate' }
@ -34,11 +36,11 @@ export function parseUpdateStatus(value: unknown): UpdateStatus {
}
/** Unobtrusive: nothing while checking/downloading/erroring; a chip only when action is useful. */
export function chipFor(status: UpdateStatus, product: string): UpdateChip | null {
export function chipFor(status: UpdateStatus, product: string, text?: ShellText): UpdateChip | null {
switch (status.state) {
case 'ready': return { kind: 'restart', label: 'Update ready · restart', title: `Restart to update to ${product} ${status.version}. Tabs are restored.` };
case 'installing': return { kind: 'installing', label: 'Updating…', title: `Installing ${product} ${status.version}` };
case 'problem': return { kind: 'problem', label: 'Update problem', title: 'The last update did not start correctly. Download the current version.', href: status.downloadUrl };
case 'ready': return { kind: 'restart', label: shellText('wb.update.readyLabel', {}, text), title: shellText('wb.update.readyTitle', { product, version: status.version }, text) };
case 'installing': return { kind: 'installing', label: shellText('wb.update.installingLabel', {}, text), title: shellText('wb.update.installingTitle', { product, version: status.version }, text) };
case 'problem': return { kind: 'problem', label: shellText('wb.update.problemLabel', {}, text), title: shellText('wb.update.problemTitle', {}, text), href: status.downloadUrl };
default: return null;
}
}