1 |
|
2 | import type { $Dictionary, $NormalizeIntoArray } from './typescript/helpers.js';
|
3 | import type {
|
4 | DefaultNamespace,
|
5 | FlatNamespace,
|
6 | FormatFunction,
|
7 | InitOptions,
|
8 | InterpolationOptions,
|
9 | Namespace,
|
10 | Resource,
|
11 | ResourceKey,
|
12 | ResourceLanguage,
|
13 | TOptions,
|
14 | } from './typescript/options.js';
|
15 | import type { KeyPrefix, TFunction } from './typescript/t.v4.js';
|
16 |
|
17 | export interface WithT<Ns extends Namespace = DefaultNamespace> {
|
18 |
|
19 | t: TFunction<Ns>;
|
20 | }
|
21 |
|
22 | export interface Interpolator {
|
23 | init(options: InterpolationOptions, reset: boolean): undefined;
|
24 | reset(): undefined;
|
25 | resetRegExp(): undefined;
|
26 | interpolate(str: string, data: object, lng: string, options: InterpolationOptions): string;
|
27 | nest(str: string, fc: (...args: any[]) => any, options: InterpolationOptions): string;
|
28 | }
|
29 |
|
30 | export class ResourceStore {
|
31 | constructor(data: Resource, options: InitOptions);
|
32 |
|
33 | public data: Resource;
|
34 |
|
35 | public options: InitOptions;
|
36 |
|
37 | /**
|
38 | * Gets fired when resources got added or removed
|
39 | */
|
40 | on(event: 'added' | 'removed', callback: (lng: string, ns: string) => void): void;
|
41 |
|
42 | /**
|
43 | * Remove event listener
|
44 | * removes all callback when callback not specified
|
45 | */
|
46 | off(event: 'added' | 'removed', callback?: (lng: string, ns: string) => void): void;
|
47 | }
|
48 |
|
49 | export interface Formatter {
|
50 | init(services: Services, i18nextOptions: InitOptions): void;
|
51 | add(name: string, fc: (value: any, lng: string | undefined, options: any) => string): void;
|
52 | addCached(
|
53 | name: string,
|
54 | fc: (lng: string | undefined, options: any) => (value: any) => string,
|
55 | ): void;
|
56 | format: FormatFunction;
|
57 | }
|
58 |
|
59 | export interface Services {
|
60 | backendConnector: any;
|
61 | i18nFormat: any;
|
62 | interpolator: Interpolator;
|
63 | languageDetector: any;
|
64 | languageUtils: any;
|
65 | logger: any;
|
66 | pluralResolver: any;
|
67 | resourceStore: ResourceStore;
|
68 | formatter?: Formatter;
|
69 | }
|
70 |
|
71 | export type ModuleType =
|
72 | | 'backend'
|
73 | | 'logger'
|
74 | | 'languageDetector'
|
75 | | 'postProcessor'
|
76 | | 'i18nFormat'
|
77 | | 'formatter'
|
78 | | '3rdParty';
|
79 |
|
80 | export interface Module {
|
81 | type: ModuleType;
|
82 | }
|
83 |
|
84 | export type CallbackError = Error | string | null | undefined;
|
85 | export type ReadCallback = (
|
86 | err: CallbackError,
|
87 | data: ResourceKey | boolean | null | undefined,
|
88 | ) => void;
|
89 | export type MultiReadCallback = (err: CallbackError, data: Resource | null | undefined) => void;
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | export interface BackendModule<Options = object> extends Module {
|
97 | type: 'backend';
|
98 | init(services: Services, backendOptions: Options, i18nextOptions: InitOptions): void;
|
99 | read(language: string, namespace: string, callback: ReadCallback): void;
|
100 |
|
101 | create?(
|
102 | languages: readonly string[],
|
103 | namespace: string,
|
104 | key: string,
|
105 | fallbackValue: string,
|
106 | ): void;
|
107 |
|
108 | readMulti?(
|
109 | languages: readonly string[],
|
110 | namespaces: readonly string[],
|
111 | callback: MultiReadCallback,
|
112 | ): void;
|
113 |
|
114 | save?(language: string, namespace: string, data: ResourceLanguage): void;
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | export interface LanguageDetectorModule extends Module {
|
123 | type: 'languageDetector';
|
124 | init?(services: Services, detectorOptions: object, i18nextOptions: InitOptions): void;
|
125 |
|
126 | detect(): string | readonly string[] | undefined;
|
127 | cacheUserLanguage?(lng: string): void;
|
128 | }
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | export interface LanguageDetectorAsyncModule extends Module {
|
136 | type: 'languageDetector';
|
137 |
|
138 | async: true;
|
139 | init?(services: Services, detectorOptions: object, i18nextOptions: InitOptions): void;
|
140 |
|
141 | detect(
|
142 | callback: (lng: string | readonly string[] | undefined) => void | undefined,
|
143 | ): void | Promise<string | readonly string[] | undefined>;
|
144 | cacheUserLanguage?(lng: string): void | Promise<void>;
|
145 | }
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | export interface PostProcessorModule extends Module {
|
152 |
|
153 | name: string;
|
154 | type: 'postProcessor';
|
155 | process(value: string, key: string | string[], options: TOptions, translator: any): string;
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | export interface LoggerModule extends Module {
|
163 | type: 'logger';
|
164 | log(...args: any[]): void;
|
165 | warn(...args: any[]): void;
|
166 | error(...args: any[]): void;
|
167 | }
|
168 |
|
169 | export interface I18nFormatModule extends Module {
|
170 | type: 'i18nFormat';
|
171 | }
|
172 |
|
173 | export interface FormatterModule extends Module, Formatter {
|
174 | type: 'formatter';
|
175 | }
|
176 |
|
177 | export interface ThirdPartyModule extends Module {
|
178 | type: '3rdParty';
|
179 | init(i18next: i18n): void;
|
180 | }
|
181 |
|
182 | export interface Modules {
|
183 | backend?: BackendModule;
|
184 | logger?: LoggerModule;
|
185 | languageDetector?: LanguageDetectorModule | LanguageDetectorAsyncModule;
|
186 | i18nFormat?: I18nFormatModule;
|
187 | formatter?: FormatterModule;
|
188 | external: ThirdPartyModule[];
|
189 | }
|
190 |
|
191 |
|
192 | export interface Newable<T> {
|
193 | new (...args: any[]): T;
|
194 | }
|
195 | export interface NewableModule<T extends Module> extends Newable<T> {
|
196 | type: T['type'];
|
197 | }
|
198 |
|
199 | export type Callback = (error: any, t: TFunction) => void;
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | export interface ExistsFunction<
|
205 | TKeys extends string = string,
|
206 | TInterpolationMap extends object = $Dictionary,
|
207 | > {
|
208 | (key: TKeys | TKeys[], options?: TOptions<TInterpolationMap>): boolean;
|
209 | }
|
210 |
|
211 | export interface CloneOptions extends InitOptions {
|
212 | |
213 |
|
214 |
|
215 |
|
216 |
|
217 | forkResourceStore?: boolean;
|
218 | }
|
219 |
|
220 | export interface CustomInstanceExtensions {}
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | type InferArrayValuesElseReturnType<T> = T extends (infer A)[] ? A : T;
|
226 |
|
227 |
|
228 | export interface i18n extends CustomInstanceExtensions {
|
229 |
|
230 | t: TFunction<
|
231 | [
|
232 | ...$NormalizeIntoArray<DefaultNamespace>,
|
233 | ...Exclude<FlatNamespace, InferArrayValuesElseReturnType<DefaultNamespace>>[],
|
234 | ]
|
235 | >;
|
236 |
|
237 | |
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | init(callback?: Callback): Promise<TFunction>;
|
245 | init<T>(options: InitOptions<T>, callback?: Callback): Promise<TFunction>;
|
246 |
|
247 | loadResources(callback?: (err: any) => void): void;
|
248 |
|
249 | |
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | use<T extends Module>(module: T | NewableModule<T> | Newable<T>): this;
|
256 |
|
257 | /**
|
258 | * List of modules used
|
259 | */
|
260 | modules: Modules;
|
261 |
|
262 | /**
|
263 | * Internal container for all used plugins and implementation details like languageUtils, pluralResolvers, etc.
|
264 | */
|
265 | services: Services;
|
266 |
|
267 | /**
|
268 | * Internal container for translation resources
|
269 | */
|
270 | store: ResourceStore;
|
271 |
|
272 | /**
|
273 | * Uses similar args as the t function and returns true if a key exists.
|
274 | */
|
275 | exists: ExistsFunction;
|
276 |
|
277 | /**
|
278 | * Returns a resource data by language.
|
279 | */
|
280 | getDataByLanguage(lng: string): { [key: string]: { [key: string]: string } } | undefined;
|
281 |
|
282 | |
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 | getFixedT<
|
290 | Ns extends Namespace | null = DefaultNamespace,
|
291 | TKPrefix extends KeyPrefix<ActualNs> = undefined,
|
292 | ActualNs extends Namespace = Ns extends null ? DefaultNamespace : Ns,
|
293 | >(
|
294 | ...args:
|
295 | | [lng: string | readonly string[], ns?: Ns, keyPrefix?: TKPrefix]
|
296 | | [lng: null, ns: Ns, keyPrefix?: TKPrefix]
|
297 | ): TFunction<ActualNs, TKPrefix>;
|
298 |
|
299 | |
300 |
|
301 |
|
302 |
|
303 | changeLanguage(lng?: string, callback?: Callback): Promise<TFunction>;
|
304 |
|
305 | |
306 |
|
307 |
|
308 |
|
309 | language: string;
|
310 |
|
311 | |
312 |
|
313 |
|
314 | languages: readonly string[];
|
315 |
|
316 | |
317 |
|
318 |
|
319 |
|
320 | resolvedLanguage?: string;
|
321 |
|
322 | |
323 |
|
324 |
|
325 |
|
326 | hasLoadedNamespace(
|
327 | ns: string | readonly string[],
|
328 | options?: {
|
329 | lng?: string | readonly string[];
|
330 | fallbackLng?: InitOptions['fallbackLng'];
|
331 | |
332 |
|
333 |
|
334 | precheck?: (
|
335 | i18n: i18n,
|
336 | |
337 |
|
338 |
|
339 |
|
340 | loadNotPending: (
|
341 | lng: string | readonly string[],
|
342 | ns: string | readonly string[],
|
343 | ) => boolean,
|
344 | ) => boolean | undefined;
|
345 | },
|
346 | ): boolean;
|
347 |
|
348 | |
349 |
|
350 |
|
351 | loadNamespaces(ns: string | readonly string[], callback?: Callback): Promise<void>;
|
352 |
|
353 | |
354 |
|
355 |
|
356 | loadLanguages(lngs: string | readonly string[], callback?: Callback): Promise<void>;
|
357 |
|
358 | |
359 |
|
360 |
|
361 | reloadResources(
|
362 | lngs?: string | readonly string[],
|
363 | ns?: string | readonly string[],
|
364 | callback?: () => void,
|
365 | ): Promise<void>;
|
366 | reloadResources(lngs: null, ns: string | readonly string[], callback?: () => void): Promise<void>;
|
367 |
|
368 | |
369 |
|
370 |
|
371 | setDefaultNamespace(ns: string): void;
|
372 |
|
373 | |
374 |
|
375 |
|
376 | dir(lng?: string): 'ltr' | 'rtl';
|
377 |
|
378 | |
379 |
|
380 |
|
381 | format: FormatFunction;
|
382 |
|
383 | |
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 | createInstance(options?: InitOptions, callback?: Callback): i18n;
|
390 |
|
391 | |
392 |
|
393 |
|
394 |
|
395 | cloneInstance(options?: CloneOptions, callback?: Callback): i18n;
|
396 |
|
397 | |
398 |
|
399 |
|
400 | on(event: 'initialized', callback: (options: InitOptions) => void): void;
|
401 |
|
402 | |
403 |
|
404 |
|
405 | on(
|
406 | event: 'loaded',
|
407 | callback: (loaded: { [language: string]: { [namespace: string]: boolean } }) => void,
|
408 | ): void;
|
409 |
|
410 | |
411 |
|
412 |
|
413 | on(event: 'failedLoading', callback: (lng: string, ns: string, msg: string) => void): void;
|
414 |
|
415 | |
416 |
|
417 |
|
418 | on(
|
419 | event: 'missingKey',
|
420 | callback: (lngs: readonly string[], namespace: string, key: string, res: string) => void,
|
421 | ): void;
|
422 |
|
423 | |
424 |
|
425 |
|
426 | on(event: 'added' | 'removed', callback: (lng: string, ns: string) => void): void;
|
427 |
|
428 | |
429 |
|
430 |
|
431 | on(event: 'languageChanged', callback: (lng: string) => void): void;
|
432 |
|
433 | |
434 |
|
435 |
|
436 | on(event: string, listener: (...args: any[]) => void): void;
|
437 |
|
438 | |
439 |
|
440 |
|
441 |
|
442 | off(event: string, listener?: (...args: any[]) => void): void;
|
443 |
|
444 | |
445 |
|
446 |
|
447 | getResource(
|
448 | lng: string,
|
449 | ns: string,
|
450 | key: string,
|
451 | options?: Pick<InitOptions, 'keySeparator' | 'ignoreJSONStructure'>,
|
452 | ): any;
|
453 |
|
454 | |
455 |
|
456 |
|
457 | addResource(
|
458 | lng: string,
|
459 | ns: string,
|
460 | key: string,
|
461 | value: string,
|
462 | options?: { keySeparator?: string; silent?: boolean },
|
463 | ): i18n;
|
464 |
|
465 | |
466 |
|
467 |
|
468 | addResources(lng: string, ns: string, resources: any): i18n;
|
469 |
|
470 | |
471 |
|
472 |
|
473 |
|
474 |
|
475 | addResourceBundle(
|
476 | lng: string,
|
477 | ns: string,
|
478 | resources: any,
|
479 | deep?: boolean,
|
480 | overwrite?: boolean,
|
481 | ): i18n;
|
482 |
|
483 | |
484 |
|
485 |
|
486 | hasResourceBundle(lng: string, ns: string): boolean;
|
487 |
|
488 | |
489 |
|
490 |
|
491 | getResourceBundle(lng: string, ns: string): any;
|
492 |
|
493 | |
494 |
|
495 |
|
496 | removeResourceBundle(lng: string, ns: string): i18n;
|
497 |
|
498 | |
499 |
|
500 |
|
501 | options: InitOptions;
|
502 |
|
503 | |
504 |
|
505 |
|
506 | isInitialized: boolean;
|
507 |
|
508 | |
509 |
|
510 |
|
511 | isInitializing: boolean;
|
512 |
|
513 | |
514 |
|
515 |
|
516 | initializedStoreOnce: boolean;
|
517 |
|
518 | |
519 |
|
520 |
|
521 | initializedLanguageOnce: boolean;
|
522 |
|
523 | |
524 |
|
525 |
|
526 | emit(eventName: string, ...args: any[]): void;
|
527 | }
|
528 |
|
529 |
|
530 | export type {
|
531 |
|
532 | FallbackLngObjList,
|
533 | FallbackLng,
|
534 | InitOptions,
|
535 | TypeOptions,
|
536 | CustomTypeOptions,
|
537 | CustomPluginOptions,
|
538 | PluginOptions,
|
539 | FormatFunction,
|
540 | InterpolationOptions,
|
541 | ReactOptions,
|
542 | ResourceKey,
|
543 | ResourceLanguage,
|
544 | Resource,
|
545 | TOptions,
|
546 | Namespace,
|
547 | DefaultNamespace,
|
548 | FlatNamespace,
|
549 | } from './typescript/options.js';
|
550 |
|
551 | export type {
|
552 | TFunction,
|
553 | ParseKeys,
|
554 | TFunctionReturn,
|
555 | TFunctionDetailedResult,
|
556 | KeyPrefix,
|
557 | } from './typescript/t.v4.js';
|
558 |
|
559 | declare const i18next: i18n;
|
560 | export default i18next;
|
561 |
|
562 | export const createInstance: i18n['createInstance'];
|
563 |
|
564 | export const dir: i18n['dir'];
|
565 | export const init: i18n['init'];
|
566 | export const loadResources: i18n['loadResources'];
|
567 | export const reloadResources: i18n['reloadResources'];
|
568 | export const use: i18n['use'];
|
569 | export const changeLanguage: i18n['changeLanguage'];
|
570 | export const getFixedT: i18n['getFixedT'];
|
571 | export const t: i18n['t'];
|
572 | export const exists: i18n['exists'];
|
573 | export const setDefaultNamespace: i18n['setDefaultNamespace'];
|
574 | export const hasLoadedNamespace: i18n['hasLoadedNamespace'];
|
575 | export const loadNamespaces: i18n['loadNamespaces'];
|
576 | export const loadLanguages: i18n['loadLanguages'];
|