react-i18next 14.1.3 → 15.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/dist/amd/react-i18next.js +36 -54
- package/dist/amd/react-i18next.min.js +1 -1
- package/dist/commonjs/Trans.js +2 -2
- package/dist/commonjs/TransWithoutContext.js +10 -10
- package/dist/commonjs/Translation.js +6 -5
- package/dist/commonjs/context.js +6 -4
- package/dist/commonjs/useSSR.js +1 -1
- package/dist/commonjs/useTranslation.js +6 -6
- package/dist/commonjs/utils.js +9 -29
- package/dist/es/I18nextProvider.js +5 -6
- package/dist/es/Trans.js +18 -19
- package/dist/es/TransWithoutContext.js +26 -27
- package/dist/es/Translation.js +6 -7
- package/dist/es/context.js +6 -4
- package/dist/es/defaults.js +1 -2
- package/dist/es/package.json +1 -1
- package/dist/es/useSSR.js +2 -3
- package/dist/es/useTranslation.js +7 -8
- package/dist/es/utils.js +7 -34
- package/dist/es/withSSR.js +5 -6
- package/dist/es/withTranslation.js +27 -31
- package/dist/umd/react-i18next.js +36 -54
- package/dist/umd/react-i18next.min.js +1 -1
- package/icu.macro.d.ts +5 -5
- package/index.d.ts +2 -2
- package/package.json +23 -30
- package/react-i18next.js +36 -54
- package/react-i18next.min.js +1 -1
- package/src/Trans.js +2 -3
- package/src/TransWithoutContext.js +12 -15
- package/src/Translation.js +2 -3
- package/src/context.js +6 -6
- package/src/useSSR.js +1 -1
- package/src/useTranslation.js +6 -6
- package/src/utils.js +7 -60
- package/vitest.workspace.typescript.mts +11 -0
package/dist/es/Trans.js
CHANGED
|
@@ -2,29 +2,28 @@ import { useContext } from 'react';
|
|
|
2
2
|
import { nodesToString, Trans as TransWithoutContext } from './TransWithoutContext.js';
|
|
3
3
|
import { getI18n, I18nContext } from './context.js';
|
|
4
4
|
export { nodesToString };
|
|
5
|
-
export function Trans(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
} = _ref;
|
|
5
|
+
export function Trans({
|
|
6
|
+
children,
|
|
7
|
+
count,
|
|
8
|
+
parent,
|
|
9
|
+
i18nKey,
|
|
10
|
+
context,
|
|
11
|
+
tOptions = {},
|
|
12
|
+
values,
|
|
13
|
+
defaults,
|
|
14
|
+
components,
|
|
15
|
+
ns,
|
|
16
|
+
i18n: i18nFromProps,
|
|
17
|
+
t: tFromProps,
|
|
18
|
+
shouldUnescape,
|
|
19
|
+
...additionalProps
|
|
20
|
+
}) {
|
|
22
21
|
const {
|
|
23
22
|
i18n: i18nFromContext,
|
|
24
23
|
defaultNS: defaultNSFromContext
|
|
25
24
|
} = useContext(I18nContext) || {};
|
|
26
25
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
27
|
-
const t = tFromProps || i18n
|
|
26
|
+
const t = tFromProps || i18n?.t.bind(i18n);
|
|
28
27
|
return TransWithoutContext({
|
|
29
28
|
children,
|
|
30
29
|
count,
|
|
@@ -35,7 +34,7 @@ export function Trans(_ref) {
|
|
|
35
34
|
values,
|
|
36
35
|
defaults,
|
|
37
36
|
components,
|
|
38
|
-
ns: ns || t
|
|
37
|
+
ns: ns || t?.ns || defaultNSFromContext || i18n?.options?.defaultNS,
|
|
39
38
|
i18n,
|
|
40
39
|
t: tFromProps,
|
|
41
40
|
shouldUnescape,
|
|
@@ -5,14 +5,14 @@ import { getDefaults } from './defaults.js';
|
|
|
5
5
|
import { getI18n } from './i18nInstance.js';
|
|
6
6
|
const hasChildren = (node, checkLength) => {
|
|
7
7
|
if (!node) return false;
|
|
8
|
-
const base = node.props
|
|
8
|
+
const base = node.props?.children ?? node.children;
|
|
9
9
|
if (checkLength) return base.length > 0;
|
|
10
10
|
return !!base;
|
|
11
11
|
};
|
|
12
12
|
const getChildren = node => {
|
|
13
13
|
if (!node) return [];
|
|
14
|
-
const children = node.props
|
|
15
|
-
return node.props
|
|
14
|
+
const children = node.props?.children ?? node.children;
|
|
15
|
+
return node.props?.i18nIsDynamicList ? getAsArray(children) : children;
|
|
16
16
|
};
|
|
17
17
|
const hasValidReactChildren = children => Array.isArray(children) && children.every(isValidElement);
|
|
18
18
|
const getAsArray = data => Array.isArray(data) ? data : [data];
|
|
@@ -27,7 +27,7 @@ export const nodesToString = (children, i18nOptions) => {
|
|
|
27
27
|
if (!children) return '';
|
|
28
28
|
let stringNode = '';
|
|
29
29
|
const childrenArray = getAsArray(children);
|
|
30
|
-
const keepArray = i18nOptions
|
|
30
|
+
const keepArray = i18nOptions?.transSupportBasicHtmlNodes ? i18nOptions.transKeepBasicHtmlNodesFor ?? [] : [];
|
|
31
31
|
childrenArray.forEach((child, childIndex) => {
|
|
32
32
|
if (isString(child)) {
|
|
33
33
|
stringNode += `${child}`;
|
|
@@ -91,7 +91,7 @@ const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, s
|
|
|
91
91
|
const renderInner = (child, node, rootReactNode) => {
|
|
92
92
|
const childs = getChildren(child);
|
|
93
93
|
const mappedChildren = mapAST(childs, node.children, rootReactNode);
|
|
94
|
-
return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props
|
|
94
|
+
return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props?.i18nIsDynamicList ? childs : mappedChildren;
|
|
95
95
|
};
|
|
96
96
|
const pushTranslatedJSX = (child, inner, mem, i, isVoid) => {
|
|
97
97
|
if (child.dummy) {
|
|
@@ -117,7 +117,7 @@ const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, s
|
|
|
117
117
|
const reactNodes = getAsArray(reactNode);
|
|
118
118
|
const astNodes = getAsArray(astNode);
|
|
119
119
|
return astNodes.reduce((mem, node, i) => {
|
|
120
|
-
const translationContent = node.children
|
|
120
|
+
const translationContent = node.children?.[0]?.content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
|
|
121
121
|
if (node.type === 'tag') {
|
|
122
122
|
let tmp = reactNodes[parseInt(node.name, 10)];
|
|
123
123
|
if (rootReactNode.length === 1 && !tmp) tmp = rootReactNode[0][node.name];
|
|
@@ -185,23 +185,22 @@ const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, s
|
|
|
185
185
|
}], ast, getAsArray(children || []));
|
|
186
186
|
return getChildren(result[0]);
|
|
187
187
|
};
|
|
188
|
-
export function Trans(
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
} = _ref;
|
|
188
|
+
export function Trans({
|
|
189
|
+
children,
|
|
190
|
+
count,
|
|
191
|
+
parent,
|
|
192
|
+
i18nKey,
|
|
193
|
+
context,
|
|
194
|
+
tOptions = {},
|
|
195
|
+
values,
|
|
196
|
+
defaults,
|
|
197
|
+
components,
|
|
198
|
+
ns,
|
|
199
|
+
i18n: i18nFromProps,
|
|
200
|
+
t: tFromProps,
|
|
201
|
+
shouldUnescape,
|
|
202
|
+
...additionalProps
|
|
203
|
+
}) {
|
|
205
204
|
const i18n = i18nFromProps || getI18n();
|
|
206
205
|
if (!i18n) {
|
|
207
206
|
warnOnce('You will need to pass in an i18next instance by using i18nextReactModule');
|
|
@@ -210,9 +209,9 @@ export function Trans(_ref) {
|
|
|
210
209
|
const t = tFromProps || i18n.t.bind(i18n) || (k => k);
|
|
211
210
|
const reactI18nextOptions = {
|
|
212
211
|
...getDefaults(),
|
|
213
|
-
...
|
|
212
|
+
...i18n.options?.react
|
|
214
213
|
};
|
|
215
|
-
let namespaces = ns || t.ns || i18n.options
|
|
214
|
+
let namespaces = ns || t.ns || i18n.options?.defaultNS;
|
|
216
215
|
namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
|
|
217
216
|
const nodeAsString = nodesToString(children, reactI18nextOptions);
|
|
218
217
|
const defaultValue = defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || i18nKey;
|
|
@@ -220,7 +219,7 @@ export function Trans(_ref) {
|
|
|
220
219
|
hashTransKey
|
|
221
220
|
} = reactI18nextOptions;
|
|
222
221
|
const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue);
|
|
223
|
-
if (i18n.options
|
|
222
|
+
if (i18n.options?.interpolation?.defaultVariables) {
|
|
224
223
|
values = values && Object.keys(values).length > 0 ? {
|
|
225
224
|
...values,
|
|
226
225
|
...i18n.options.interpolation.defaultVariables
|
|
@@ -256,6 +255,6 @@ export function Trans(_ref) {
|
|
|
256
255
|
});
|
|
257
256
|
}
|
|
258
257
|
const content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
|
|
259
|
-
const useAsParent = parent
|
|
258
|
+
const useAsParent = parent ?? reactI18nextOptions.defaultTransParent;
|
|
260
259
|
return useAsParent ? createElement(useAsParent, additionalProps, content) : content;
|
|
261
260
|
}
|
package/dist/es/Translation.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { useTranslation } from './useTranslation.js';
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} = props;
|
|
2
|
+
export const Translation = ({
|
|
3
|
+
ns,
|
|
4
|
+
children,
|
|
5
|
+
...options
|
|
6
|
+
}) => {
|
|
8
7
|
const [t, i18n, ready] = useTranslation(ns, options);
|
|
9
8
|
return children(t, {
|
|
10
9
|
i18n,
|
|
11
10
|
lng: i18n.language
|
|
12
11
|
}, ready);
|
|
13
|
-
}
|
|
12
|
+
};
|
package/dist/es/context.js
CHANGED
|
@@ -10,13 +10,15 @@ export class ReportNamespaces {
|
|
|
10
10
|
}
|
|
11
11
|
addUsedNamespaces(namespaces) {
|
|
12
12
|
namespaces.forEach(ns => {
|
|
13
|
-
|
|
13
|
+
this.usedNamespaces[ns] ??= true;
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
-
getUsedNamespaces
|
|
16
|
+
getUsedNamespaces() {
|
|
17
|
+
return Object.keys(this.usedNamespaces);
|
|
18
|
+
}
|
|
17
19
|
}
|
|
18
20
|
export const composeInitialProps = ForComponent => async ctx => {
|
|
19
|
-
const componentsInitialProps =
|
|
21
|
+
const componentsInitialProps = (await ForComponent.getInitialProps?.(ctx)) ?? {};
|
|
20
22
|
const i18nInitialProps = getInitialProps();
|
|
21
23
|
return {
|
|
22
24
|
...componentsInitialProps,
|
|
@@ -25,7 +27,7 @@ export const composeInitialProps = ForComponent => async ctx => {
|
|
|
25
27
|
};
|
|
26
28
|
export const getInitialProps = () => {
|
|
27
29
|
const i18n = getI18n();
|
|
28
|
-
const namespaces = i18n.reportNamespaces
|
|
30
|
+
const namespaces = i18n.reportNamespaces?.getUsedNamespaces() ?? [];
|
|
29
31
|
const ret = {};
|
|
30
32
|
const initialI18nStore = {};
|
|
31
33
|
i18n.languages.forEach(l => {
|
package/dist/es/defaults.js
CHANGED
|
@@ -9,8 +9,7 @@ let defaultOptions = {
|
|
|
9
9
|
useSuspense: true,
|
|
10
10
|
unescape
|
|
11
11
|
};
|
|
12
|
-
export const setDefaults =
|
|
13
|
-
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
12
|
+
export const setDefaults = (options = {}) => {
|
|
14
13
|
defaultOptions = {
|
|
15
14
|
...defaultOptions,
|
|
16
15
|
...options
|
package/dist/es/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"type":"module","version":"
|
|
1
|
+
{"type":"module","version":"15.0.1"}
|
package/dist/es/useSSR.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { useContext } from 'react';
|
|
2
2
|
import { getI18n, I18nContext } from './context.js';
|
|
3
|
-
export const useSSR =
|
|
4
|
-
let props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
3
|
+
export const useSSR = (initialI18nStore, initialLanguage, props = {}) => {
|
|
5
4
|
const {
|
|
6
5
|
i18n: i18nFromProps
|
|
7
6
|
} = props;
|
|
@@ -9,7 +8,7 @@ export const useSSR = function (initialI18nStore, initialLanguage) {
|
|
|
9
8
|
i18n: i18nFromContext
|
|
10
9
|
} = useContext(I18nContext) || {};
|
|
11
10
|
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
12
|
-
if (i18n.options
|
|
11
|
+
if (i18n.options?.isClone) return;
|
|
13
12
|
if (initialI18nStore && !i18n.initializedStoreOnce) {
|
|
14
13
|
i18n.services.resourceStore.data = initialI18nStore;
|
|
15
14
|
i18n.options.ns = Object.values(initialI18nStore).reduce((mem, lngResources) => {
|
|
@@ -10,8 +10,7 @@ const usePrevious = (value, ignore) => {
|
|
|
10
10
|
};
|
|
11
11
|
const alwaysNewT = (i18n, language, namespace, keyPrefix) => i18n.getFixedT(language, namespace, keyPrefix);
|
|
12
12
|
const useMemoizedT = (i18n, language, namespace, keyPrefix) => useCallback(alwaysNewT(i18n, language, namespace, keyPrefix), [i18n, language, namespace, keyPrefix]);
|
|
13
|
-
export const useTranslation =
|
|
14
|
-
let props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
13
|
+
export const useTranslation = (ns, props = {}) => {
|
|
15
14
|
const {
|
|
16
15
|
i18n: i18nFromProps
|
|
17
16
|
} = props;
|
|
@@ -34,7 +33,7 @@ export const useTranslation = function (ns) {
|
|
|
34
33
|
retNotReady.ready = false;
|
|
35
34
|
return retNotReady;
|
|
36
35
|
}
|
|
37
|
-
if (i18n.options.react
|
|
36
|
+
if (i18n.options.react?.wait) warnOnce('It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
|
|
38
37
|
const i18nOptions = {
|
|
39
38
|
...getDefaults(),
|
|
40
39
|
...i18n.options.react,
|
|
@@ -44,9 +43,9 @@ export const useTranslation = function (ns) {
|
|
|
44
43
|
useSuspense,
|
|
45
44
|
keyPrefix
|
|
46
45
|
} = i18nOptions;
|
|
47
|
-
let namespaces = ns || defaultNSFromContext || i18n.options
|
|
46
|
+
let namespaces = ns || defaultNSFromContext || i18n.options?.defaultNS;
|
|
48
47
|
namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
|
|
49
|
-
|
|
48
|
+
i18n.reportNamespaces.addUsedNamespaces?.(namespaces);
|
|
50
49
|
const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
|
|
51
50
|
const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
|
|
52
51
|
const getT = () => memoGetT;
|
|
@@ -79,11 +78,11 @@ export const useTranslation = function (ns) {
|
|
|
79
78
|
const boundReset = () => {
|
|
80
79
|
if (isMounted.current) setT(getNewT);
|
|
81
80
|
};
|
|
82
|
-
if (bindI18n
|
|
83
|
-
if (bindI18nStore
|
|
81
|
+
if (bindI18n) i18n?.on(bindI18n, boundReset);
|
|
82
|
+
if (bindI18nStore) i18n?.store.on(bindI18nStore, boundReset);
|
|
84
83
|
return () => {
|
|
85
84
|
isMounted.current = false;
|
|
86
|
-
if (
|
|
85
|
+
if (i18n) bindI18n?.split(' ').forEach(e => i18n.off(e, boundReset));
|
|
87
86
|
if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
|
|
88
87
|
};
|
|
89
88
|
}, [i18n, joinedNS]);
|
package/dist/es/utils.js
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
|
-
export
|
|
2
|
-
if (console
|
|
3
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
4
|
-
args[_key] = arguments[_key];
|
|
5
|
-
}
|
|
1
|
+
export const warn = (...args) => {
|
|
2
|
+
if (console?.warn) {
|
|
6
3
|
if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
|
|
7
4
|
console.warn(...args);
|
|
8
5
|
}
|
|
9
|
-
}
|
|
6
|
+
};
|
|
10
7
|
const alreadyWarned = {};
|
|
11
|
-
export
|
|
12
|
-
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
13
|
-
args[_key2] = arguments[_key2];
|
|
14
|
-
}
|
|
8
|
+
export const warnOnce = (...args) => {
|
|
15
9
|
if (isString(args[0]) && alreadyWarned[args[0]]) return;
|
|
16
10
|
if (isString(args[0])) alreadyWarned[args[0]] = new Date();
|
|
17
11
|
warn(...args);
|
|
18
|
-
}
|
|
12
|
+
};
|
|
19
13
|
const loadedClb = (i18n, cb) => () => {
|
|
20
14
|
if (i18n.isInitialized) {
|
|
21
15
|
cb();
|
|
@@ -39,36 +33,15 @@ export const loadLanguages = (i18n, lng, ns, cb) => {
|
|
|
39
33
|
});
|
|
40
34
|
i18n.loadLanguages(lng, loadedClb(i18n, cb));
|
|
41
35
|
};
|
|
42
|
-
const
|
|
43
|
-
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
44
|
-
const lng = i18n.languages[0];
|
|
45
|
-
const fallbackLng = i18n.options ? i18n.options.fallbackLng : false;
|
|
46
|
-
const lastLng = i18n.languages[i18n.languages.length - 1];
|
|
47
|
-
if (lng.toLowerCase() === 'cimode') return true;
|
|
48
|
-
const loadNotPending = (l, n) => {
|
|
49
|
-
const loadState = i18n.services.backendConnector.state[`${l}|${n}`];
|
|
50
|
-
return loadState === -1 || loadState === 2;
|
|
51
|
-
};
|
|
52
|
-
if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18n.services.backendConnector.backend && i18n.isLanguageChangingTo && !loadNotPending(i18n.isLanguageChangingTo, ns)) return false;
|
|
53
|
-
if (i18n.hasResourceBundle(lng, ns)) return true;
|
|
54
|
-
if (!i18n.services.backendConnector.backend || i18n.options.resources && !i18n.options.partialBundledLanguages) return true;
|
|
55
|
-
if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
|
|
56
|
-
return false;
|
|
57
|
-
};
|
|
58
|
-
export const hasLoadedNamespace = function (ns, i18n) {
|
|
59
|
-
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
36
|
+
export const hasLoadedNamespace = (ns, i18n, options = {}) => {
|
|
60
37
|
if (!i18n.languages || !i18n.languages.length) {
|
|
61
38
|
warnOnce('i18n.languages were undefined or empty', i18n.languages);
|
|
62
39
|
return true;
|
|
63
40
|
}
|
|
64
|
-
const isNewerI18next = i18n.options.ignoreJSONStructure !== undefined;
|
|
65
|
-
if (!isNewerI18next) {
|
|
66
|
-
return oldI18nextHasLoadedNamespace(ns, i18n, options);
|
|
67
|
-
}
|
|
68
41
|
return i18n.hasLoadedNamespace(ns, {
|
|
69
42
|
lng: options.lng,
|
|
70
43
|
precheck: (i18nInstance, loadNotPending) => {
|
|
71
|
-
if (options.bindI18n
|
|
44
|
+
if (options.bindI18n?.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
|
|
72
45
|
}
|
|
73
46
|
});
|
|
74
47
|
};
|
package/dist/es/withSSR.js
CHANGED
|
@@ -3,12 +3,11 @@ import { useSSR } from './useSSR.js';
|
|
|
3
3
|
import { composeInitialProps } from './context.js';
|
|
4
4
|
import { getDisplayName } from './utils.js';
|
|
5
5
|
export const withSSR = () => function Extend(WrappedComponent) {
|
|
6
|
-
function I18nextWithSSR(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} = _ref;
|
|
6
|
+
function I18nextWithSSR({
|
|
7
|
+
initialI18nStore,
|
|
8
|
+
initialLanguage,
|
|
9
|
+
...rest
|
|
10
|
+
}) {
|
|
12
11
|
useSSR(initialI18nStore, initialLanguage);
|
|
13
12
|
return createElement(WrappedComponent, {
|
|
14
13
|
...rest
|
|
@@ -1,36 +1,32 @@
|
|
|
1
1
|
import { createElement, forwardRef as forwardRefReact } from 'react';
|
|
2
2
|
import { useTranslation } from './useTranslation.js';
|
|
3
3
|
import { getDisplayName } from './utils.js';
|
|
4
|
-
export const withTranslation = function (
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
passDownProps.ref = forwardedRef;
|
|
24
|
-
} else if (!options.withRef && forwardedRef) {
|
|
25
|
-
passDownProps.forwardedRef = forwardedRef;
|
|
26
|
-
}
|
|
27
|
-
return createElement(WrappedComponent, passDownProps);
|
|
4
|
+
export const withTranslation = (ns, options = {}) => function Extend(WrappedComponent) {
|
|
5
|
+
function I18nextWithTranslation({
|
|
6
|
+
forwardedRef,
|
|
7
|
+
...rest
|
|
8
|
+
}) {
|
|
9
|
+
const [t, i18n, ready] = useTranslation(ns, {
|
|
10
|
+
...rest,
|
|
11
|
+
keyPrefix: options.keyPrefix
|
|
12
|
+
});
|
|
13
|
+
const passDownProps = {
|
|
14
|
+
...rest,
|
|
15
|
+
t,
|
|
16
|
+
i18n,
|
|
17
|
+
tReady: ready
|
|
18
|
+
};
|
|
19
|
+
if (options.withRef && forwardedRef) {
|
|
20
|
+
passDownProps.ref = forwardedRef;
|
|
21
|
+
} else if (!options.withRef && forwardedRef) {
|
|
22
|
+
passDownProps.forwardedRef = forwardedRef;
|
|
28
23
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
};
|
|
24
|
+
return createElement(WrappedComponent, passDownProps);
|
|
25
|
+
}
|
|
26
|
+
I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName(WrappedComponent)})`;
|
|
27
|
+
I18nextWithTranslation.WrappedComponent = WrappedComponent;
|
|
28
|
+
const forwardRef = (props, ref) => createElement(I18nextWithTranslation, Object.assign({}, props, {
|
|
29
|
+
forwardedRef: ref
|
|
30
|
+
}));
|
|
31
|
+
return options.withRef ? forwardRefReact(forwardRef) : I18nextWithTranslation;
|
|
36
32
|
};
|