Skip to content

Commit

Permalink
do not modify passed tOptions context property to address #1745
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Apr 23, 2024
1 parent f8d0111 commit b8a0d8c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
### 14.1.1

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

### 14.1.0

- types(`Trans`): add typechecking on context prop [1732](https://github.com/i18next/react-i18next/pull/1732) (might break if using "internal" `Trans` or `TransProps`)
Expand Down
2 changes: 1 addition & 1 deletion react-i18next.js
Expand Up @@ -459,7 +459,6 @@
return children;
}
const t = tFromProps || i18n.t.bind(i18n) || (k => k);
tOptions.context = context;
const reactI18nextOptions = {
...getDefaults(),
...(i18n.options && i18n.options.react)
Expand All @@ -482,6 +481,7 @@
}
const combinedTOpts = {
...tOptions,
context: context || tOptions.context,
count,
...values,
defaultValue,
Expand Down
2 changes: 1 addition & 1 deletion react-i18next.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/TransWithoutContext.js
Expand Up @@ -329,8 +329,6 @@ export function Trans({

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

tOptions.context = context;

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

// prepare having a namespace
Expand All @@ -353,6 +351,7 @@ export function Trans({
}
const combinedTOpts = {
...tOptions,
context: context || tOptions.context, // Add `context` from the props or fallback to the value from `tOptions`
count,
...values,
defaultValue,
Expand Down
27 changes: 27 additions & 0 deletions test/trans.render.spec.jsx
Expand Up @@ -810,6 +810,33 @@ describe('trans with custom unescape', () => {
});
});

describe('trans with context via tOptions', () => {
const tOptions = { context: 'home' };
function TestComponent({ parent }) {
return (
<Trans i18nKey="testTransWithCtx" tOptions={tOptions} parent={parent}>
Open <Link to="/msgs">here</Link>.
</Trans>
);
}

it('should render correct content', () => {
const { container } = render(<TestComponent />);
expect(container.firstChild).toMatchInlineSnapshot(`
<div>
Go
<a
href="/msgs"
>
home
</a>
.
</div>
`);
expect(tOptions).to.have.property('context', 'home');
});
});

describe('trans with context property', () => {
function TestComponent({ parent }) {
return (
Expand Down

0 comments on commit b8a0d8c

Please sign in to comment.