Files
jobtrackingapp/job-tracker-ui/src/dialogText.ts
T

13 lines
816 B
TypeScript

import { interpolate, type TranslationParams } from "./i18n/I18nProvider";
import { translations, type TranslationKey } from "./i18n/translations";
type DialogParams = Record<string, string | number | { key: TranslationKey }>;
export type DialogText = string | { key: TranslationKey; params?: DialogParams };
export function resolveDialogText(text: DialogText, t?: (key: TranslationKey, params?: TranslationParams) => string): string {
if (typeof text === "string") return text;
const translate = t ?? ((key: TranslationKey, params?: TranslationParams) => interpolate(translations.en[key], params));
const params = text.params && Object.fromEntries(Object.entries(text.params).map(([name, value]) => [name, typeof value === "object" ? translate(value.key) : value]));
return translate(text.key, params);
}