diff --git a/modules/@angular/common/src/localization.ts b/modules/@angular/common/src/localization.ts index a6c64c6fa2..8eaa560165 100644 --- a/modules/@angular/common/src/localization.ts +++ b/modules/@angular/common/src/localization.ts @@ -23,9 +23,23 @@ export abstract class NgLocalization { abstract getPluralCategory(value: any): s */ export function getPluralCategory( value: number, cases: string[], ngLocalization: NgLocalization): string { - const nbCase = `=${value}`; + let key = `=${value}`; - return cases.indexOf(nbCase) > -1 ? nbCase : ngLocalization.getPluralCategory(value); + if (cases.indexOf(key) > -1) { + return key; + } + + key = ngLocalization.getPluralCategory(value); + + if (cases.indexOf(key) > -1) { + return key; + } + + if (cases.indexOf('other') > -1) { + return 'other'; + } + + throw new Error(`No plural message found for value "${value}"`); } /** diff --git a/modules/@angular/common/test/localization_spec.ts b/modules/@angular/common/test/localization_spec.ts index 28d82716fd..20f6fa51cc 100644 --- a/modules/@angular/common/test/localization_spec.ts +++ b/modules/@angular/common/test/localization_spec.ts @@ -7,13 +7,10 @@ */ import {LOCALE_ID} from '@angular/core'; -import {TestBed} from '@angular/core/testing'; -import {beforeEach, describe, inject, it} from '@angular/core/testing/testing_internal'; -import {expect} from '@angular/platform-browser/testing/matchers'; +import {TestBed, inject} from '@angular/core/testing'; import {NgLocaleLocalization, NgLocalization, getPluralCategory} from '../src/localization'; - export function main() { describe('l10n', () => { @@ -141,6 +138,23 @@ export function main() { expect(getPluralCategory(5, ['one', 'other', '=5'], l10n)).toEqual('=5'); expect(getPluralCategory(6, ['one', 'other', '=5'], l10n)).toEqual('other'); }); + + it('should fallback to other when the case is not present', () => { + const l10n = new NgLocaleLocalization('ro'); + expect(getPluralCategory(1, ['one', 'other'], l10n)).toEqual('one'); + // 2 -> 'few' + expect(getPluralCategory(2, ['one', 'other'], l10n)).toEqual('other'); + }); + + describe('errors', () => { + it('should report an error when the "other" category is not present', () => { + expect(() => { + const l10n = new NgLocaleLocalization('ro'); + // 2 -> 'few' + getPluralCategory(2, ['one'], l10n); + }).toThrowError('No plural message found for value "2"'); + }); + }); }); }); } \ No newline at end of file