123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- import type { $Dictionary, $NormalizeIntoArray } from './typescript/helpers.js';
- import type {
- DefaultNamespace,
- FlatNamespace,
- FormatFunction,
- InitOptions,
- InterpolationOptions,
- Namespace,
- Resource,
- ResourceKey,
- ResourceLanguage,
- TOptions,
- } from './typescript/options.js';
- import type { KeyPrefix, TFunction } from './typescript/t.js';
- export interface WithT<Ns extends Namespace = DefaultNamespace> {
-
- t: TFunction<Ns>;
- }
- export interface Interpolator {
- init(options: InterpolationOptions, reset: boolean): undefined;
- reset(): undefined;
- resetRegExp(): undefined;
- interpolate(str: string, data: object, lng: string, options: InterpolationOptions): string;
- nest(str: string, fc: (...args: any[]) => any, options: InterpolationOptions): string;
- }
- export class ResourceStore {
- constructor(data: Resource, options: InitOptions);
- public data: Resource;
- public options: InitOptions;
-
- on(event: 'added' | 'removed', callback: (lng: string, ns: string) => void): void;
-
- off(event: 'added' | 'removed', callback?: (lng: string, ns: string) => void): void;
- }
- export interface Formatter {
- init(services: Services, i18nextOptions: InitOptions): void;
- add(name: string, fc: (value: any, lng: string | undefined, options: any) => string): void;
- addCached(
- name: string,
- fc: (lng: string | undefined, options: any) => (value: any) => string,
- ): void;
- format: FormatFunction;
- }
- export interface Services {
- backendConnector: any;
- i18nFormat: any;
- interpolator: Interpolator;
- languageDetector: any;
- languageUtils: any;
- logger: any;
- pluralResolver: any;
- resourceStore: ResourceStore;
- formatter?: Formatter;
- }
- export type ModuleType =
- | 'backend'
- | 'logger'
- | 'languageDetector'
- | 'postProcessor'
- | 'i18nFormat'
- | 'formatter'
- | '3rdParty';
- export interface Module {
- type: ModuleType;
- }
- export type CallbackError = Error | string | null | undefined;
- export type ReadCallback = (
- err: CallbackError,
- data: ResourceKey | boolean | null | undefined,
- ) => void;
- export type MultiReadCallback = (err: CallbackError, data: Resource | null | undefined) => void;
- export interface BackendModule<Options = object> extends Module {
- type: 'backend';
- init(services: Services, backendOptions: Options, i18nextOptions: InitOptions): void;
- read(language: string, namespace: string, callback: ReadCallback): void;
-
- create?(
- languages: readonly string[],
- namespace: string,
- key: string,
- fallbackValue: string,
- ): void;
-
- readMulti?(
- languages: readonly string[],
- namespaces: readonly string[],
- callback: MultiReadCallback,
- ): void;
-
- save?(language: string, namespace: string, data: ResourceLanguage): void;
- }
- export interface LanguageDetectorModule extends Module {
- type: 'languageDetector';
- init?(services: Services, detectorOptions: object, i18nextOptions: InitOptions): void;
-
- detect(): string | readonly string[] | undefined;
- cacheUserLanguage?(lng: string): void;
- }
- export interface LanguageDetectorAsyncModule extends Module {
- type: 'languageDetector';
-
- async: true;
- init?(services: Services, detectorOptions: object, i18nextOptions: InitOptions): void;
-
- detect(
- callback: (lng: string | readonly string[] | undefined) => void | undefined,
- ): void | Promise<string | readonly string[] | undefined>;
- cacheUserLanguage?(lng: string): void | Promise<void>;
- }
- export interface PostProcessorModule extends Module {
-
- name: string;
- type: 'postProcessor';
- process(value: string, key: string | string[], options: TOptions, translator: any): string;
- }
- export interface LoggerModule extends Module {
- type: 'logger';
- log(...args: any[]): void;
- warn(...args: any[]): void;
- error(...args: any[]): void;
- }
- export interface I18nFormatModule extends Module {
- type: 'i18nFormat';
- }
- export interface FormatterModule extends Module, Formatter {
- type: 'formatter';
- }
- export interface ThirdPartyModule extends Module {
- type: '3rdParty';
- init(i18next: i18n): void;
- }
- export interface Modules {
- backend?: BackendModule;
- logger?: LoggerModule;
- languageDetector?: LanguageDetectorModule | LanguageDetectorAsyncModule;
- i18nFormat?: I18nFormatModule;
- formatter?: FormatterModule;
- external: ThirdPartyModule[];
- }
- export interface Newable<T> {
- new (...args: any[]): T;
- }
- export interface NewableModule<T extends Module> extends Newable<T> {
- type: T['type'];
- }
- export type Callback = (error: any, t: TFunction) => void;
- export interface ExistsFunction<
- TKeys extends string = string,
- TInterpolationMap extends object = $Dictionary,
- > {
- (key: TKeys | TKeys[], options?: TOptions<TInterpolationMap>): boolean;
- }
- export interface CloneOptions extends InitOptions {
-
- forkResourceStore?: boolean;
- }
- export interface CustomInstanceExtensions {}
- type InferArrayValuesElseReturnType<T> = T extends (infer A)[] ? A : T;
- export interface i18n extends CustomInstanceExtensions {
-
- t: TFunction<
- [
- ...$NormalizeIntoArray<DefaultNamespace>,
- ...Exclude<FlatNamespace, InferArrayValuesElseReturnType<DefaultNamespace>>[],
- ]
- >;
-
- init(callback?: Callback): Promise<TFunction>;
- init<T>(options: InitOptions<T>, callback?: Callback): Promise<TFunction>;
- loadResources(callback?: (err: any) => void): void;
-
- use<T extends Module>(module: T | NewableModule<T> | Newable<T>): this;
- /**
- * List of modules used
- */
- modules: Modules;
- /**
- * Internal container for all used plugins and implementation details like languageUtils, pluralResolvers, etc.
- */
- services: Services;
- /**
- * Internal container for translation resources
- */
- store: ResourceStore;
- /**
- * Uses similar args as the t function and returns true if a key exists.
- */
- exists: ExistsFunction;
- /**
- * Returns a resource data by language.
- */
- getDataByLanguage(lng: string): { [key: string]: { [key: string]: string } } | undefined;
-
- getFixedT<
- Ns extends Namespace | null = DefaultNamespace,
- TKPrefix extends KeyPrefix<ActualNs> = undefined,
- ActualNs extends Namespace = Ns extends null ? DefaultNamespace : Ns,
- >(
- ...args:
- | [lng: string | readonly string[], ns?: Ns, keyPrefix?: TKPrefix]
- | [lng: null, ns: Ns, keyPrefix?: TKPrefix]
- ): TFunction<ActualNs, TKPrefix>;
-
- changeLanguage(lng?: string, callback?: Callback): Promise<TFunction>;
-
- language: string;
-
- languages: readonly string[];
-
- resolvedLanguage?: string;
-
- hasLoadedNamespace(
- ns: string | readonly string[],
- options?: {
- lng?: string | readonly string[];
- fallbackLng?: InitOptions['fallbackLng'];
-
- precheck?: (
- i18n: i18n,
-
- loadNotPending: (
- lng: string | readonly string[],
- ns: string | readonly string[],
- ) => boolean,
- ) => boolean | undefined;
- },
- ): boolean;
-
- loadNamespaces(ns: string | readonly string[], callback?: Callback): Promise<void>;
-
- loadLanguages(lngs: string | readonly string[], callback?: Callback): Promise<void>;
-
- reloadResources(
- lngs?: string | readonly string[],
- ns?: string | readonly string[],
- callback?: () => void,
- ): Promise<void>;
- reloadResources(lngs: null, ns: string | readonly string[], callback?: () => void): Promise<void>;
-
- setDefaultNamespace(ns: string): void;
-
- dir(lng?: string): 'ltr' | 'rtl';
-
- format: FormatFunction;
-
- createInstance(options?: InitOptions, callback?: Callback): i18n;
-
- cloneInstance(options?: CloneOptions, callback?: Callback): i18n;
-
- on(event: 'initialized', callback: (options: InitOptions) => void): void;
-
- on(
- event: 'loaded',
- callback: (loaded: { [language: string]: { [namespace: string]: boolean } }) => void,
- ): void;
-
- on(event: 'failedLoading', callback: (lng: string, ns: string, msg: string) => void): void;
-
- on(
- event: 'missingKey',
- callback: (lngs: readonly string[], namespace: string, key: string, res: string) => void,
- ): void;
-
- on(event: 'added' | 'removed', callback: (lng: string, ns: string) => void): void;
-
- on(event: 'languageChanged', callback: (lng: string) => void): void;
-
- on(event: string, listener: (...args: any[]) => void): void;
-
- off(event: string, listener?: (...args: any[]) => void): void;
-
- getResource(
- lng: string,
- ns: string,
- key: string,
- options?: Pick<InitOptions, 'keySeparator' | 'ignoreJSONStructure'>,
- ): any;
-
- addResource(
- lng: string,
- ns: string,
- key: string,
- value: string,
- options?: { keySeparator?: string; silent?: boolean },
- ): i18n;
-
- addResources(lng: string, ns: string, resources: any): i18n;
-
- addResourceBundle(
- lng: string,
- ns: string,
- resources: any,
- deep?: boolean,
- overwrite?: boolean,
- ): i18n;
-
- hasResourceBundle(lng: string, ns: string): boolean;
-
- getResourceBundle(lng: string, ns: string): any;
-
- removeResourceBundle(lng: string, ns: string): i18n;
-
- options: InitOptions;
-
- isInitialized: boolean;
-
- isInitializing: boolean;
-
- initializedStoreOnce: boolean;
-
- initializedLanguageOnce: boolean;
-
- emit(eventName: string, ...args: any[]): void;
- }
- export type * from './typescript/options.js';
- export type {
-
- FallbackLngObjList,
- FallbackLng,
- InitOptions,
- TypeOptions,
- CustomTypeOptions,
- CustomPluginOptions,
- PluginOptions,
- FormatFunction,
- InterpolationOptions,
- ReactOptions,
- ResourceKey,
- ResourceLanguage,
- Resource,
- TOptions,
- Namespace,
- DefaultNamespace,
- FlatNamespace,
- } from './typescript/options.js';
- export type * from './typescript/t.js';
- export type {
- TFunction,
- ParseKeys,
- TFunctionReturn,
- TFunctionDetailedResult,
- KeyPrefix,
- } from './typescript/t.js';
- declare const i18next: i18n;
- export default i18next;
- export const createInstance: i18n['createInstance'];
- export const dir: i18n['dir'];
- export const init: i18n['init'];
- export const loadResources: i18n['loadResources'];
- export const reloadResources: i18n['reloadResources'];
- export const use: i18n['use'];
- export const changeLanguage: i18n['changeLanguage'];
- export const getFixedT: i18n['getFixedT'];
- export const t: i18n['t'];
- export const exists: i18n['exists'];
- export const setDefaultNamespace: i18n['setDefaultNamespace'];
- export const hasLoadedNamespace: i18n['hasLoadedNamespace'];
- export const loadNamespaces: i18n['loadNamespaces'];
- export const loadLanguages: i18n['loadLanguages'];
|