Skip to content

Commit

Permalink
Respect defaultVariables in the interpolation options #1685
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Oct 13, 2023
1 parent 1ff5cab commit 110eeae
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 13.3.0

- Respect defaultVariables in the interpolation options [1685](https://github.com/i18next/react-i18next/issues/1685)

### 13.2.2

- Fix missing TransWithoutContext type [1672](https://github.com/i18next/react-i18next/pull/1672)
Expand Down
11 changes: 10 additions & 1 deletion react-i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,22 @@
hashTransKey
} = reactI18nextOptions;
const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue);
const interpolationOverride = values ? tOptions.interpolation : {
let interpolationOverride = values ? tOptions.interpolation : {
interpolation: {
...tOptions.interpolation,
prefix: '#$?',
suffix: '?$#'
}
};
if (i18n.options && i18n.options.interpolation && i18n.options.interpolation.defaultVariables) {
if (!interpolationOverride) interpolationOverride = {};
interpolationOverride.interpolation = {
defaultVariables: {
...i18n.options.interpolation.defaultVariables,
...(interpolationOverride.interpolation && interpolationOverride.interpolation.defaultVariables || {})
}
};
}
const combinedTOpts = {
...tOptions,
count,
Expand Down
2 changes: 1 addition & 1 deletion react-i18next.min.js

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion src/TransWithoutContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,20 @@ export function Trans({
const key =
i18nKey ||
(hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue);
const interpolationOverride = values
let interpolationOverride = values
? tOptions.interpolation
: { interpolation: { ...tOptions.interpolation, prefix: '#$?', suffix: '?$#' } };
if (i18n.options && i18n.options.interpolation && i18n.options.interpolation.defaultVariables) {
if (!interpolationOverride) interpolationOverride = {};
interpolationOverride.interpolation = {
defaultVariables: {

This comment has been minimized.

Copy link
@hubgit

hubgit Oct 19, 2023

@adrai I think there needs to be a ...interpolationOverride.interpolation here (and in react-i18next.js), otherwise the rest of the interpolation settings are being lost?

This comment has been minimized.

Copy link
@adrai

adrai Oct 19, 2023

Author Member

@hubgit optimized with 16c8d07 in v13.3.1

...i18n.options.interpolation.defaultVariables,
...((interpolationOverride.interpolation &&
interpolationOverride.interpolation.defaultVariables) ||
{}),
},
};
}
const combinedTOpts = {
...tOptions,
count,
Expand Down
5 changes: 5 additions & 0 deletions test/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ i18n.init({
en: {
translation: {
key1: 'test',
interpolateKeyWithDefaultVariables: 'add {{defaultInsert}} {{defaultUp, uppercase}}',
interpolateKey: 'add {{insert}} {{up, uppercase}}',
interpolateKey2: '<strong>add</strong> {{insert}} {{up, uppercase}}',
transTest1: 'Go <1>there</1>.',
Expand Down Expand Up @@ -66,6 +67,10 @@ i18n.init({
if (format === 'uppercase') return value.toUpperCase();
return value;
},
defaultVariables: {
defaultInsert: 'first',
defaultUp: 'second',
},
},

react: {
Expand Down
14 changes: 14 additions & 0 deletions test/trans.render.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,20 @@ describe('trans should work with misleading overloaded empty elements in compone
});
});

describe('trans should work with defaultVariables', () => {
function TestComponent() {
return <Trans i18nKey="interpolateKeyWithDefaultVariables" />;
}
it('should render translated string', () => {
const { container } = render(<TestComponent />);
expect(container.firstChild).toMatchInlineSnapshot(`
<div>
add first SECOND
</div>
`);
});
});

describe('trans should work with lowercase elements in components', () => {
function TestComponent() {
return (
Expand Down

0 comments on commit 110eeae

Please sign in to comment.