import { I18nStrings, I18nValue, MtcuteI18nAdapter, MtcuteI18nFunction, OtherLanguageWrap, } from './types' import { createI18nStringsIndex, extractLanguageFromUpdate } from './utils' export * from './types' export { extractLanguageFromUpdate } from './utils' export interface MtcuteI18nParameters { /** * Primary language which will also be used as a fallback */ primaryLanguage: { /** * Two letter language code. */ name: string /** * Strings for the language. */ strings: Strings } otherLanguages?: Record> /** * Language that will be used if no language is specified * * Defaults to {@link primaryLanguage} */ defaultLanguage?: string /** * Adapter that will be used to extract language from the update. */ adapter?: MtcuteI18nAdapter } export function createMtcuteI18n( params: MtcuteI18nParameters, ): MtcuteI18nFunction { const { primaryLanguage, otherLanguages, defaultLanguage = primaryLanguage.name, adapter = extractLanguageFromUpdate as unknown as MtcuteI18nAdapter, } = params const indexes: Record> = {} const fallbackIndex = (indexes[primaryLanguage.name] = createI18nStringsIndex(primaryLanguage.strings)) if (otherLanguages) { Object.keys(otherLanguages).forEach((lang) => { indexes[lang] = createI18nStringsIndex(otherLanguages[lang] as I18nStrings) }) } if (!(defaultLanguage in indexes)) { throw new TypeError( 'defaultLanguage is not a registered language', ) } const tr = ( lang: Input | string | null, key: string, ...params: unknown[] ) => { if (lang === null) lang = defaultLanguage if (typeof lang !== 'string') { lang = adapter(lang) ?? defaultLanguage } const strings = indexes[lang] ?? fallbackIndex let val = strings[key] ?? fallbackIndex[key] ?? `[missing: ${key}]` if (typeof val === 'function') { val = val(...params) } return val } return tr as MtcuteI18nFunction }