mtcute/packages/i18n/src/index.ts
Alina Tumanova f5976a2d74
ESM + end-to-end tests (#11)
* feat: moved tl-runtime to esm and native ArrayBuffers

* feat: migration to esm

* fix(core): web-related fixes

* test: finally, some good fucking e2e

* chore: fixed linters etc

* ci: added e2e to ci

* build(tl): fixed gen-code on node 20

* fix: codegen Uint8Array, not Buffer

never `git reset --hard` kids

* build: only do type-aware linting for `packages/*`

* build: ignore no-unresolved in ci for e2e

* fix: node 16 doesn't have subtle crypto apparently?

* fix(tests): use Uint8Array

for gods sake please can i just merge this already

* ci: don't parallel tasks in ci

because machines are utter garbage and it may just randomly break

* ci: pass secrets to e2e tests

* ci: separate cli command for ci

apparently im retarded

* fix: run codegen in e2e

im actually retarded

* ci: more fixes for e2e

* ci: debugging stuff

* ci: still debugging

* ci: hopefully fix ci???
2023-10-16 19:23:53 +03:00

80 lines
2.3 KiB
TypeScript

import { I18nStrings, I18nValue, MtcuteI18nAdapter, MtcuteI18nFunction, OtherLanguageWrap } from './types.js'
import { createI18nStringsIndex, extractLanguageFromUpdate } from './utils.js'
export * from './types.js'
export { extractLanguageFromUpdate } from './utils.js'
export interface MtcuteI18nParameters<Strings extends I18nStrings, Input> {
/**
* 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
/**
* Adapter that will be used to extract language from the update.
*/
adapter?: MtcuteI18nAdapter<Input>
}
export function createMtcuteI18n<Strings extends I18nStrings, Input>(
params: MtcuteI18nParameters<Strings, Input>,
): MtcuteI18nFunction<Strings, Input> {
const {
primaryLanguage,
otherLanguages,
defaultLanguage = primaryLanguage.name,
adapter = extractLanguageFromUpdate as unknown as MtcuteI18nAdapter<Input>,
} = 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] 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<Strings, Input>
}