Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
import { en } from "./dictionaries/en";
|
|
import type { Messages } from "./dictionaries/en";
|
|
|
|
/**
|
|
* Dotted key paths into the message catalog, e.g. `"landing.heroTitle"`.
|
|
* Derived from the English master so keys are checked at compile time.
|
|
*/
|
|
export type TranslationKey = PathsOf<Messages>;
|
|
|
|
type PathsOf<T> = {
|
|
[K in keyof T & string]: T[K] extends string
|
|
? K
|
|
: `${K}.${PathsOf<T[K]>}`;
|
|
}[keyof T & string];
|
|
|
|
/** Interpolation values substituted into `{placeholder}` slots. */
|
|
export type TranslationVars = Readonly<Record<string, string | number>>;
|
|
|
|
/** The translate function returned by {@link createTranslator}. */
|
|
export type Translator = (key: TranslationKey, vars?: TranslationVars) => string;
|
|
|
|
function resolve(source: unknown, key: string): string | undefined {
|
|
let node: unknown = source;
|
|
for (const segment of key.split(".")) {
|
|
if (node && typeof node === "object" && segment in node) {
|
|
node = (node as Record<string, unknown>)[segment];
|
|
} else {
|
|
return undefined;
|
|
}
|
|
}
|
|
return typeof node === "string" ? node : undefined;
|
|
}
|
|
|
|
function interpolate(template: string, vars?: TranslationVars): string {
|
|
if (!vars) return template;
|
|
return template.replace(/\{(\w+)\}/g, (match, name: string) => {
|
|
const value = vars[name];
|
|
return value === undefined ? match : String(value);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Build a translator bound to a locale's catalog. Resolution order is:
|
|
* active catalog → English fallback → the raw key. A missing translation can
|
|
* therefore never render blank or throw — it degrades to English, then to the
|
|
* key itself, so surfaces stay legible while a locale is still being filled in.
|
|
*/
|
|
export function createTranslator(messages: Messages): Translator {
|
|
return (key, vars) => {
|
|
const value = resolve(messages, key) ?? resolve(en, key) ?? key;
|
|
return interpolate(value, vars);
|
|
};
|
|
}
|