Skip to content

Commit

Permalink
optimize: consistent \_zero special handling and fix dev plural rules
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Jan 26, 2024
1 parent 8ac8be6 commit e51e540
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 23.7.20

- optimize: consistent \_zero special handling also for defaultValue_zero [2124](https://github.com/i18next/i18next/issues/2124)
- intl plural rule for dev is now en (consistent with v3 compatibility)

## 23.7.19

- fix: consistent \_zero special handling also for defaultValue_zero [2124](https://github.com/i18next/i18next/issues/2124)
Expand Down
8 changes: 6 additions & 2 deletions i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,11 @@
if (this.options.saveMissing) {
if (this.options.saveMissingPlurals && needsPluralHandling) {
lngs.forEach(language => {
this.pluralResolver.getSuffixes(language, options).forEach(suffix => {
const suffixes = this.pluralResolver.getSuffixes(language, options);
if (needsZeroSuffixLookup && options[`defaultValue${this.options.pluralSeparator}zero`] && suffixes.indexOf(`${this.options.pluralSeparator}zero`) < 0) {
suffixes.push(`${this.options.pluralSeparator}zero`);
}
suffixes.forEach(suffix => {
send([language], key + suffix, options[`defaultValue${suffix}`] || defaultValue);
});
});
Expand Down Expand Up @@ -1133,7 +1137,7 @@
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (this.shouldUseIntlApi()) {
try {
return new Intl.PluralRules(getCleanedCode(code), {
return new Intl.PluralRules(getCleanedCode(code === 'dev' ? 'en' : code), {
type: options.ordinal ? 'ordinal' : 'cardinal'
});
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/PluralResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class PluralResolver {
getRule(code, options = {}) {
if (this.shouldUseIntlApi()) {
try {
return new Intl.PluralRules(getCleanedCode(code), { type: options.ordinal ? 'ordinal' : 'cardinal' });
return new Intl.PluralRules(getCleanedCode(code === 'dev' ? 'en' : code), { type: options.ordinal ? 'ordinal' : 'cardinal' });
} catch (err) {
return;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,15 @@ class Translator extends EventEmitter {
if (this.options.saveMissing) {
if (this.options.saveMissingPlurals && needsPluralHandling) {
lngs.forEach((language) => {
this.pluralResolver.getSuffixes(language, options).forEach((suffix) => {
const suffixes = this.pluralResolver.getSuffixes(language, options);
if (
needsZeroSuffixLookup &&
options[`defaultValue${this.options.pluralSeparator}zero`] &&
suffixes.indexOf(`${this.options.pluralSeparator}zero`) < 0
) {
suffixes.push(`${this.options.pluralSeparator}zero`);
}
suffixes.forEach((suffix) => {
send([language], key + suffix, options[`defaultValue${suffix}`] || defaultValue);
});
});
Expand Down
3 changes: 2 additions & 1 deletion test/runtime/translator/translator.translate.missing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,12 @@ describe('Translator', () => {
});
t.changeLanguage('ar');

t.translate('translation:test.missing', {
const tr = t.translate('translation:test.missing', {
count: 0,
defaultValue_zero: 'default0',
defaultValue_one: 'default1',
});
expect(tr).toEqual('default0');
});

it('correctly sends missing resolved value', () => {
Expand Down

0 comments on commit e51e540

Please sign in to comment.