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