Skip to content

Commit f22d478

Browse files
committed
try to address #1876 and use also defaultValue via tOptions as fallback
1 parent 619fa59 commit f22d478

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
### 16.1.5
2+
3+
- fix: Incosistent behaviour of Trans and t. Trans set defaultValue when t call doesn't set the field. [1876](https://github.com/i18next/react-i18next/issues/1876)
4+
- Trans: use also defaultValue via tOptions as fallback
5+
16
### 16.1.4
27

38
- fix: detect pre-transformation use of interpolation like number/date/etc. [1875](https://github.com/i18next/react-i18next/pull/1875)

react-i18next.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,7 +2690,7 @@
26902690
let namespaces = ns || t.ns || i18n.options?.defaultNS;
26912691
namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
26922692
const nodeAsString = nodesToString(children, reactI18nextOptions, i18n, i18nKey);
2693-
const defaultValue = defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || (typeof i18nKey === 'function' ? keysFromSelector(i18nKey) : i18nKey);
2693+
const defaultValue = defaults || tOptions?.defaultValue || nodeAsString || reactI18nextOptions.transEmptyNodeValue || (typeof i18nKey === 'function' ? keysFromSelector(i18nKey) : i18nKey);
26942694
const {
26952695
hashTransKey
26962696
} = reactI18nextOptions;
@@ -2716,7 +2716,7 @@
27162716
count,
27172717
...values,
27182718
...interpolationOverride,
2719-
defaultValue,
2719+
defaultValue: defaults || tOptions?.defaultValue,
27202720
ns: namespaces
27212721
};
27222722
const translation = key ? t(key, combinedTOpts) : defaultValue;

react-i18next.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/TransWithoutContext.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ export function Trans({
426426
const nodeAsString = nodesToString(children, reactI18nextOptions, i18n, i18nKey);
427427
const defaultValue =
428428
defaults ||
429+
tOptions?.defaultValue ||
429430
nodeAsString ||
430431
reactI18nextOptions.transEmptyNodeValue ||
431432
(typeof i18nKey === 'function' ? keyFromSelector(i18nKey) : i18nKey);
@@ -452,7 +453,7 @@ export function Trans({
452453
count,
453454
...values,
454455
...interpolationOverride,
455-
defaultValue,
456+
defaultValue: defaults || tOptions?.defaultValue,
456457
ns: namespaces,
457458
};
458459
const translation = key ? t(key, combinedTOpts) : defaultValue;

test/trans.render.spec.jsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,42 @@ describe('trans with defaults property and children', () => {
10821082
});
10831083
});
10841084

1085+
describe('trans with defaults property and no children', () => {
1086+
function TestComponent() {
1087+
return <Trans i18nKey="missing-key-here" count={2} defaults="There are {{ count }} items" />;
1088+
}
1089+
1090+
it('should render correct content', () => {
1091+
const { container } = render(<TestComponent />);
1092+
expect(container.firstChild).toMatchInlineSnapshot(`
1093+
<div>
1094+
There are 2 items
1095+
</div>
1096+
`);
1097+
});
1098+
});
1099+
1100+
describe('trans with defaults (via tOptions) property and no children', () => {
1101+
function TestComponent() {
1102+
return (
1103+
<Trans
1104+
i18nKey="missing-key-here"
1105+
count={4}
1106+
tOptions={{ defaultValue: 'There are {{ count }} items' }}
1107+
/>
1108+
);
1109+
}
1110+
1111+
it('should render correct content', () => {
1112+
const { container } = render(<TestComponent />);
1113+
expect(container.firstChild).toMatchInlineSnapshot(`
1114+
<div>
1115+
There are 4 items
1116+
</div>
1117+
`);
1118+
});
1119+
});
1120+
10851121
describe('trans with nesting $t()', () => {
10861122
function TestComponent({ parent }) {
10871123
return <Trans i18nKey="nestingKey1" ns="other" parent={parent} />;

0 commit comments

Comments
 (0)