Skip to content

Commit 9684fab

Browse files
committed
align context handling of Trans component with t function, fixes #1729
1 parent ea0c6e5 commit 9684fab

6 files changed

+85
-3
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 14.0.6
2+
3+
- align context handling of Trans component with t function, fixes [1729](https://github.com/i18next/react-i18next/issues/1729)
4+
15
### 14.0.5
26

37
- Fix [1691](https://github.com/i18next/react-i18next/issues/1691) for strict mode, by preserving change language binding [1720](https://github.com/i18next/react-i18next/pull/1720)

react-i18next.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@
459459
return children;
460460
}
461461
const t = tFromProps || i18n.t.bind(i18n) || (k => k);
462-
if (context) tOptions.context = context;
462+
tOptions.context = context;
463463
const reactI18nextOptions = {
464464
...getDefaults(),
465465
...(i18n.options && i18n.options.react)

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-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export function Trans({
329329

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

332-
if (context) tOptions.context = context;
332+
tOptions.context = context;
333333

334334
const reactI18nextOptions = { ...getDefaults(), ...(i18n.options && i18n.options.react) };
335335

test/i18n.js

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ i18n.init({
4848
transTestCustomUnescapeSecond: 'Vertrauens­kennwert',
4949
testTransWithCtx: 'Go <1>there</1>.',
5050
testTransWithCtx_home: 'Go <1>home</1>.',
51+
testTransNoChildrenWithCtx: 'Go {{context}}.',
52+
testTransNoChildrenWithCtx_home: 'Go to Switzerland.',
5153
'You have {{count}} message_one': 'You have {{count}} message',
5254
'You have {{count}} message_other': 'You have {{count}} messages',
5355
deepPath: {

test/trans.render.spec.jsx

+76
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,82 @@ describe('trans with context property', () => {
835835
});
836836
});
837837

838+
describe('trans with undefined context property', () => {
839+
function TestComponent({ parent }) {
840+
return (
841+
<Trans i18nKey="testTransWithCtx" context={undefined} parent={parent}>
842+
Open <Link to="/msgs">here</Link>.
843+
</Trans>
844+
);
845+
}
846+
847+
it('should render correct content', () => {
848+
const { container } = render(<TestComponent />);
849+
expect(container.firstChild).toMatchInlineSnapshot(`
850+
<div>
851+
Go
852+
<a
853+
href="/msgs"
854+
>
855+
there
856+
</a>
857+
.
858+
</div>
859+
`);
860+
});
861+
});
862+
863+
describe('trans with context property but no children', () => {
864+
function TestComponent({ parent }) {
865+
return (
866+
<Trans i18nKey="testTransNoChildrenWithCtx" context="home" parent={parent} />
867+
);
868+
}
869+
870+
it('should render correct content', () => {
871+
const { container } = render(<TestComponent />);
872+
expect(container.firstChild).toMatchInlineSnapshot(`
873+
<div>
874+
Go to Switzerland.
875+
</div>
876+
`);
877+
});
878+
});
879+
880+
describe('trans with undefined context property but no children', () => {
881+
function TestComponent({ parent }) {
882+
return (
883+
<Trans i18nKey="testTransNoChildrenWithCtx" context={undefined} parent={parent} />
884+
);
885+
}
886+
887+
it('should render correct content', () => {
888+
const { container } = render(<TestComponent />);
889+
expect(container.firstChild).toMatchInlineSnapshot(`
890+
<div>
891+
Go .
892+
</div>
893+
`);
894+
});
895+
});
896+
897+
describe('trans with different context property but no children', () => {
898+
function TestComponent({ parent }) {
899+
return (
900+
<Trans i18nKey="testTransNoChildrenWithCtx" context="somewhere" parent={parent} />
901+
);
902+
}
903+
904+
it('should render correct content', () => {
905+
const { container } = render(<TestComponent />);
906+
expect(container.firstChild).toMatchInlineSnapshot(`
907+
<div>
908+
Go somewhere.
909+
</div>
910+
`);
911+
});
912+
});
913+
838914
describe('trans with defaults property and children', () => {
839915
function TestComponent() {
840916
return (

0 commit comments

Comments
 (0)