import type { ElementType } from 'react'; import type { Dictionary } from '../localization/locales'; import { LOCALE_DEFAULTS } from '../session/constants'; /** 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>; export type PluralKey = 'count'; export type PluralString = `{${string}, plural, one {${string}} other {${string}}}`; /** The dynamic arguments in a localized string */ type DynamicArgs = /** If a string follows the plural format use its plural variable name and recursively check for * dynamic args inside all plural forms */ LocalizedString extends `{${infer PluralVar}, plural, one {${infer PluralOne}} other {${infer PluralOther}}}` ? PluralVar | DynamicArgs | DynamicArgs : /** If a string segment has follows the variable form parse its variable name and recursively * check for more dynamic args */ LocalizedString extends `${infer _Pre}{${infer Var}}${infer Rest}` ? Var | DynamicArgs : never; type ArgsRecordExcludingDefaults = Omit< ArgsRecord, keyof typeof LOCALE_DEFAULTS >; /** The arguments for retrieving a localized message */ export type GetMessageArgs = T extends Token ? DynamicArgs extends never ? [T] : ArgsRecordExcludingDefaults extends Record ? [T] : [T, ArgsRecordExcludingDefaults] : never; /** Basic props for all calls of the I18n component */ type I18nBaseProps = { token: T; as?: ElementType }; /** The props for the localization component */ export type I18nProps = T extends Token ? DynamicArgs extends never ? I18nBaseProps : ArgsRecordExcludingDefaults extends Record ? I18nBaseProps : I18nBaseProps & { args: ArgsRecordExcludingDefaults } : never; /** The dictionary of localized strings */ export type LocalizerDictionary = Dictionary; /** A localization dictionary key */ export type LocalizerToken = keyof LocalizerDictionary;