Skip to content

Commit 0441701

Browse files
committed
release
1 parent 85349f9 commit 0441701

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 15.1.4
2+
3+
- Fix: warning each child should have a unique key [1820](https://github.com/i18next/react-i18next/pull/1820)
4+
15
### 15.1.3
26

37
- fix: Self-closing REACT components in translation strings should not attempt to replace the component's children [1815](https://github.com/i18next/react-i18next/issues/1815) [1816](https://github.com/i18next/react-i18next/pull/1816)

react-i18next.js

+36-14
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,40 @@
410410
}], ast, getAsArray(children || []));
411411
return getChildren(result[0]);
412412
};
413+
const fixComponentProps = (component, index, translation) => {
414+
const componentKey = component.key || index;
415+
const comp = react.cloneElement(component, {
416+
key: componentKey
417+
});
418+
if (!comp.props || !comp.props.children || translation.indexOf(`${index}/>`) < 0 && translation.indexOf(`${index} />`) < 0) {
419+
return comp;
420+
}
421+
function Componentized() {
422+
return react.createElement(react.Fragment, null, comp);
423+
}
424+
return react.createElement(Componentized);
425+
};
426+
const generateArrayComponents = (components, translation) => components.map((c, index) => fixComponentProps(c, index, translation));
427+
const generateObjectComponents = (components, translation) => {
428+
const componentMap = {};
429+
Object.keys(components).forEach(c => {
430+
Object.assign(componentMap, {
431+
[c]: fixComponentProps(components[c], c, translation)
432+
});
433+
});
434+
return componentMap;
435+
};
436+
const generateComponents = (components, translation) => {
437+
if (!components) return null;
438+
if (Array.isArray(components)) {
439+
return generateArrayComponents(components, translation);
440+
}
441+
if (isObject(components)) {
442+
return generateObjectComponents(components, translation);
443+
}
444+
warnOnce('<Trans /> component prop expects an object or an array');
445+
return null;
446+
};
413447
function Trans$1(_ref) {
414448
let {
415449
children,
@@ -470,20 +504,8 @@
470504
ns: namespaces
471505
};
472506
const translation = key ? t(key, combinedTOpts) : defaultValue;
473-
if (components) {
474-
Object.keys(components).forEach(c => {
475-
const componentKey = components[c].key || c;
476-
const comp = react.cloneElement(components[c], {
477-
key: componentKey
478-
});
479-
if (!comp.props || !comp.props.children || translation.indexOf(`${c}/>`) < 0 && translation.indexOf(`${c} />`) < 0) return;
480-
function Componentized() {
481-
return react.createElement(react.Fragment, null, comp);
482-
}
483-
components[c] = react.createElement(Componentized);
484-
});
485-
}
486-
const content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
507+
const generatedComponents = generateComponents(components, translation);
508+
const content = renderNodes(generatedComponents || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
487509
const useAsParent = parent ?? reactI18nextOptions.defaultTransParent;
488510
return useAsParent ? react.createElement(useAsParent, additionalProps, content) : content;
489511
}

0 commit comments

Comments
 (0)