2022-07-19 02:47:59 +03:00
|
|
|
import { MtArgumentError, ParsedUpdate } from '@mtcute/client'
|
2022-08-29 18:53:27 +03:00
|
|
|
import {
|
|
|
|
I18nValue,
|
|
|
|
MtcuteI18nAdapter,
|
|
|
|
MtcuteI18nFunction,
|
|
|
|
OtherLanguageWrap,
|
|
|
|
} from './types'
|
2022-07-19 02:47:59 +03:00
|
|
|
import { createI18nStringsIndex, extractLanguageFromUpdate } from './utils'
|
|
|
|
|
2022-08-24 23:37:26 +03:00
|
|
|
export * from './types'
|
|
|
|
|
2022-08-29 18:53:27 +03:00
|
|
|
export interface MtcuteI18nParameters<Strings, Input> {
|
2022-07-19 02:47:59 +03:00
|
|
|
/**
|
|
|
|
* 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<string, OtherLanguageWrap<Strings>>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Language that will be used if no language is specified
|
|
|
|
*
|
|
|
|
* Defaults to {@link primaryLanguage}
|
|
|
|
*/
|
|
|
|
defaultLanguage?: string
|
2022-08-29 18:53:27 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adapter that will be used to extract language from the update.
|
|
|
|
*/
|
|
|
|
adapter?: MtcuteI18nAdapter<Input>
|
2022-07-19 02:47:59 +03:00
|
|
|
}
|
|
|
|
|
2022-08-29 18:53:27 +03:00
|
|
|
export function createMtcuteI18n<Strings, Input>(
|
|
|
|
params: MtcuteI18nParameters<Strings, Input>
|
|
|
|
): MtcuteI18nFunction<Strings, Input> {
|
2022-07-19 02:47:59 +03:00
|
|
|
const {
|
|
|
|
primaryLanguage,
|
|
|
|
otherLanguages,
|
|
|
|
defaultLanguage = primaryLanguage.name,
|
2022-08-29 18:53:27 +03:00
|
|
|
adapter = extractLanguageFromUpdate as any as MtcuteI18nAdapter<Input>,
|
2022-07-19 02:47:59 +03:00
|
|
|
} = params
|
|
|
|
|
|
|
|
const indexes: Record<string, Record<string, I18nValue>> = {}
|
|
|
|
const fallbackIndex = (indexes[primaryLanguage.name] =
|
|
|
|
createI18nStringsIndex(primaryLanguage.strings))
|
|
|
|
if (otherLanguages) {
|
|
|
|
Object.keys(otherLanguages).forEach((lang) => {
|
|
|
|
indexes[lang] = createI18nStringsIndex(otherLanguages[lang])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(defaultLanguage in indexes)) {
|
|
|
|
throw new MtArgumentError(
|
|
|
|
'defaultLanguage is not a registered language'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-29 18:53:27 +03:00
|
|
|
const tr = (
|
|
|
|
lang: Input | string | null,
|
|
|
|
key: string,
|
|
|
|
...params: any[]
|
|
|
|
) => {
|
2022-07-19 02:47:59 +03:00
|
|
|
if (lang === null) lang = defaultLanguage
|
|
|
|
|
2022-08-29 18:53:27 +03:00
|
|
|
if (typeof lang !== 'string') {
|
|
|
|
lang = adapter(lang) ?? defaultLanguage
|
2022-07-19 02:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const strings = indexes[lang] ?? fallbackIndex
|
|
|
|
|
|
|
|
let val = strings[key] ?? fallbackIndex[key] ?? `[missing: ${key}]`
|
|
|
|
|
|
|
|
if (typeof val === 'function') {
|
|
|
|
val = val(...params)
|
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2022-08-29 18:53:27 +03:00
|
|
|
return tr as MtcuteI18nFunction<Strings, Input>
|
2022-07-19 02:47:59 +03:00
|
|
|
}
|