Skip to content

Commit 29c7794

Browse files
committed
bring back internal interpolationOverride handling for Trans component (if there are childrens), fixes #1754
1 parent 9fd452d commit 29c7794

8 files changed

+69
-9
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 14.1.2
2+
3+
- bring back internal interpolationOverride handling for Trans component (if there are childrens), fixes [1754](https://github.com/i18next/react-i18next/issues/1754)
4+
15
### 14.1.1
26

37
- do not modify passed tOptions context property to address [1745](https://github.com/i18next/react-i18next/issues/1745)

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"eslint-plugin-testing-library": "^5.11.0",
107107
"happy-dom": "^12.10.3",
108108
"husky": "^8.0.3",
109-
"i18next": "^23.8.2",
109+
"i18next": "^23.11.5",
110110
"lint-staged": "^8.1.3",
111111
"mkdirp": "^1.0.4",
112112
"prettier": "2.8.8",

react-i18next.js

+8
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,19 @@
479479
...i18n.options.interpolation.defaultVariables
480480
};
481481
}
482+
const interpolationOverride = values || count !== undefined || !children ? tOptions.interpolation : {
483+
interpolation: {
484+
...tOptions.interpolation,
485+
prefix: '#$?',
486+
suffix: '?$#'
487+
}
488+
};
482489
const combinedTOpts = {
483490
...tOptions,
484491
context: context || tOptions.context,
485492
count,
486493
...values,
494+
...interpolationOverride,
487495
defaultValue,
488496
ns: namespaces
489497
};

react-i18next.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/TransWithoutContext.js

+5
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,16 @@ export function Trans({
349349
? { ...values, ...i18n.options.interpolation.defaultVariables }
350350
: { ...i18n.options.interpolation.defaultVariables };
351351
}
352+
const interpolationOverride =
353+
values || count !== undefined || !children // if !children gets problems in future, undo that fix: https://github.com/i18next/react-i18next/issues/1729 by removing !children from this condition
354+
? tOptions.interpolation
355+
: { interpolation: { ...tOptions.interpolation, prefix: '#$?', suffix: '?$#' } };
352356
const combinedTOpts = {
353357
...tOptions,
354358
context: context || tOptions.context, // Add `context` from the props or fallback to the value from `tOptions`
355359
count,
356360
...values,
361+
...interpolationOverride,
357362
defaultValue,
358363
ns: namespaces,
359364
};

test/i18n.js

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ i18n.init({
4646
transTestEscapedHtml: 'Escaped html should unescape correctly <0>&lt;&nbsp;&amp;&gt;</0>.',
4747
transTestCustomUnescape: 'Text should be passed through custom unescape <0>&shy;</0>',
4848
transTestCustomUnescapeSecond: 'Vertrauens&shy;kennwert',
49+
'trans-key-with-generic-var': 'Value as is: {{foo}}',
50+
'trans-key-with-number-var': 'Treat value as number: {{foo, number}}',
4951
testTransWithCtx: 'Go <1>there</1>.',
5052
testTransWithCtx_home: 'Go <1>home</1>.',
5153
testTransNoChildrenWithCtx: 'Go {{context}}.',

test/trans.render.spec.jsx

+41
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,47 @@ describe('trans with context property', () => {
862862
});
863863
});
864864

865+
describe.only('trans with formatting', () => {
866+
function TestComponent({ parent }) {
867+
return (
868+
<>
869+
<Trans
870+
parent={parent}
871+
i18nKey="trans-key-with-generic-var"
872+
values={{ foo: 1234 }}
873+
/>
874+
<Trans
875+
parent={parent}
876+
i18nKey="trans-key-with-number-var"
877+
values={{ foo: 1234 }}
878+
/>
879+
<Trans parent={parent} i18nKey="trans-key-with-number-var">
880+
{{ foo: 1234 }}
881+
</Trans>
882+
</>
883+
);
884+
}
885+
886+
it('should render correct content', () => {
887+
const { container } = render(<TestComponent />);
888+
expect(container.childNodes[0]).toMatchInlineSnapshot(`
889+
<div>
890+
Value as is: 1234
891+
</div>
892+
`);
893+
expect(container.childNodes[1]).toMatchInlineSnapshot(`
894+
<div>
895+
Treat value as number: 1234
896+
</div>
897+
`);
898+
expect(container.childNodes[2]).toMatchInlineSnapshot(`
899+
<div>
900+
Treat value as number: 1234
901+
</div>
902+
`);
903+
});
904+
});
905+
865906
describe('trans with undefined context property', () => {
866907
function TestComponent({ parent }) {
867908
return (

0 commit comments

Comments
 (0)