import type { ElementType } from 'react'; import type { Dictionary } from '../localization/locales'; /** The dictionary of localized strings */ export type LocalizerDictionary = Dictionary; /** A localization dictionary key */ export type LocalizerToken = keyof Dictionary; /** A dynamic argument that can be used in a localized string */ type DynamicArg = string | number; type DynamicArgStr = 'string' | 'number'; /** A record of dynamic arguments for a specific key in the localization dictionary */ export type ArgsRecord = Record, DynamicArg>; // TODO: create a proper type for this export type DictionaryWithoutPluralStrings = Dictionary; export type PluralKey = 'count'; export type PluralString = `{${string}, plural, one [${string}] other [${string}]}`; type ArgsTypeStrToTypes = T extends 'string' ? string : T extends 'number' ? number : never; // those are still a string of the type "string" | "number" and not the typescript types themselves type ArgsFromTokenStr = Dictionary[T]['args'] extends undefined ? never : Dictionary[T]['args']; type ArgsFromToken = MappedToTsTypes>; type IsTokenWithCountArgs = 'count' extends keyof ArgsFromToken ? true : false; /** The arguments for retrieving a localized message */ export type GetMessageArgs = T extends LocalizerToken ? ArgsFromToken extends never ? [T] : [T, ArgsFromToken] : never; type MappedToTsTypes> = { [K in keyof T]: ArgsTypeStrToTypes; }; /** Basic props for all calls of the Localizer component */ type LocalizerComponentBaseProps = { token: T; asTag?: ElementType; className?: string; }; /** The props for the localization component */ export type LocalizerComponentProps = T extends LocalizerToken ? ArgsFromToken extends never ? LocalizerComponentBaseProps : ArgsFromToken extends Record ? LocalizerComponentBaseProps : LocalizerComponentBaseProps & { args: ArgsFromToken } : never; export type LocalizerComponentPropsObject = LocalizerComponentProps; export type I18nMethods = { /** @see {@link window.i18n.stripped} */ stripped: ( ...[token, args]: GetMessageArgs ) => R | T; /** @see {@link window.i18n.inEnglish} */ inEnglish: ( ...[token, args]: GetMessageArgs ) => R | T; /** @see {@link window.i18n.formatMessageWithArgs */ getRawMessage: ( ...[token, args]: GetMessageArgs ) => R | T; /** @see {@link window.i18n.formatMessageWithArgs} */ formatMessageWithArgs: ( rawMessage: R, args?: ArgsRecord ) => R; }; export type SetupI18nReturnType = I18nMethods & (( ...[token, args]: GetMessageArgs ) => R);