diff --git a/README.md b/README.md index b58a0ba..d3e5177 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,11 @@ yarn add factorial-i18n const translations = { en: { common: { hello: 'hello %{name}', }, - beers: { one: '%{count} beer', other: '%{count} beers' } + beers: { zero: 'No beers :(', one: '%{count} beer', other: '%{count} beers' } }, es: { common: { hello: 'hola %{name}', }, - beers: { one: '%{count} cerveza', other: '%{count} cervezas' } + beers: { zero: 'Sin cervezas!', one: '%{count} cerveza', other: '%{count} cervezas' } } } @@ -32,8 +32,9 @@ const i18n = new I18n() i18n.setTranslations(translations) i18n.setLocale('es') i18n.t('common.hello') // => Hola -i18n.tp('common.beers', { count: 0 }) // => 0 cervezas +i18n.tp('common.beers', { count: 0 }) // => Sin cervezas! i18n.tp('common.beers', { count: 1 }) // => 1 cerveza +i18n.tp('common.beers', { count: 2 }) // => 2 cervezas ``` ## Where is it used? diff --git a/__tests__/index.spec.ts b/__tests__/index.spec.ts index 67fc803..cbbc0f3 100644 --- a/__tests__/index.spec.ts +++ b/__tests__/index.spec.ts @@ -23,6 +23,7 @@ const mockedTranslations = { es: { hello: 'hola %{name}', beers: { + zero: 'sin cervezas!', one: '%{count} cerveza', other: '%{count} cervezas' }, @@ -152,7 +153,7 @@ describe('i18n', () => { }) it('uses pluralizations correctly otherwise', () => { - expect(i18n.tp('beers', { count: 0 })).toBe('0 cervezas') + expect(i18n.tp('beers', { count: 0 })).toBe('sin cervezas!') expect(i18n.tp('beers', { count: 1 })).toBe('1 cerveza') expect(i18n.tp('beers', { count: 2 })).toBe('2 cervezas') }) diff --git a/package.json b/package.json index 626b3d9..2420e2e 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "jest": { "preset": "ts-jest" }, "dependencies": { "lodash": "^4.17.15", - "plurals-cldr": "1.0.3" + "plurals-cldr": "2.0.1" }, "devDependencies": { "@types/jest": "24.0.13", diff --git a/src/index.ts b/src/index.ts index e988312..9559356 100644 --- a/src/index.ts +++ b/src/index.ts @@ -82,7 +82,10 @@ export default class I18n { } const form = plural(this.locale, num) - const pluralPath = `${path}.${form}` + let pluralPath = `${path}.${form}` + if (num === 0 && this.getKey(`${path}.zero`) !== undefined) { + pluralPath = `${path}.zero` + } return this.t(pluralPath, opts) }