Skip to content

Commit ddea5c0

Browse files
committed
release
1 parent 648c705 commit ddea5c0

File tree

2 files changed

+34
-54
lines changed

2 files changed

+34
-54
lines changed

react-i18next.js

+33-53
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,24 @@
118118
}
119119
};
120120

121-
function warn() {
122-
if (console && console.warn) {
121+
const warn = function () {
122+
if (console?.warn) {
123123
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
124124
args[_key] = arguments[_key];
125125
}
126126
if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
127127
console.warn(...args);
128128
}
129-
}
129+
};
130130
const alreadyWarned = {};
131-
function warnOnce() {
131+
const warnOnce = function () {
132132
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
133133
args[_key2] = arguments[_key2];
134134
}
135135
if (isString(args[0]) && alreadyWarned[args[0]]) return;
136136
if (isString(args[0])) alreadyWarned[args[0]] = new Date();
137137
warn(...args);
138-
}
138+
};
139139
const loadedClb = (i18n, cb) => () => {
140140
if (i18n.isInitialized) {
141141
cb();
@@ -159,36 +159,16 @@
159159
});
160160
i18n.loadLanguages(lng, loadedClb(i18n, cb));
161161
};
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-
};
178162
const hasLoadedNamespace = function (ns, i18n) {
179163
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
180164
if (!i18n.languages || !i18n.languages.length) {
181165
warnOnce('i18n.languages were undefined or empty', i18n.languages);
182166
return true;
183167
}
184-
const isNewerI18next = i18n.options.ignoreJSONStructure !== undefined;
185-
if (!isNewerI18next) {
186-
return oldI18nextHasLoadedNamespace(ns, i18n, options);
187-
}
188168
return i18n.hasLoadedNamespace(ns, {
189169
lng: options.lng,
190170
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;
192172
}
193173
});
194174
};
@@ -249,14 +229,14 @@
249229

250230
const hasChildren = (node, checkLength) => {
251231
if (!node) return false;
252-
const base = node.props ? node.props.children : node.children;
232+
const base = node.props?.children ?? node.children;
253233
if (checkLength) return base.length > 0;
254234
return !!base;
255235
};
256236
const getChildren = node => {
257237
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;
260240
};
261241
const hasValidReactChildren = children => Array.isArray(children) && children.every(react.isValidElement);
262242
const getAsArray = data => Array.isArray(data) ? data : [data];
@@ -271,7 +251,7 @@
271251
if (!children) return '';
272252
let stringNode = '';
273253
const childrenArray = getAsArray(children);
274-
const keepArray = i18nOptions.transSupportBasicHtmlNodes && i18nOptions.transKeepBasicHtmlNodesFor ? i18nOptions.transKeepBasicHtmlNodesFor : [];
254+
const keepArray = i18nOptions?.transSupportBasicHtmlNodes ? i18nOptions.transKeepBasicHtmlNodesFor ?? [] : [];
275255
childrenArray.forEach((child, childIndex) => {
276256
if (isString(child)) {
277257
stringNode += `${child}`;
@@ -335,7 +315,7 @@
335315
const renderInner = (child, node, rootReactNode) => {
336316
const childs = getChildren(child);
337317
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;
339319
};
340320
const pushTranslatedJSX = (child, inner, mem, i, isVoid) => {
341321
if (child.dummy) {
@@ -361,7 +341,7 @@
361341
const reactNodes = getAsArray(reactNode);
362342
const astNodes = getAsArray(astNode);
363343
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);
365345
if (node.type === 'tag') {
366346
let tmp = reactNodes[parseInt(node.name, 10)];
367347
if (rootReactNode.length === 1 && !tmp) tmp = rootReactNode[0][node.name];
@@ -454,17 +434,17 @@
454434
const t = tFromProps || i18n.t.bind(i18n) || (k => k);
455435
const reactI18nextOptions = {
456436
...getDefaults(),
457-
...(i18n.options && i18n.options.react)
437+
...i18n.options?.react
458438
};
459-
let namespaces = ns || t.ns || i18n.options && i18n.options.defaultNS;
439+
let namespaces = ns || t.ns || i18n.options?.defaultNS;
460440
namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
461441
const nodeAsString = nodesToString(children, reactI18nextOptions);
462442
const defaultValue = defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || i18nKey;
463443
const {
464444
hashTransKey
465445
} = reactI18nextOptions;
466446
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) {
468448
values = values && Object.keys(values).length > 0 ? {
469449
...values,
470450
...i18n.options.interpolation.defaultVariables
@@ -500,7 +480,7 @@
500480
});
501481
}
502482
const content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
503-
const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
483+
const useAsParent = parent ?? reactI18nextOptions.defaultTransParent;
504484
return useAsParent ? react.createElement(useAsParent, additionalProps, content) : content;
505485
}
506486

@@ -519,13 +499,13 @@
519499
}
520500
addUsedNamespaces(namespaces) {
521501
namespaces.forEach(ns => {
522-
if (!this.usedNamespaces[ns]) this.usedNamespaces[ns] = true;
502+
this.usedNamespaces[ns] ??= true;
523503
});
524504
}
525505
getUsedNamespaces = () => Object.keys(this.usedNamespaces);
526506
}
527507
const composeInitialProps = ForComponent => async ctx => {
528-
const componentsInitialProps = ForComponent.getInitialProps ? await ForComponent.getInitialProps(ctx) : {};
508+
const componentsInitialProps = (await ForComponent.getInitialProps?.(ctx)) ?? {};
529509
const i18nInitialProps = getInitialProps();
530510
return {
531511
...componentsInitialProps,
@@ -534,7 +514,7 @@
534514
};
535515
const getInitialProps = () => {
536516
const i18n = getI18n();
537-
const namespaces = i18n.reportNamespaces ? i18n.reportNamespaces.getUsedNamespaces() : [];
517+
const namespaces = i18n.reportNamespaces?.getUsedNamespaces() ?? [];
538518
const ret = {};
539519
const initialI18nStore = {};
540520
i18n.languages.forEach(l => {
@@ -570,7 +550,7 @@
570550
defaultNS: defaultNSFromContext
571551
} = react.useContext(I18nContext) || {};
572552
const i18n = i18nFromProps || i18nFromContext || getI18n();
573-
const t = tFromProps || i18n && i18n.t.bind(i18n);
553+
const t = tFromProps || i18n?.t.bind(i18n);
574554
return Trans$1({
575555
children,
576556
count,
@@ -581,7 +561,7 @@
581561
values,
582562
defaults,
583563
components,
584-
ns: ns || t && t.ns || defaultNSFromContext || i18n && i18n.options && i18n.options.defaultNS,
564+
ns: ns || t?.ns || defaultNSFromContext || i18n?.options?.defaultNS,
585565
i18n,
586566
t: tFromProps,
587567
shouldUnescape,
@@ -592,7 +572,7 @@
592572
const usePrevious = (value, ignore) => {
593573
const ref = react.useRef();
594574
react.useEffect(() => {
595-
ref.current = ignore ? ref.current : value;
575+
ref.current = value;
596576
}, [value, ignore]);
597577
return ref.current;
598578
};
@@ -622,7 +602,7 @@
622602
retNotReady.ready = false;
623603
return retNotReady;
624604
}
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.');
626606
const i18nOptions = {
627607
...getDefaults(),
628608
...i18n.options.react,
@@ -632,9 +612,9 @@
632612
useSuspense,
633613
keyPrefix
634614
} = i18nOptions;
635-
let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
615+
let namespaces = ns || defaultNSFromContext || i18n.options?.defaultNS;
636616
namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
637-
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces);
617+
i18n.reportNamespaces.addUsedNamespaces?.(namespaces);
638618
const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
639619
const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
640620
const getT = () => memoGetT;
@@ -667,11 +647,11 @@
667647
const boundReset = () => {
668648
if (isMounted.current) setT(getNewT);
669649
};
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);
672652
return () => {
673653
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));
675655
if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
676656
};
677657
}, [i18n, joinedNS]);
@@ -729,18 +709,18 @@
729709
};
730710
};
731711

732-
function Translation(props) {
733-
const {
712+
const Translation = _ref => {
713+
let {
734714
ns,
735715
children,
736716
...options
737-
} = props;
717+
} = _ref;
738718
const [t, i18n, ready] = useTranslation(ns, options);
739719
return children(t, {
740720
i18n,
741721
lng: i18n.language
742722
}, ready);
743-
}
723+
};
744724

745725
function I18nextProvider(_ref) {
746726
let {
@@ -766,7 +746,7 @@
766746
i18n: i18nFromContext
767747
} = react.useContext(I18nContext) || {};
768748
const i18n = i18nFromProps || i18nFromContext || getI18n();
769-
if (i18n.options && i18n.options.isClone) return;
749+
if (i18n.options?.isClone) return;
770750
if (initialI18nStore && !i18n.initializedStoreOnce) {
771751
i18n.services.resourceStore.data = initialI18nStore;
772752
i18n.options.ns = Object.values(initialI18nStore).reduce((mem, lngResources) => {

0 commit comments

Comments
 (0)