/** * 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>; /** The active catalog. */ const catalog: Readonly> = 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); }); }