UNPKG

andrei-bread-i18n

Version:

Small and type-safe package to create multi-language interfaces.

31 lines (30 loc) 1.59 kB
type Translation = string | Record<string, string>; type Keyset = Record<string, Translation>; export type LanguageConfig = { keyset: Keyset | (() => Promise<Keyset>); pluralize: (count: number) => string; }; interface I18NOptions<LanguagesMap extends Record<string, LanguageConfig>, Lang extends keyof LanguagesMap = keyof LanguagesMap> { defaultLang: Lang; languages: LanguagesMap; } type KeyType<KeysetsMap extends Record<string, LanguageConfig>> = keyof KeysetType<KeysetsMap>; type KeysetType<KeysetsMap extends Record<string, LanguageConfig>> = UnwrapKeysetType<KeysetsMap[keyof KeysetsMap]["keyset"]>; type UnwrapKeysetType<MaybeUnresolvedKeyset extends Keyset | (() => Promise<Keyset>)> = MaybeUnresolvedKeyset extends () => Promise<infer ResolvedKeyset> ? ResolvedKeyset : MaybeUnresolvedKeyset; type GetRestParams<KeysetsMap extends Record<string, LanguageConfig>, Key extends KeyType<KeysetsMap>> = KeysetType<KeysetsMap>[Key] extends object ? [options: { count: number; [key: string]: number | string; }] : [options?: Record<string, string | number>]; export declare class I18N<KeysetsMap extends Record<string, LanguageConfig>> { private lang; private subscribers; private keysets; constructor(options: I18NOptions<KeysetsMap>); getLang(): keyof KeysetsMap; get<Key extends KeyType<KeysetsMap>>(key: Key, ...rest: GetRestParams<KeysetsMap, Key>): string; setLang(newLang: keyof KeysetsMap): Promise<void>; subscribe(cb: (fn: keyof KeysetsMap) => void, options?: { immediate: boolean; }): () => void; } export {};