Skip to content

Commit b8a0d8c

Browse files
committed
do not modify passed tOptions context property to address #1745
1 parent f8d0111 commit b8a0d8c

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 14.1.1
2+
3+
- do not modify passed tOptions context property to address [1745](https://github.com/i18next/react-i18next/issues/1745)
4+
15
### 14.1.0
26

37
- types(`Trans`): add typechecking on context prop [1732](https://github.com/i18next/react-i18next/pull/1732) (might break if using "internal" `Trans` or `TransProps`)

react-i18next.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,6 @@
459459
return children;
460460
}
461461
const t = tFromProps || i18n.t.bind(i18n) || (k => k);
462-
tOptions.context = context;
463462
const reactI18nextOptions = {
464463
...getDefaults(),
465464
...(i18n.options && i18n.options.react)
@@ -482,6 +481,7 @@
482481
}
483482
const combinedTOpts = {
484483
...tOptions,
484+
context: context || tOptions.context,
485485
count,
486486
...values,
487487
defaultValue,

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,6 @@ export function Trans({
329329

330330
const t = tFromProps || i18n.t.bind(i18n) || ((k) => k);
331331

332-
tOptions.context = context;
333-
334332
const reactI18nextOptions = { ...getDefaults(), ...(i18n.options && i18n.options.react) };
335333

336334
// prepare having a namespace
@@ -353,6 +351,7 @@ export function Trans({
353351
}
354352
const combinedTOpts = {
355353
...tOptions,
354+
context: context || tOptions.context, // Add `context` from the props or fallback to the value from `tOptions`
356355
count,
357356
...values,
358357
defaultValue,

test/trans.render.spec.jsx

+27
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,33 @@ describe('trans with custom unescape', () => {
810810
});
811811
});
812812

813+
describe('trans with context via tOptions', () => {
814+
const tOptions = { context: 'home' };
815+
function TestComponent({ parent }) {
816+
return (
817+
<Trans i18nKey="testTransWithCtx" tOptions={tOptions} parent={parent}>
818+
Open <Link to="/msgs">here</Link>.
819+
</Trans>
820+
);
821+
}
822+
823+
it('should render correct content', () => {
824+
const { container } = render(<TestComponent />);
825+
expect(container.firstChild).toMatchInlineSnapshot(`
826+
<div>
827+
Go
828+
<a
829+
href="/msgs"
830+
>
831+
home
832+
</a>
833+
.
834+
</div>
835+
`);
836+
expect(tOptions).to.have.property('context', 'home');
837+
});
838+
});
839+
813840
describe('trans with context property', () => {
814841
function TestComponent({ parent }) {
815842
return (

0 commit comments

Comments
 (0)