Skip to content

Commit

Permalink
fix: optional other plural form
Browse files Browse the repository at this point in the history
  • Loading branch information
dgaponov committed May 20, 2024
1 parent 2cd60e6 commit e6ee304
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 21 deletions.
16 changes: 1 addition & 15 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,20 +345,6 @@ describe('i18n', () => {
})).toBe('11 пользователей');
});

it('should throw exception when missing required plural form', () => {
i18n.setLang('ru');
i18n.registerKeyset('ru', 'app', {
// @ts-ignore
users: {'many': '{{count}} пользователей'}
});

expect(() => {
i18n.i18n('app', 'users', {
count: 11,
})
}).toThrow(new Error(`Missing required plural form 'other' for key 'users'`));
});

it('should use `other` form when no other forms are specified', () => {
i18n.setLang('ru');
i18n.registerKeyset('ru', 'app', {
Expand Down Expand Up @@ -583,7 +569,7 @@ describe('registerKeyset', () => {
lang: 'en',
data: {en: {notification: {hey: 'Hello!'}}},
});

expect(() => {
i18n.registerKeyset('en', 'notification', {
title: 'Hello!'
Expand Down
8 changes: 2 additions & 6 deletions src/plural/general.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import type { DeprecatedPluralValue, PluralValue, Pluralizer } from "../types";
import {PluralForm} from '../types';

export function getPluralViaIntl(key: string, value: PluralValue, count: number, lang: string) {
if (typeof value.other === 'undefined') {
throw new Error(`Missing required plural form 'other' for key '${key}'`);
}

export function getPluralViaIntl(value: PluralValue, count: number, lang: string) {
if (value.zero && count === 0) {
return value.zero;
}
Expand Down Expand Up @@ -37,7 +33,7 @@ type FormatPluralArgs = {

export function getPluralValue({value, count, lang, pluralizers, log, key}: FormatPluralArgs) {
if (!Array.isArray(value)) {
return getPluralViaIntl(key, value, count, lang) || key;
return getPluralViaIntl(value, count, lang) || key;
}

if (!pluralizers) {
Expand Down

0 comments on commit e6ee304

Please sign in to comment.