Skip to content

Commit

Permalink
fix: consistent _zero special handling also for defaultValue_zero #2124
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Jan 24, 2024
1 parent ffd59fd commit c55917e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 23.7.19

- fix: consistent \_zero special handling also for defaultValue_zero [2124](https://github.com/i18next/i18next/issues/2124)

## 23.7.18

- types: support readonly defaultNS [2123](https://github.com/i18next/i18next/pull/2123)
Expand Down
3 changes: 2 additions & 1 deletion i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,8 @@
const defaultValueSuffixOrdinalFallback = options.ordinal && needsPluralHandling ? this.pluralResolver.getSuffix(lng, options.count, {
ordinal: false
}) : '';
const defaultValue = options[`defaultValue${defaultValueSuffix}`] || options[`defaultValue${defaultValueSuffixOrdinalFallback}`] || options.defaultValue;
const needsZeroSuffixLookup = needsPluralHandling && !options.ordinal && options.count === 0 && this.pluralResolver.shouldUseIntlApi();
const defaultValue = needsZeroSuffixLookup && options[`defaultValue${this.options.pluralSeparator}zero`] || options[`defaultValue${defaultValueSuffix}`] || options[`defaultValue${defaultValueSuffixOrdinalFallback}`] || options.defaultValue;
if (!this.isValidLookup(res) && hasDefaultValue) {
usedDefault = true;
res = defaultValue;
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,13 @@ class Translator extends EventEmitter {
options.ordinal && needsPluralHandling
? this.pluralResolver.getSuffix(lng, options.count, { ordinal: false })
: '';
const needsZeroSuffixLookup =
needsPluralHandling &&
!options.ordinal &&
options.count === 0 &&
this.pluralResolver.shouldUseIntlApi();
const defaultValue =
(needsZeroSuffixLookup && options[`defaultValue${this.options.pluralSeparator}zero`]) ||
options[`defaultValue${defaultValueSuffix}`] ||
options[`defaultValue${defaultValueSuffixOrdinalFallback}`] ||
options.defaultValue;
Expand Down
13 changes: 13 additions & 0 deletions test/runtime/translator/translator.translate.defaultValue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('Translator', () => {
interpolateDefaultValue: true,
interpolateKey: true,
},
pluralSeparator: '_',
},
);
t.changeLanguage('en');
Expand All @@ -47,6 +48,18 @@ describe('Translator', () => {
],
expected: 'test_en_plural',
},
{
args: [
'translation:testMe',
{
defaultValue_other: 'test_en_plural',
defaultValue_one: 'test_en',
defaultValue_zero: 'test_en_zero',
count: 0,
},
],
expected: 'test_en_zero',
},
];

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

0 comments on commit c55917e

Please sign in to comment.