diff --git a/ts/types/Localizer.ts b/ts/types/Localizer.ts new file mode 100644 index 000000000..4e2242520 --- /dev/null +++ b/ts/types/Localizer.ts @@ -0,0 +1,31 @@ +import type { Dictionary } from '../localization/locales'; + +/** A localization dictionary key */ +type Token = keyof Dictionary; + +/** A dynamic argument that can be used in a localized string */ +type DynamicArg = string | number; + +/** A record of dynamic arguments for a specific key in the localization dictionary */ +type ArgsRecord = Record, DynamicArg>; + +/** The dynamic arguments in a localized string */ +type DynamicArgs = + // eslint-disable-next-line @typescript-eslint/no-unused-vars + LocalizedString extends `${infer _Pre}{${infer Var}}${infer Rest}` + ? Var | DynamicArgs + : never; + +/** The arguments for retrieving a localized message */ +export type GetMessageArgs = + DynamicArgs extends never ? [T] : [T, ArgsRecord]; + +/** The props for the localization component */ +export type I18nProps = + DynamicArgs extends never ? { token: T } : { token: T; args: ArgsRecord }; + +/** The dictionary of localized strings */ +export type LocalizerDictionary = Dictionary; + +/** A localization dictionary key */ +export type LocalizerToken = keyof LocalizerDictionary;