forked from i18next/i18next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
1243 lines (1099 loc) · 32.4 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
type MergeBy<T, K> = Omit<T, keyof K> & K;
export interface FallbackLngObjList {
[language: string]: readonly string[];
}
export type FallbackLng =
| string
| readonly string[]
| FallbackLngObjList
| ((code: string) => string | readonly string[] | FallbackLngObjList);
export type FormatFunction = (
value: any,
format?: string,
lng?: string,
options?: InterpolationOptions & { [key: string]: any },
) => string;
export interface InterpolationOptions {
/**
* Format function see formatting for details
* @default noop
*/
format?: FormatFunction;
/**
* Used to separate format from interpolation value
* @default ','
*/
formatSeparator?: string;
/**
* Escape function
* @default str => str
*/
escape?(str: string): string;
/**
* Always format interpolated values.
* @default false
*/
alwaysFormat?: boolean;
/**
* Escape passed in values to avoid xss injection
* @default true
*/
escapeValue?: boolean;
/**
* If true, then value passed into escape function is not casted to string, use with custom escape function that does its own type check
* @default false
*/
useRawValueToEscape?: boolean;
/**
* Prefix for interpolation
* @default '{{'
*/
prefix?: string;
/**
* Suffix for interpolation
* @default '}}'
*/
suffix?: string;
/**
* Escaped prefix for interpolation (regexSafe)
* @default undefined
*/
prefixEscaped?: string;
/**
* Escaped suffix for interpolation (regexSafe)
* @default undefined
*/
suffixEscaped?: string;
/**
* Suffix to unescaped mode
* @default undefined
*/
unescapeSuffix?: string;
/**
* Prefix to unescaped mode
* @default '-'
*/
unescapePrefix?: string;
/**
* Prefix for nesting
* @default '$t('
*/
nestingPrefix?: string;
/**
* Suffix for nesting
* @default ')'
*/
nestingSuffix?: string;
/**
* Escaped prefix for nesting (regexSafe)
* @default undefined
*/
nestingPrefixEscaped?: string;
/**
* Escaped suffix for nesting (regexSafe)
* @default undefined
*/
nestingSuffixEscaped?: string;
/**
* Separates options from key
* @default ','
*/
nestingOptionsSeparator?: string;
/**
* Global variables to use in interpolation replacements
* @default undefined
*/
defaultVariables?: { [index: string]: any };
/**
* After how many interpolation runs to break out before throwing a stack overflow
* @default 1000
*/
maxReplaces?: number;
/**
* If true, it will skip to interpolate the variables
* @default true
*/
skipOnVariables?: boolean;
}
export interface ReactOptions {
/**
* Set it to fallback to let passed namespaces to translated hoc act as fallbacks
* @default 'default'
*/
nsMode?: 'default' | 'fallback';
/**
* Set it to the default parent element created by the Trans component.
* @default 'div'
*/
defaultTransParent?: string;
/**
* Set which events trigger a re-render, can be set to false or string of events
* @default 'languageChanged'
*/
bindI18n?: string | false;
/**
* Set which events on store trigger a re-render, can be set to false or string of events
* @default ''
*/
bindI18nStore?: string | false;
/**
* Set fallback value for Trans components without children
* @default undefined
*/
transEmptyNodeValue?: string;
/**
* Set it to false if you do not want to use Suspense
* @default true
*/
useSuspense?: boolean;
/**
* Function to generate an i18nKey from the defaultValue (or Trans children)
* when no key is provided.
* By default, the defaultValue (Trans text) itself is used as the key.
* If you want to require keys for all translations, supply a function
* that always throws an error.
* @default undefined
*/
hashTransKey?(defaultValue: TOptionsBase['defaultValue']): TOptionsBase['defaultValue'];
/**
* Convert eg. <br/> found in translations to a react component of type br
* @default true
*/
transSupportBasicHtmlNodes?: boolean;
/**
* Which nodes not to convert in defaultValue generation in the Trans component.
* @default ['br', 'strong', 'i', 'p']
*/
transKeepBasicHtmlNodesFor?: readonly string[];
/**
* Wrap text nodes in a user-specified element.
* @default ''
*/
transWrapTextNodes?: string;
}
/**
* This interface can be augmented by users to add types to `i18next` default PluginOptions.
*/
export interface PluginOptions {}
interface DefaultPluginOptions {
/**
* Options for language detection - check documentation of plugin
* @default undefined
*/
detection?: object;
/**
* Options for backend - check documentation of plugin
* @default undefined
*/
backend?: object;
/**
* Options for cache layer - check documentation of plugin
* @default undefined
*/
cache?: object;
/**
* Options for i18n message format - check documentation of plugin
* @default undefined
*/
i18nFormat?: object;
}
export interface InitOptions extends MergeBy<DefaultPluginOptions, PluginOptions> {
/**
* Logs info level to console output. Helps finding issues with loading not working.
* @default false
*/
debug?: boolean;
/**
* Resources to initialize with (if not using loading or not appending using addResourceBundle)
* @default undefined
*/
resources?: Resource;
/**
* Allow initializing with bundled resources while using a backend to load non bundled ones.
* @default false
*/
partialBundledLanguages?: boolean;
/**
* Language to use (overrides language detection)
* @default undefined
*/
lng?: string;
/**
* Language to use if translations in user language are not available.
* @default 'dev'
*/
fallbackLng?: false | FallbackLng;
/**
* Array of allowed languages
* @default false
*/
supportedLngs?: false | readonly string[];
/**
* If true will pass eg. en-US if finding en in supportedLngs
* @default false
*/
nonExplicitSupportedLngs?: boolean;
/**
* Language codes to lookup, given set language is
* 'en-US': 'all' --> ['en-US', 'en', 'dev'],
* 'currentOnly' --> 'en-US',
* 'languageOnly' --> 'en'
* @default 'all'
*/
load?: 'all' | 'currentOnly' | 'languageOnly';
/**
* Array of languages to preload. Important on server-side to assert translations are loaded before rendering views.
* @default false
*/
preload?: false | readonly string[];
/**
* Language will be lowercased eg. en-US --> en-us
* @default false
*/
lowerCaseLng?: boolean;
/**
* Language will be lowercased EN --> en while leaving full locales like en-US
* @default false
*/
cleanCode?: boolean;
/**
* String or array of namespaces to load
* @default 'translation'
*/
ns?: string | readonly string[];
/**
* Default namespace used if not passed to translation function
* @default 'translation'
*/
defaultNS?: string;
/**
* String or array of namespaces to lookup key if not found in given namespace.
* @default false
*/
fallbackNS?: false | string | readonly string[];
/**
* Calls save missing key function on backend if key not found
* @default false
*/
saveMissing?: boolean;
/**
* Experimental: enable to update default values using the saveMissing
* (Works only if defaultValue different from translated value.
* Only useful on initial development or when keeping code as source of truth not changing values outside of code.
* Only supported if backend supports it already)
* @default false
*/
updateMissing?: boolean;
/**
* @default 'fallback'
*/
saveMissingTo?: 'current' | 'all' | 'fallback';
/**
* Used to not fallback to the key as default value, when using saveMissing functionality.
* i.e. when using with i18next-http-backend this will result in having a key with an empty string value.
* @default false
*/
missingKeyNoValueFallbackToKey?: boolean;
/**
* Used for custom missing key handling (needs saveMissing set to true!)
* @default false
*/
missingKeyHandler?:
| false
| ((
lngs: readonly string[],
ns: string,
key: string,
fallbackValue: string,
updateMissing: boolean,
options: any,
) => void);
/**
* Receives a key that was not found in `t()` and returns a value, that will be returned by `t()`
* @default noop
*/
parseMissingKeyHandler?(key: string, defaultValue?: string): any;
/**
* Appends namespace to missing key
* @default false
*/
appendNamespaceToMissingKey?: boolean;
/**
* Gets called in case a interpolation value is undefined. This method will not be called if the value is empty string or null
* @default noop
*/
missingInterpolationHandler?: (text: string, value: any, options: InitOptions) => any;
/**
* Will use 'plural' as suffix for languages only having 1 plural form, setting it to false will suffix all with numbers
* @default true
*/
simplifyPluralSuffix?: boolean;
/**
* String or array of postProcessors to apply per default
* @default false
*/
postProcess?: false | string | readonly string[];
/**
* passthrough the resolved object including 'usedNS', 'usedLang' etc into options object of postprocessors as 'i18nResolved' property
* @default false
*/
postProcessPassResolved?: boolean;
/**
* Allows null values as valid translation
* @default true
*/
returnNull?: boolean;
/**
* Allows empty string as valid translation
* @default true
*/
returnEmptyString?: boolean;
/**
* Allows objects as valid translation result
* @default false
*/
returnObjects?: boolean;
/**
* Returns an object that includes information about the used language, namespace, key and value
*/
returnDetails?: boolean;
/**
* Gets called if object was passed in as key but returnObjects was set to false
* @default noop
*/
returnedObjectHandler?(key: string, value: string, options: any): void;
/**
* Char, eg. '\n' that arrays will be joined by
* @default false
*/
joinArrays?: false | string;
/**
* Sets defaultValue
* @default args => ({ defaultValue: args[1] })
*/
overloadTranslationOptionHandler?(args: string[]): TOptions;
/**
* @see https://www.i18next.com/interpolation.html
*/
interpolation?: InterpolationOptions;
/**
* Options for react - check documentation of plugin
* @default undefined
*/
react?: ReactOptions;
/**
* Triggers resource loading in init function inside a setTimeout (default async behaviour).
* Set it to false if your backend loads resources sync - that way calling i18next.t after
* init is possible without relaying on the init callback.
* @default true
*/
initImmediate?: boolean;
/**
* Char to separate keys
* @default '.'
*/
keySeparator?: false | string;
/**
* Char to split namespace from key
* @default ':'
*/
nsSeparator?: false | string;
/**
* Char to split plural from key
* @default '_'
*/
pluralSeparator?: string;
/**
* Char to split context from key
* @default '_'
*/
contextSeparator?: string;
/**
* Prefixes the namespace to the returned key when using `cimode`
* @default false
*/
appendNamespaceToCIMode?: boolean;
/**
* Compatibility JSON version
* @default 'v3'
*/
compatibilityJSON?: 'v1' | 'v2' | 'v3';
/**
* Options for https://github.com/locize/locize-editor
* @default undefined
*/
editor?: {
/**
* Enable on init without the need of adding querystring locize=true
* @default false
*/
enabled?: boolean;
/**
* If set to false you will need to open the editor via API
* @default true
*/
autoOpen?: boolean;
/**
* Enable by adding querystring locize=true; can be set to another value or turned off by setting to false
* @default 'locize'
*/
enableByQS?: string | false;
/**
* Turn on/off by pressing key combination. Combine this with `toggleKeyCode`
* @default 'ctrlKey'
*/
toggleKeyModifier?: 'ctrlKey' | 'metaKey' | 'altKey' | 'shiftKey';
/**
* Turn on/off by pressing key combination. Combine this with `toggleKeyModifier`
* @default 24 (x)
*/
toggleKeyCode?: number;
/**
* Use lng in editor taken from query string, eg. if running with lng=cimode (i18next, locize)
* @default 'useLng'
*/
lngOverrideQS?: string;
/**
* Use lng in editor, eg. if running with lng=cimode (i18next, locize)
* @default null
*/
lngOverride?: string | null;
/**
* How the editor will open.
* Setting to window will open a new window/tab instead
* @default 'iframe'
*/
mode?: 'iframe' | 'window';
/**
* Styles to adapt layout in iframe mode to your website layout.
* This will add a style to the `<iframe>`
* @default 'z-index: 2000; position: fixed; top: 0; right: 0; bottom: 0; width: 600px; box-shadow: -3px 0 5px 0 rgba(0,0,0,0.5);'
*/
iframeContainerStyle?: string;
/**
* Styles to adapt layout in iframe mode to your website layout.
* This will add a style to the parent of `<iframe>`
* @default 'height: 100%; width: 600px; border: none;'
*/
iframeStyle?: string;
/**
* Styles to adapt layout in iframe mode to your website layout.
* This will add a style to `<body>`
* @default 'margin-right: 605px;'
*/
bodyStyle?: string;
/**
* Handle when locize saved the edited translations, eg. reload website
* @default noop
*/
onEditorSaved?: (lng: null, ns: string | readonly string[]) => void;
};
/**
* Options for https://github.com/locize/locize-lastused
* @default undefined
*/
locizeLastUsed?: {
/**
* The id of your locize project
*/
projectId: string;
/**
* An api key if you want to send missing keys
*/
apiKey?: string;
/**
* The reference language of your project
* @default 'en'
*/
referenceLng?: string;
/**
* Version
* @default 'latest'
*/
version?: string;
/**
* Debounce interval to send data in milliseconds
* @default 90000
*/
debounceSubmit?: number;
/**
* Hostnames that are allowed to send last used data.
* Please keep those to your local system, staging, test servers (not production)
* @default ['localhost']
*/
allowedHosts?: readonly string[];
};
/**
* Automatically lookup for a flat key if a nested key is not found an vice-versa
* @default true
*/
ignoreJSONStructure?: boolean;
/**
* Limit parallelism of calls to backend
* This is needed to prevent trying to open thousands of
* sockets or file descriptors, which can cause failures
* and actually make the entire process take longer.
* @default 10
*/
maxParallelReads?: number;
}
export interface TOptionsBase {
/**
* Default value to return if a translation was not found
*/
defaultValue?: any;
/**
* Count value used for plurals
*/
count?: number;
/**
* Used for contexts (eg. male\female)
*/
context?: any;
/**
* Object with vars for interpolation - or put them directly in options
*/
replace?: any;
/**
* Override language to use
*/
lng?: string;
/**
* Override languages to use
*/
lngs?: readonly string[];
/**
* Override language to lookup key if not found see fallbacks for details
*/
fallbackLng?: FallbackLng;
/**
* Override namespaces (string or array)
*/
ns?: string | readonly string[];
/**
* Override char to separate keys
*/
keySeparator?: false | string;
/**
* Override char to split namespace from key
*/
nsSeparator?: false | string;
/**
* Accessing an object not a translation string (can be set globally too)
*/
returnObjects?: boolean;
/**
* Returns an object that includes information about the used language, namespace, key and value
*/
returnDetails?: boolean;
/**
* Char, eg. '\n' that arrays will be joined by (can be set globally too)
*/
joinArrays?: string;
/**
* String or array of postProcessors to apply see interval plurals as a sample
*/
postProcess?: string | readonly string[];
/**
* Override interpolation options
*/
interpolation?: InterpolationOptions;
}
/**
* indexer that is open to any value
*/
export type StringMap = { [key: string]: any };
/**
* Options that allow open ended values for interpolation unless type is provided.
*/
export type TOptions<TInterpolationMap extends object = StringMap> = TOptionsBase &
TInterpolationMap;
export type Callback = (error: any, t: TFunction) => void;
/**
* Uses similar args as the t function and returns true if a key exists.
*/
export interface ExistsFunction<
TKeys extends string = string,
TInterpolationMap extends object = StringMap,
> {
(key: TKeys | TKeys[], options?: TOptions<TInterpolationMap>): boolean;
}
export interface WithT {
// Expose parameterized t in the i18next interface hierarchy
t: TFunction;
}
/**
* Object returned from t() function when passed returnDetails: true option.
*/
export type TFunctionDetailedResult<T = string> = {
/**
* The plain used key
*/
usedKey: string;
/**
* The translation result.
*/
res: T;
/**
* The key with context / plural
*/
exactUsedKey: string;
/**
* The used language for this translation.
*/
usedLng: string;
/**
* The used namespace for this translation.
*/
usedNS: string;
};
export type TFunctionResult =
| string
| object
| TFunctionDetailedResult
| Array<string | object>
| undefined
| null;
export type TFunctionKeys = string | TemplateStringsArray;
export interface TFunction {
// basic usage
<
TResult extends TFunctionResult = string,
TKeys extends TFunctionKeys = string,
TInterpolationMap extends object = StringMap,
>(
key: TKeys | TKeys[],
): TResult;
<
TResult extends TFunctionResult = TFunctionDetailedResult<object>,
TKeys extends TFunctionKeys = string,
TInterpolationMap extends object = StringMap,
>(
key: TKeys | TKeys[],
options?: TOptions<TInterpolationMap> & { returnDetails: true; returnObjects: true },
): TResult;
<
TResult extends TFunctionResult = TFunctionDetailedResult,
TKeys extends TFunctionKeys = string,
TInterpolationMap extends object = StringMap,
>(
key: TKeys | TKeys[],
options?: TOptions<TInterpolationMap> & { returnDetails: true },
): TResult;
<
TResult extends TFunctionResult = object,
TKeys extends TFunctionKeys = string,
TInterpolationMap extends object = StringMap,
>(
key: TKeys | TKeys[],
options?: TOptions<TInterpolationMap> & { returnObjects: true },
): TResult;
<
TResult extends TFunctionResult = string,
TKeys extends TFunctionKeys = string,
TInterpolationMap extends object = StringMap,
>(
key: TKeys | TKeys[],
options?: TOptions<TInterpolationMap> | string,
): TResult;
// overloaded usage
<
TResult extends TFunctionResult = string,
TKeys extends TFunctionKeys = string,
TInterpolationMap extends object = StringMap,
>(
key: TKeys | TKeys[],
defaultValue?: string,
options?: TOptions<TInterpolationMap> | string,
): TResult;
}
export interface Resource {
[language: string]: ResourceLanguage;
}
export interface ResourceLanguage {
[namespace: string]: ResourceKey;
}
export type ResourceKey =
| string
| {
[key: string]: any;
};
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;
/**
* Gets fired when resources got added or removed
*/
on(event: 'added' | 'removed', callback: (lng: string, ns: string) => void): void;
/**
* Remove event listener
* removes all callback when callback not specified
*/
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;
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 | null | undefined;
export type ReadCallback = (
err: CallbackError,
data: ResourceKey | boolean | null | undefined,
) => void;
export type MultiReadCallback = (err: CallbackError, data: Resource | null | undefined) => void;
/**
* Used to load data for i18next.
* Can be provided as a singleton or as a prototype constructor (preferred for supporting multiple instances of i18next).
* For singleton set property `type` to `'backend'` For a prototype constructor set static property.
*/
export interface BackendModule<TOptions = object> extends Module {
type: 'backend';
init(services: Services, backendOptions: TOptions, i18nextOptions: InitOptions): void;
read(language: string, namespace: string, callback: ReadCallback): void;
/** Save the missing translation */
create?(
languages: readonly string[],
namespace: string,
key: string,
fallbackValue: string,
): void;
/** Load multiple languages and namespaces. For backends supporting multiple resources loading */
readMulti?(
languages: readonly string[],
namespaces: readonly string[],
callback: MultiReadCallback,
): void;
/** Store the translation. For backends acting as cache layer */
save?(language: string, namespace: string, data: ResourceLanguage): void;
}
/**
* Used to detect language in user land.
* Can be provided as a singleton or as a prototype constructor (preferred for supporting multiple instances of i18next).
* For singleton set property `type` to `'languageDetector'` For a prototype constructor set static property.
*/
export interface LanguageDetectorModule extends Module {
type: 'languageDetector';
init(services: Services, detectorOptions: object, i18nextOptions: InitOptions): void;
/** Must return detected language */
detect(): string | readonly string[] | undefined;
cacheUserLanguage(lng: string): void;
}
/**
* Used to detect language in user land.
* Can be provided as a singleton or as a prototype constructor (preferred for supporting multiple instances of i18next).
* For singleton set property `type` to `'languageDetector'` For a prototype constructor set static property.
*/
export interface LanguageDetectorAsyncModule extends Module {
type: 'languageDetector';
/** Set to true to enable async detection */
async: true;
init(services: Services, detectorOptions: object, i18nextOptions: InitOptions): void;
/** Must call callback passing detected language */
detect(callback: (lng: string | readonly string[] | undefined) => void): void;
cacheUserLanguage(lng: string): void;
}
/**
* Used to extend or manipulate the translated values before returning them in `t` function.
* Need to be a singleton object.
*/
export interface PostProcessorModule extends Module {
/** Unique name */
name: string;
type: 'postProcessor';
process(value: string, key: string, options: TOptions, translator: any): string;
}
/**
* Override the built-in console logger.
* Do not need to be a prototype function.
*/
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[];
}
// helper to identify class https://stackoverflow.com/a/45983481/2363935
export interface Newable<T> {
new (...args: any[]): T;
}
export interface NewableModule<T extends Module> extends Newable<T> {
type: T['type'];
}
export interface i18n {
// Expose parameterized t in the i18next interface hierarchy
t: TFunction;
/**
* The default of the i18next module is an i18next instance ready to be initialized by calling init.
* You can create additional instances using the createInstance function.
*
* @param options - Initial options.
* @param callback - will be called after all translations were loaded or with an error when failed (in case of using a backend).
*/
init(callback?: Callback): Promise<TFunction>;
init(options: InitOptions, callback?: Callback): Promise<TFunction>;
loadResources(callback?: (err: any) => void): void;
/**
* The use function is there to load additional plugins to i18next.
* For available module see the plugins page and don't forget to read the documentation of the plugin.
*
* @param module Accepts a class or object
*/
use<T extends Module>(module: T | NewableModule<T> | Newable<T>): this;
/**
* List of modules used
*/
modules: Modules;