|
118 | 118 | }
|
119 | 119 | };
|
120 | 120 |
|
121 |
| - function warn() { |
122 |
| - if (console && console.warn) { |
| 121 | + const warn = function () { |
| 122 | + if (console?.warn) { |
123 | 123 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
124 | 124 | args[_key] = arguments[_key];
|
125 | 125 | }
|
126 | 126 | if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
|
127 | 127 | console.warn(...args);
|
128 | 128 | }
|
129 |
| - } |
| 129 | + }; |
130 | 130 | const alreadyWarned = {};
|
131 |
| - function warnOnce() { |
| 131 | + const warnOnce = function () { |
132 | 132 | for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
133 | 133 | args[_key2] = arguments[_key2];
|
134 | 134 | }
|
135 | 135 | if (isString(args[0]) && alreadyWarned[args[0]]) return;
|
136 | 136 | if (isString(args[0])) alreadyWarned[args[0]] = new Date();
|
137 | 137 | warn(...args);
|
138 |
| - } |
| 138 | + }; |
139 | 139 | const loadedClb = (i18n, cb) => () => {
|
140 | 140 | if (i18n.isInitialized) {
|
141 | 141 | cb();
|
|
159 | 159 | });
|
160 | 160 | i18n.loadLanguages(lng, loadedClb(i18n, cb));
|
161 | 161 | };
|
162 |
| - const oldI18nextHasLoadedNamespace = function (ns, i18n) { |
163 |
| - let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; |
164 |
| - const lng = i18n.languages[0]; |
165 |
| - const fallbackLng = i18n.options ? i18n.options.fallbackLng : false; |
166 |
| - const lastLng = i18n.languages[i18n.languages.length - 1]; |
167 |
| - if (lng.toLowerCase() === 'cimode') return true; |
168 |
| - const loadNotPending = (l, n) => { |
169 |
| - const loadState = i18n.services.backendConnector.state[`${l}|${n}`]; |
170 |
| - return loadState === -1 || loadState === 2; |
171 |
| - }; |
172 |
| - if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18n.services.backendConnector.backend && i18n.isLanguageChangingTo && !loadNotPending(i18n.isLanguageChangingTo, ns)) return false; |
173 |
| - if (i18n.hasResourceBundle(lng, ns)) return true; |
174 |
| - if (!i18n.services.backendConnector.backend || i18n.options.resources && !i18n.options.partialBundledLanguages) return true; |
175 |
| - if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true; |
176 |
| - return false; |
177 |
| - }; |
178 | 162 | const hasLoadedNamespace = function (ns, i18n) {
|
179 | 163 | let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
180 | 164 | if (!i18n.languages || !i18n.languages.length) {
|
181 | 165 | warnOnce('i18n.languages were undefined or empty', i18n.languages);
|
182 | 166 | return true;
|
183 | 167 | }
|
184 |
| - const isNewerI18next = i18n.options.ignoreJSONStructure !== undefined; |
185 |
| - if (!isNewerI18next) { |
186 |
| - return oldI18nextHasLoadedNamespace(ns, i18n, options); |
187 |
| - } |
188 | 168 | return i18n.hasLoadedNamespace(ns, {
|
189 | 169 | lng: options.lng,
|
190 | 170 | precheck: (i18nInstance, loadNotPending) => {
|
191 |
| - if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false; |
| 171 | + if (options.bindI18n?.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false; |
192 | 172 | }
|
193 | 173 | });
|
194 | 174 | };
|
|
249 | 229 |
|
250 | 230 | const hasChildren = (node, checkLength) => {
|
251 | 231 | if (!node) return false;
|
252 |
| - const base = node.props ? node.props.children : node.children; |
| 232 | + const base = node.props?.children ?? node.children; |
253 | 233 | if (checkLength) return base.length > 0;
|
254 | 234 | return !!base;
|
255 | 235 | };
|
256 | 236 | const getChildren = node => {
|
257 | 237 | if (!node) return [];
|
258 |
| - const children = node.props ? node.props.children : node.children; |
259 |
| - return node.props && node.props.i18nIsDynamicList ? getAsArray(children) : children; |
| 238 | + const children = node.props?.children ?? node.children; |
| 239 | + return node.props?.i18nIsDynamicList ? getAsArray(children) : children; |
260 | 240 | };
|
261 | 241 | const hasValidReactChildren = children => Array.isArray(children) && children.every(react.isValidElement);
|
262 | 242 | const getAsArray = data => Array.isArray(data) ? data : [data];
|
|
271 | 251 | if (!children) return '';
|
272 | 252 | let stringNode = '';
|
273 | 253 | const childrenArray = getAsArray(children);
|
274 |
| - const keepArray = i18nOptions.transSupportBasicHtmlNodes && i18nOptions.transKeepBasicHtmlNodesFor ? i18nOptions.transKeepBasicHtmlNodesFor : []; |
| 254 | + const keepArray = i18nOptions?.transSupportBasicHtmlNodes ? i18nOptions.transKeepBasicHtmlNodesFor ?? [] : []; |
275 | 255 | childrenArray.forEach((child, childIndex) => {
|
276 | 256 | if (isString(child)) {
|
277 | 257 | stringNode += `${child}`;
|
|
335 | 315 | const renderInner = (child, node, rootReactNode) => {
|
336 | 316 | const childs = getChildren(child);
|
337 | 317 | const mappedChildren = mapAST(childs, node.children, rootReactNode);
|
338 |
| - return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props && child.props.i18nIsDynamicList ? childs : mappedChildren; |
| 318 | + return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props?.i18nIsDynamicList ? childs : mappedChildren; |
339 | 319 | };
|
340 | 320 | const pushTranslatedJSX = (child, inner, mem, i, isVoid) => {
|
341 | 321 | if (child.dummy) {
|
|
361 | 341 | const reactNodes = getAsArray(reactNode);
|
362 | 342 | const astNodes = getAsArray(astNode);
|
363 | 343 | return astNodes.reduce((mem, node, i) => {
|
364 |
| - const translationContent = node.children && node.children[0] && node.children[0].content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language); |
| 344 | + const translationContent = node.children?.[0]?.content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language); |
365 | 345 | if (node.type === 'tag') {
|
366 | 346 | let tmp = reactNodes[parseInt(node.name, 10)];
|
367 | 347 | if (rootReactNode.length === 1 && !tmp) tmp = rootReactNode[0][node.name];
|
|
454 | 434 | const t = tFromProps || i18n.t.bind(i18n) || (k => k);
|
455 | 435 | const reactI18nextOptions = {
|
456 | 436 | ...getDefaults(),
|
457 |
| - ...(i18n.options && i18n.options.react) |
| 437 | + ...i18n.options?.react |
458 | 438 | };
|
459 |
| - let namespaces = ns || t.ns || i18n.options && i18n.options.defaultNS; |
| 439 | + let namespaces = ns || t.ns || i18n.options?.defaultNS; |
460 | 440 | namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
|
461 | 441 | const nodeAsString = nodesToString(children, reactI18nextOptions);
|
462 | 442 | const defaultValue = defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || i18nKey;
|
463 | 443 | const {
|
464 | 444 | hashTransKey
|
465 | 445 | } = reactI18nextOptions;
|
466 | 446 | const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue);
|
467 |
| - if (i18n.options && i18n.options.interpolation && i18n.options.interpolation.defaultVariables) { |
| 447 | + if (i18n.options?.interpolation?.defaultVariables) { |
468 | 448 | values = values && Object.keys(values).length > 0 ? {
|
469 | 449 | ...values,
|
470 | 450 | ...i18n.options.interpolation.defaultVariables
|
|
500 | 480 | });
|
501 | 481 | }
|
502 | 482 | const content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
|
503 |
| - const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent; |
| 483 | + const useAsParent = parent ?? reactI18nextOptions.defaultTransParent; |
504 | 484 | return useAsParent ? react.createElement(useAsParent, additionalProps, content) : content;
|
505 | 485 | }
|
506 | 486 |
|
|
519 | 499 | }
|
520 | 500 | addUsedNamespaces(namespaces) {
|
521 | 501 | namespaces.forEach(ns => {
|
522 |
| - if (!this.usedNamespaces[ns]) this.usedNamespaces[ns] = true; |
| 502 | + this.usedNamespaces[ns] ??= true; |
523 | 503 | });
|
524 | 504 | }
|
525 | 505 | getUsedNamespaces = () => Object.keys(this.usedNamespaces);
|
526 | 506 | }
|
527 | 507 | const composeInitialProps = ForComponent => async ctx => {
|
528 |
| - const componentsInitialProps = ForComponent.getInitialProps ? await ForComponent.getInitialProps(ctx) : {}; |
| 508 | + const componentsInitialProps = (await ForComponent.getInitialProps?.(ctx)) ?? {}; |
529 | 509 | const i18nInitialProps = getInitialProps();
|
530 | 510 | return {
|
531 | 511 | ...componentsInitialProps,
|
|
534 | 514 | };
|
535 | 515 | const getInitialProps = () => {
|
536 | 516 | const i18n = getI18n();
|
537 |
| - const namespaces = i18n.reportNamespaces ? i18n.reportNamespaces.getUsedNamespaces() : []; |
| 517 | + const namespaces = i18n.reportNamespaces?.getUsedNamespaces() ?? []; |
538 | 518 | const ret = {};
|
539 | 519 | const initialI18nStore = {};
|
540 | 520 | i18n.languages.forEach(l => {
|
|
570 | 550 | defaultNS: defaultNSFromContext
|
571 | 551 | } = react.useContext(I18nContext) || {};
|
572 | 552 | const i18n = i18nFromProps || i18nFromContext || getI18n();
|
573 |
| - const t = tFromProps || i18n && i18n.t.bind(i18n); |
| 553 | + const t = tFromProps || i18n?.t.bind(i18n); |
574 | 554 | return Trans$1({
|
575 | 555 | children,
|
576 | 556 | count,
|
|
581 | 561 | values,
|
582 | 562 | defaults,
|
583 | 563 | components,
|
584 |
| - ns: ns || t && t.ns || defaultNSFromContext || i18n && i18n.options && i18n.options.defaultNS, |
| 564 | + ns: ns || t?.ns || defaultNSFromContext || i18n?.options?.defaultNS, |
585 | 565 | i18n,
|
586 | 566 | t: tFromProps,
|
587 | 567 | shouldUnescape,
|
|
592 | 572 | const usePrevious = (value, ignore) => {
|
593 | 573 | const ref = react.useRef();
|
594 | 574 | react.useEffect(() => {
|
595 |
| - ref.current = ignore ? ref.current : value; |
| 575 | + ref.current = value; |
596 | 576 | }, [value, ignore]);
|
597 | 577 | return ref.current;
|
598 | 578 | };
|
|
622 | 602 | retNotReady.ready = false;
|
623 | 603 | return retNotReady;
|
624 | 604 | }
|
625 |
| - if (i18n.options.react && i18n.options.react.wait !== undefined) warnOnce('It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.'); |
| 605 | + if (i18n.options.react?.wait) warnOnce('It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.'); |
626 | 606 | const i18nOptions = {
|
627 | 607 | ...getDefaults(),
|
628 | 608 | ...i18n.options.react,
|
|
632 | 612 | useSuspense,
|
633 | 613 | keyPrefix
|
634 | 614 | } = i18nOptions;
|
635 |
| - let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS; |
| 615 | + let namespaces = ns || defaultNSFromContext || i18n.options?.defaultNS; |
636 | 616 | namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
|
637 |
| - if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); |
| 617 | + i18n.reportNamespaces.addUsedNamespaces?.(namespaces); |
638 | 618 | const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
|
639 | 619 | const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
|
640 | 620 | const getT = () => memoGetT;
|
|
667 | 647 | const boundReset = () => {
|
668 | 648 | if (isMounted.current) setT(getNewT);
|
669 | 649 | };
|
670 |
| - if (bindI18n && i18n) i18n.on(bindI18n, boundReset); |
671 |
| - if (bindI18nStore && i18n) i18n.store.on(bindI18nStore, boundReset); |
| 650 | + if (bindI18n) i18n?.on(bindI18n, boundReset); |
| 651 | + if (bindI18nStore) i18n?.store.on(bindI18nStore, boundReset); |
672 | 652 | return () => {
|
673 | 653 | isMounted.current = false;
|
674 |
| - if (bindI18n && i18n) bindI18n.split(' ').forEach(e => i18n.off(e, boundReset)); |
| 654 | + if (i18n) bindI18n?.split(' ').forEach(e => i18n.off(e, boundReset)); |
675 | 655 | if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
|
676 | 656 | };
|
677 | 657 | }, [i18n, joinedNS]);
|
|
729 | 709 | };
|
730 | 710 | };
|
731 | 711 |
|
732 |
| - function Translation(props) { |
733 |
| - const { |
| 712 | + const Translation = _ref => { |
| 713 | + let { |
734 | 714 | ns,
|
735 | 715 | children,
|
736 | 716 | ...options
|
737 |
| - } = props; |
| 717 | + } = _ref; |
738 | 718 | const [t, i18n, ready] = useTranslation(ns, options);
|
739 | 719 | return children(t, {
|
740 | 720 | i18n,
|
741 | 721 | lng: i18n.language
|
742 | 722 | }, ready);
|
743 |
| - } |
| 723 | + }; |
744 | 724 |
|
745 | 725 | function I18nextProvider(_ref) {
|
746 | 726 | let {
|
|
766 | 746 | i18n: i18nFromContext
|
767 | 747 | } = react.useContext(I18nContext) || {};
|
768 | 748 | const i18n = i18nFromProps || i18nFromContext || getI18n();
|
769 |
| - if (i18n.options && i18n.options.isClone) return; |
| 749 | + if (i18n.options?.isClone) return; |
770 | 750 | if (initialI18nStore && !i18n.initializedStoreOnce) {
|
771 | 751 | i18n.services.resourceStore.data = initialI18nStore;
|
772 | 752 | i18n.options.ns = Object.values(initialI18nStore).reduce((mem, lngResources) => {
|
|
0 commit comments