Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): make sure the plural category exists #13169

Merged
merged 1 commit into from Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions modules/@angular/common/src/localization.ts
Expand Up @@ -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}"`);
}

/**
Expand Down
22 changes: 18 additions & 4 deletions modules/@angular/common/test/localization_spec.ts
Expand Up @@ -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', () => {

Expand Down Expand Up @@ -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"');
});
});
});
});
}