1 | import type { $MergeBy, $PreservedValue, $Dictionary } from './helpers.js';
|
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 | export interface CustomTypeOptions {}
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | export interface CustomPluginOptions {}
|
34 |
|
35 | export type TypeOptions = $MergeBy<
|
36 | {
|
37 |
|
38 | returnNull: false;
|
39 |
|
40 |
|
41 | returnEmptyString: true;
|
42 |
|
43 |
|
44 | returnObjects: false;
|
45 |
|
46 |
|
47 | keySeparator: '.';
|
48 |
|
49 |
|
50 | nsSeparator: ':';
|
51 |
|
52 |
|
53 | pluralSeparator: '_';
|
54 |
|
55 |
|
56 | contextSeparator: '_';
|
57 |
|
58 |
|
59 | defaultNS: 'translation';
|
60 |
|
61 |
|
62 | fallbackNS: false;
|
63 |
|
64 |
|
65 | compatibilityJSON: 'v4';
|
66 |
|
67 |
|
68 | resources: object;
|
69 |
|
70 | |
71 |
|
72 |
|
73 |
|
74 |
|
75 | allowObjectInHTMLChildren: false;
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 | strictKeyChecks: false;
|
82 |
|
83 | |
84 |
|
85 |
|
86 | interpolationPrefix: '{{';
|
87 |
|
88 | |
89 |
|
90 |
|
91 | interpolationSuffix: '}}';
|
92 |
|
93 |
|
94 | unescapePrefix: '-';
|
95 |
|
96 |
|
97 | unescapeSuffix: '';
|
98 | },
|
99 | CustomTypeOptions
|
100 | >;
|
101 |
|
102 | export type PluginOptions<T> = $MergeBy<
|
103 | {
|
104 | |
105 |
|
106 |
|
107 |
|
108 | detection?: object;
|
109 |
|
110 | |
111 |
|
112 |
|
113 |
|
114 | backend?: T;
|
115 |
|
116 | |
117 |
|
118 |
|
119 |
|
120 | cache?: object;
|
121 |
|
122 | |
123 |
|
124 |
|
125 |
|
126 | i18nFormat?: object;
|
127 | },
|
128 | CustomPluginOptions
|
129 | >;
|
130 |
|
131 | export type FormatFunction = (
|
132 | value: any,
|
133 | format?: string,
|
134 | lng?: string,
|
135 | options?: InterpolationOptions & $Dictionary<any>,
|
136 | ) => string;
|
137 |
|
138 | export interface InterpolationOptions {
|
139 | |
140 |
|
141 |
|
142 |
|
143 | format?: FormatFunction;
|
144 | |
145 |
|
146 |
|
147 |
|
148 | formatSeparator?: string;
|
149 | |
150 |
|
151 |
|
152 |
|
153 | escape?(str: string): string;
|
154 |
|
155 | |
156 |
|
157 |
|
158 |
|
159 | alwaysFormat?: boolean;
|
160 | |
161 |
|
162 |
|
163 |
|
164 | escapeValue?: boolean;
|
165 | |
166 |
|
167 |
|
168 |
|
169 | useRawValueToEscape?: boolean;
|
170 | |
171 |
|
172 |
|
173 |
|
174 | prefix?: string;
|
175 | |
176 |
|
177 |
|
178 |
|
179 | suffix?: string;
|
180 | |
181 |
|
182 |
|
183 |
|
184 | prefixEscaped?: string;
|
185 | |
186 |
|
187 |
|
188 |
|
189 | suffixEscaped?: string;
|
190 | |
191 |
|
192 |
|
193 |
|
194 | unescapeSuffix?: string;
|
195 | |
196 |
|
197 |
|
198 |
|
199 | unescapePrefix?: string;
|
200 | |
201 |
|
202 |
|
203 |
|
204 | nestingPrefix?: string;
|
205 | |
206 |
|
207 |
|
208 |
|
209 | nestingSuffix?: string;
|
210 | |
211 |
|
212 |
|
213 |
|
214 | nestingPrefixEscaped?: string;
|
215 | |
216 |
|
217 |
|
218 |
|
219 | nestingSuffixEscaped?: string;
|
220 | |
221 |
|
222 |
|
223 |
|
224 | nestingOptionsSeparator?: string;
|
225 | |
226 |
|
227 |
|
228 |
|
229 |
|
230 | defaultVariables?: { [index: string]: any };
|
231 | |
232 |
|
233 |
|
234 |
|
235 | maxReplaces?: number;
|
236 |
|
237 | |
238 |
|
239 |
|
240 |
|
241 | skipOnVariables?: boolean;
|
242 | }
|
243 |
|
244 | export interface FallbackLngObjList {
|
245 | [language: string]: readonly string[];
|
246 | }
|
247 |
|
248 | export type FallbackLng =
|
249 | | string
|
250 | | readonly string[]
|
251 | | FallbackLngObjList
|
252 | | ((code: string) => string | readonly string[] | FallbackLngObjList);
|
253 |
|
254 | export interface ReactOptions {
|
255 | /**
|
256 | * Set it to fallback to let passed namespaces to translated hoc act as fallbacks
|
257 | * @default 'default'
|
258 | */
|
259 | nsMode?: 'default' | 'fallback';
|
260 | /**
|
261 | * Set it to the default parent element created by the Trans component.
|
262 | * @default 'div'
|
263 | */
|
264 | defaultTransParent?: string;
|
265 | /**
|
266 | * Set which events trigger a re-render, can be set to false or string of events
|
267 | * @default 'languageChanged'
|
268 | */
|
269 | bindI18n?: string | false;
|
270 | /**
|
271 | * Set which events on store trigger a re-render, can be set to false or string of events
|
272 | * @default ''
|
273 | */
|
274 | bindI18nStore?: string | false;
|
275 | /**
|
276 | * Set fallback value for Trans components without children
|
277 | * @default undefined
|
278 | */
|
279 | transEmptyNodeValue?: string;
|
280 | /**
|
281 | * Set it to false if you do not want to use Suspense
|
282 | * @default true
|
283 | */
|
284 | useSuspense?: boolean;
|
285 | /**
|
286 | * Function to generate an i18nKey from the defaultValue (or Trans children)
|
287 | * when no key is provided.
|
288 | * By default, the defaultValue (Trans text) itself is used as the key.
|
289 | * If you want to require keys for all translations, supply a function
|
290 | * that always throws an error.
|
291 | * @default undefined
|
292 | */
|
293 | hashTransKey?(defaultValue: TOptionsBase['defaultValue']): TOptionsBase['defaultValue'];
|
294 | /**
|
295 | * Convert eg. <br/> found in translations to a react component of type br
|
296 | * @default true
|
297 | */
|
298 | transSupportBasicHtmlNodes?: boolean;
|
299 | /**
|
300 | * Which nodes not to convert in defaultValue generation in the Trans component.
|
301 | * @default ['br', 'strong', 'i', 'p']
|
302 | */
|
303 | transKeepBasicHtmlNodesFor?: readonly string[];
|
304 | /**
|
305 | * Wrap text nodes in a user-specified element.
|
306 | * @default ''
|
307 | */
|
308 | transWrapTextNodes?: string;
|
309 | /**
|
310 | * Optional keyPrefix that will be automatically applied to returned t function in useTranslation for example.
|
311 | * @default undefined
|
312 | */
|
313 | keyPrefix?: string;
|
314 | /**
|
315 | * Unescape function
|
316 | * by default it unescapes some basic html entities
|
317 | */
|
318 | unescape?(str: string): string;
|
319 | }
|
320 |
|
321 | export type ResourceKey =
|
322 | | string
|
323 | | {
|
324 | [key: string]: any;
|
325 | };
|
326 |
|
327 | export interface ResourceLanguage {
|
328 | [namespace: string]: ResourceKey;
|
329 | }
|
330 |
|
331 | export interface Resource {
|
332 | [language: string]: ResourceLanguage;
|
333 | }
|
334 |
|
335 | export interface InitOptions<T = object> extends PluginOptions<T> {
|
336 | /**
|
337 | * Logs info level to console output. Helps finding issues with loading not working.
|
338 | * @default false
|
339 | */
|
340 | debug?: boolean;
|
341 |
|
342 | /**
|
343 | * Resources to initialize with (if not using loading or not appending using addResourceBundle)
|
344 | * @default undefined
|
345 | */
|
346 | resources?: Resource;
|
347 |
|
348 | /**
|
349 | * Allow initializing with bundled resources while using a backend to load non bundled ones.
|
350 | * @default false
|
351 | */
|
352 | partialBundledLanguages?: boolean;
|
353 |
|
354 | /**
|
355 | * Language to use (overrides language detection)
|
356 | * @default undefined
|
357 | */
|
358 | lng?: string;
|
359 |
|
360 | /**
|
361 | * Language to use if translations in user language are not available.
|
362 | * @default 'dev'
|
363 | */
|
364 | fallbackLng?: false | FallbackLng;
|
365 |
|
366 | /**
|
367 | * Array of allowed languages
|
368 | * @default false
|
369 | */
|
370 | supportedLngs?: false | readonly string[];
|
371 |
|
372 | /**
|
373 | * If true will pass eg. en-US if finding en in supportedLngs
|
374 | * @default false
|
375 | */
|
376 | nonExplicitSupportedLngs?: boolean;
|
377 |
|
378 | /**
|
379 | * Language codes to lookup, given set language is
|
380 | * 'en-US': 'all' --> ['en-US', 'en', 'dev'],
|
381 | * 'currentOnly' --> 'en-US',
|
382 | * 'languageOnly' --> 'en'
|
383 | * @default 'all'
|
384 | */
|
385 | load?: 'all' | 'currentOnly' | 'languageOnly';
|
386 |
|
387 | /**
|
388 | * Array of languages to preload. Important on server-side to assert translations are loaded before rendering views.
|
389 | * @default false
|
390 | */
|
391 | preload?: false | readonly string[];
|
392 |
|
393 | /**
|
394 | * Language will be lowercased eg. en-US --> en-us
|
395 | * @default false
|
396 | */
|
397 | lowerCaseLng?: boolean;
|
398 |
|
399 | /**
|
400 | * Language will be lowercased EN --> en while leaving full locales like en-US
|
401 | * @default false
|
402 | */
|
403 | cleanCode?: boolean;
|
404 |
|
405 | /**
|
406 | * String or array of namespaces to load
|
407 | * @default 'translation'
|
408 | */
|
409 | ns?: string | readonly string[];
|
410 |
|
411 | /**
|
412 | * Default namespace used if not passed to translation function
|
413 | * @default 'translation'
|
414 | */
|
415 | defaultNS?: string | false | readonly string[];
|
416 |
|
417 | /**
|
418 | * String or array of namespaces to lookup key if not found in given namespace.
|
419 | * @default false
|
420 | */
|
421 | fallbackNS?: false | string | readonly string[];
|
422 |
|
423 | /**
|
424 | * Calls save missing key function on backend if key not found.
|
425 | * @default false
|
426 | */
|
427 | saveMissing?: boolean;
|
428 |
|
429 | /**
|
430 | * Calls save missing key function on backend if key not found also for plural forms.
|
431 | * @default false
|
432 | */
|
433 | saveMissingPlurals?: boolean;
|
434 |
|
435 | /**
|
436 | * Experimental: enable to update default values using the saveMissing
|
437 | * (Works only if defaultValue different from translated value.
|
438 | * Only useful on initial development or when keeping code as source of truth not changing values outside of code.
|
439 | * Only supported if backend supports it already)
|
440 | * @default false
|
441 | */
|
442 | updateMissing?: boolean;
|
443 |
|
444 | /**
|
445 | * @default 'fallback'
|
446 | */
|
447 | saveMissingTo?: 'current' | 'all' | 'fallback';
|
448 |
|
449 | /**
|
450 | * Used to not fallback to the key as default value, when using saveMissing functionality.
|
451 | * i.e. when using with i18next-http-backend this will result in having a key with an empty string value.
|
452 | * @default false
|
453 | */
|
454 | missingKeyNoValueFallbackToKey?: boolean;
|
455 |
|
456 | /**
|
457 | * Used for custom missing key handling (needs saveMissing set to true!)
|
458 | * @default false
|
459 | */
|
460 | missingKeyHandler?:
|
461 | | false
|
462 | | ((
|
463 | lngs: readonly string[],
|
464 | ns: string,
|
465 | key: string,
|
466 | fallbackValue: string,
|
467 | updateMissing: boolean,
|
468 | options: any,
|
469 | ) => void);
|
470 |
|
471 | /**
|
472 | * Receives a key that was not found in `t()` and returns a value, that will be returned by `t()`
|
473 | * @default noop
|
474 | */
|
475 | parseMissingKeyHandler?(key: string, defaultValue?: string): any;
|
476 |
|
477 | /**
|
478 | * Appends namespace to missing key
|
479 | * @default false
|
480 | */
|
481 | appendNamespaceToMissingKey?: boolean;
|
482 |
|
483 | /**
|
484 | * Gets called in case a interpolation value is undefined. This method will not be called if the value is empty string or null
|
485 | * @default noop
|
486 | */
|
487 | missingInterpolationHandler?: (text: string, value: any, options: InitOptions) => any;
|
488 |
|
489 | |
490 |
|
491 |
|
492 |
|
493 | simplifyPluralSuffix?: boolean;
|
494 |
|
495 | |
496 |
|
497 |
|
498 |
|
499 | postProcess?: false | string | readonly string[];
|
500 |
|
501 | |
502 |
|
503 |
|
504 |
|
505 | postProcessPassResolved?: boolean;
|
506 |
|
507 | |
508 |
|
509 |
|
510 |
|
511 | returnNull?: boolean;
|
512 |
|
513 | |
514 |
|
515 |
|
516 |
|
517 | returnEmptyString?: boolean;
|
518 |
|
519 | |
520 |
|
521 |
|
522 |
|
523 | returnObjects?: boolean;
|
524 |
|
525 | |
526 |
|
527 |
|
528 |
|
529 | returnDetails?: boolean;
|
530 |
|
531 | |
532 |
|
533 |
|
534 |
|
535 | returnedObjectHandler?(key: string, value: string, options: any): void;
|
536 |
|
537 | |
538 |
|
539 |
|
540 |
|
541 | joinArrays?: false | string;
|
542 |
|
543 | |
544 |
|
545 |
|
546 |
|
547 | overloadTranslationOptionHandler?(args: string[]): TOptions;
|
548 |
|
549 | |
550 |
|
551 |
|
552 | interpolation?: InterpolationOptions;
|
553 |
|
554 | |
555 |
|
556 |
|
557 |
|
558 | react?: ReactOptions;
|
559 |
|
560 | |
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 | initAsync?: boolean;
|
567 |
|
568 | |
569 |
|
570 |
|
571 | initImmediate?: boolean;
|
572 |
|
573 | |
574 |
|
575 |
|
576 |
|
577 | keySeparator?: false | string;
|
578 |
|
579 | |
580 |
|
581 |
|
582 |
|
583 | nsSeparator?: false | string;
|
584 |
|
585 | |
586 |
|
587 |
|
588 |
|
589 | pluralSeparator?: string;
|
590 |
|
591 | |
592 |
|
593 |
|
594 |
|
595 | contextSeparator?: string;
|
596 |
|
597 | |
598 |
|
599 |
|
600 |
|
601 | appendNamespaceToCIMode?: boolean;
|
602 |
|
603 | |
604 |
|
605 |
|
606 |
|
607 |
|
608 | compatibilityJSON?: 'v4';
|
609 |
|
610 | |
611 |
|
612 |
|
613 |
|
614 | locizeLastUsed?: {
|
615 | |
616 |
|
617 |
|
618 | projectId: string;
|
619 |
|
620 | |
621 |
|
622 |
|
623 | apiKey?: string;
|
624 |
|
625 | |
626 |
|
627 |
|
628 |
|
629 | referenceLng?: string;
|
630 |
|
631 | |
632 |
|
633 |
|
634 |
|
635 | version?: string;
|
636 |
|
637 | |
638 |
|
639 |
|
640 |
|
641 | debounceSubmit?: number;
|
642 |
|
643 | |
644 |
|
645 |
|
646 |
|
647 |
|
648 | allowedHosts?: readonly string[];
|
649 | };
|
650 |
|
651 | |
652 |
|
653 |
|
654 |
|
655 | ignoreJSONStructure?: boolean;
|
656 |
|
657 | |
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 | maxParallelReads?: number;
|
665 |
|
666 | |
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 | maxRetries?: number;
|
674 |
|
675 | |
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 | retryTimeout?: number;
|
682 | }
|
683 |
|
684 | export interface TOptionsBase {
|
685 | |
686 |
|
687 |
|
688 | defaultValue?: unknown;
|
689 | |
690 |
|
691 |
|
692 | count?: number;
|
693 | |
694 |
|
695 |
|
696 | ordinal?: boolean;
|
697 | |
698 |
|
699 |
|
700 | context?: unknown;
|
701 | |
702 |
|
703 |
|
704 | replace?: any;
|
705 | |
706 |
|
707 |
|
708 | lng?: string;
|
709 | |
710 |
|
711 |
|
712 | lngs?: readonly string[];
|
713 | |
714 |
|
715 |
|
716 | fallbackLng?: FallbackLng;
|
717 | |
718 |
|
719 |
|
720 | ns?: Namespace;
|
721 | |
722 |
|
723 |
|
724 | keySeparator?: false | string;
|
725 | |
726 |
|
727 |
|
728 | nsSeparator?: false | string;
|
729 | |
730 |
|
731 |
|
732 | returnObjects?: boolean;
|
733 | |
734 |
|
735 |
|
736 | returnDetails?: boolean;
|
737 | |
738 |
|
739 |
|
740 | joinArrays?: string;
|
741 | |
742 |
|
743 |
|
744 | postProcess?: string | readonly string[];
|
745 | |
746 |
|
747 |
|
748 | interpolation?: InterpolationOptions;
|
749 | }
|
750 |
|
751 | export type TOptions<TInterpolationMap extends object = $Dictionary> = TOptionsBase &
|
752 | TInterpolationMap;
|
753 |
|
754 | export type FlatNamespace = $PreservedValue<keyof TypeOptions['resources'], string>;
|
755 | export type Namespace<T = FlatNamespace> = T | readonly T[];
|
756 | export type DefaultNamespace = TypeOptions['defaultNS'];
|