Skip to content

Commit

Permalink
allow defaultValue in nested translation #2174
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Apr 14, 2024
1 parent 677755c commit ba49a6f
Show file tree
Hide file tree
Showing 5 changed files with 23 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 @@
## 23.11.2

- allow defaultValue in nested translation [2174](https://github.com/i18next/i18next/issues/2174)

## 23.11.1

- types: remove const from ParseKeys Context type parameter and disable `skipLibCheck` [2169](https://github.com/i18next/i18next/pull/2169)
Expand Down
2 changes: 1 addition & 1 deletion i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@
this.logger.warn(`failed parsing options string in nesting for key ${key}`, e);
return `${key}${sep}${optionsString}`;
}
delete clonedOptions.defaultValue;
if (clonedOptions.defaultValue && clonedOptions.defaultValue.indexOf(this.prefix) > -1) delete clonedOptions.defaultValue;
return key;
}
while (match = this.nestingRegexp.exec(str)) {
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/Interpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ class Interpolator {
}

// assert we do not get a endless loop on interpolating defaultValue again and again
delete clonedOptions.defaultValue;
if (clonedOptions.defaultValue && clonedOptions.defaultValue.indexOf(this.prefix) > -1)
delete clonedOptions.defaultValue;
return key;
}

Expand Down
15 changes: 15 additions & 0 deletions test/runtime/translator/translator.translate.combination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ describe('Translator', () => {
friend_female_zero: 'no girlfriend',
friend_female_one: 'a girlfriend',
friend_female_other: '{{count}} girlfriends',

message:
'Something went wrong: $t(error_code.{{error_type}}, {"defaultValue": "Unknown error"})',
error_code: {
it_error: 'it went wrong',
},
},
},
});
Expand Down Expand Up @@ -114,6 +120,15 @@ describe('Translator', () => {
args: ['noKeyFoundTestingDefault_1', { defaultValue: '{{val}} bar', val: '$t(foo)' }],
expected: 'foo bar',
},

{
args: ['message', { error_type: 'it_error' }],
expected: 'Something went wrong: it went wrong',
},
{
args: ['message', { error_type: 'other_error' }],
expected: 'Something went wrong: Unknown error',
},
];

tests.forEach((test) => {
Expand Down

0 comments on commit ba49a6f

Please sign in to comment.