fix(common): use v4 plurals when importing DeprecatedI18NPipesModule
(#18955)
This commit is contained in:

committed by
Victor Berchet

parent
043f104738
commit
30d53a8942
21
packages/common/test/common_module_spec.ts
Normal file
21
packages/common/test/common_module_spec.ts
Normal file
@ -0,0 +1,21 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
|
||||
import {TestBed, inject} from '@angular/core/testing';
|
||||
import {DeprecatedI18NPipesModule} from '../src/common_module';
|
||||
import {USE_V4_PLURALS} from '../src/i18n/localization';
|
||||
|
||||
export function main() {
|
||||
describe('DeprecatedI18NPipesModule', () => {
|
||||
beforeEach(() => { TestBed.configureTestingModule({imports: [DeprecatedI18NPipesModule]}); });
|
||||
|
||||
it('should define the token USE_V4_PLURALS to true',
|
||||
inject([USE_V4_PLURALS], (useV4Plurals: true) => { expect(useV4Plurals).toEqual(true); }));
|
||||
});
|
||||
}
|
@ -12,7 +12,7 @@ import localeZgh from '../../locales/zgh';
|
||||
import localeFr from '../../locales/fr';
|
||||
import {LOCALE_ID} from '@angular/core';
|
||||
import {TestBed, inject} from '@angular/core/testing';
|
||||
import {NgLocaleLocalization, NgLocalization, getPluralCategory} from '../../src/i18n/localization';
|
||||
import {NgLocaleLocalization, NgLocalization, getPluralCategory, USE_V4_PLURALS} from '../../src/i18n/localization';
|
||||
import {registerLocaleData} from '../../src/i18n/locale_data';
|
||||
|
||||
export function main() {
|
||||
@ -25,13 +25,7 @@ export function main() {
|
||||
});
|
||||
|
||||
describe('NgLocalization', () => {
|
||||
describe('ro', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [{provide: LOCALE_ID, useValue: 'ro'}],
|
||||
});
|
||||
});
|
||||
|
||||
function roTests() {
|
||||
it('should return plural cases for the provided locale',
|
||||
inject([NgLocalization], (l10n: NgLocalization) => {
|
||||
expect(l10n.getPluralCategory(0)).toEqual('few');
|
||||
@ -39,15 +33,30 @@ export function main() {
|
||||
expect(l10n.getPluralCategory(1212)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(1223)).toEqual('other');
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
describe('sr', () => {
|
||||
describe('ro', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [{provide: LOCALE_ID, useValue: 'sr'}],
|
||||
providers: [{provide: LOCALE_ID, useValue: 'ro'}],
|
||||
});
|
||||
});
|
||||
|
||||
roTests();
|
||||
});
|
||||
|
||||
describe('ro with v4 plurals', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers:
|
||||
[{provide: LOCALE_ID, useValue: 'ro'}, {provide: USE_V4_PLURALS, useValue: true}],
|
||||
});
|
||||
});
|
||||
|
||||
roTests();
|
||||
});
|
||||
|
||||
function srTests() {
|
||||
it('should return plural cases for the provided locale',
|
||||
inject([NgLocalization], (l10n: NgLocalization) => {
|
||||
expect(l10n.getPluralCategory(1)).toEqual('one');
|
||||
@ -59,91 +68,117 @@ export function main() {
|
||||
expect(l10n.getPluralCategory(2.11)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(2.12)).toEqual('other');
|
||||
}));
|
||||
}
|
||||
|
||||
describe('sr', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [{provide: LOCALE_ID, useValue: 'sr'}],
|
||||
});
|
||||
});
|
||||
|
||||
srTests();
|
||||
});
|
||||
|
||||
describe('sr with v4 plurals', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers:
|
||||
[{provide: LOCALE_ID, useValue: 'sr'}, {provide: USE_V4_PLURALS, useValue: true}],
|
||||
});
|
||||
});
|
||||
|
||||
srTests();
|
||||
});
|
||||
});
|
||||
|
||||
describe('NgLocaleLocalization', () => {
|
||||
it('should return the correct values for the "en" locale', () => {
|
||||
const l10n = new NgLocaleLocalization('en-US');
|
||||
function ngLocaleLocalizationTests(useV4Plurals: boolean) {
|
||||
it('should return the correct values for the "en" locale', () => {
|
||||
const l10n = new NgLocaleLocalization('en-US', useV4Plurals);
|
||||
|
||||
expect(l10n.getPluralCategory(0)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(2)).toEqual('other');
|
||||
});
|
||||
expect(l10n.getPluralCategory(0)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(2)).toEqual('other');
|
||||
});
|
||||
|
||||
it('should return the correct values for the "ro" locale', () => {
|
||||
const l10n = new NgLocaleLocalization('ro');
|
||||
it('should return the correct values for the "ro" locale', () => {
|
||||
const l10n = new NgLocaleLocalization('ro', useV4Plurals);
|
||||
|
||||
expect(l10n.getPluralCategory(0)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(2)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(12)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(23)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(1212)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(1223)).toEqual('other');
|
||||
});
|
||||
expect(l10n.getPluralCategory(0)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(2)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(12)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(23)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(1212)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(1223)).toEqual('other');
|
||||
});
|
||||
|
||||
it('should return the correct values for the "sr" locale', () => {
|
||||
const l10n = new NgLocaleLocalization('sr');
|
||||
it('should return the correct values for the "sr" locale', () => {
|
||||
const l10n = new NgLocaleLocalization('sr', useV4Plurals);
|
||||
|
||||
expect(l10n.getPluralCategory(1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(31)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(0.1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(1.1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(2.1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(31)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(0.1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(1.1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(2.1)).toEqual('one');
|
||||
|
||||
expect(l10n.getPluralCategory(3)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(33)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(0.2)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(0.3)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(0.4)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(2.2)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(3)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(33)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(0.2)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(0.3)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(0.4)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(2.2)).toEqual('few');
|
||||
|
||||
expect(l10n.getPluralCategory(2.11)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(2.12)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(2.13)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(2.14)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(2.15)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(2.11)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(2.12)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(2.13)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(2.14)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(2.15)).toEqual('other');
|
||||
|
||||
expect(l10n.getPluralCategory(0)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(5)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(10)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(35)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(37)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(40)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(0.0)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(0.5)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(0.6)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(0)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(5)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(10)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(35)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(37)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(40)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(0.0)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(0.5)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(0.6)).toEqual('other');
|
||||
|
||||
expect(l10n.getPluralCategory(2)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(2.1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(2.2)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(2.3)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(2.4)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(2.5)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(2)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(2.1)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(2.2)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(2.3)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(2.4)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(2.5)).toEqual('other');
|
||||
|
||||
expect(l10n.getPluralCategory(20)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(21)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(22)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(23)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(24)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(25)).toEqual('other');
|
||||
});
|
||||
expect(l10n.getPluralCategory(20)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(21)).toEqual('one');
|
||||
expect(l10n.getPluralCategory(22)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(23)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(24)).toEqual('few');
|
||||
expect(l10n.getPluralCategory(25)).toEqual('other');
|
||||
});
|
||||
|
||||
it('should return the default value for a locale with no rule', () => {
|
||||
const l10n = new NgLocaleLocalization('zgh');
|
||||
it('should return the default value for a locale with no rule', () => {
|
||||
const l10n = new NgLocaleLocalization('zgh', useV4Plurals);
|
||||
|
||||
expect(l10n.getPluralCategory(0)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(1)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(3)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(5)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(10)).toEqual('other');
|
||||
});
|
||||
expect(l10n.getPluralCategory(0)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(1)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(3)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(5)).toEqual('other');
|
||||
expect(l10n.getPluralCategory(10)).toEqual('other');
|
||||
});
|
||||
}
|
||||
|
||||
ngLocaleLocalizationTests(true);
|
||||
ngLocaleLocalizationTests(false);
|
||||
});
|
||||
|
||||
describe('getPluralCategory', () => {
|
||||
function pluralCategoryTests(useV4Plurals: boolean) {
|
||||
it('should return plural category', () => {
|
||||
const l10n = new NgLocaleLocalization('fr');
|
||||
const l10n = new NgLocaleLocalization('fr', useV4Plurals);
|
||||
|
||||
expect(getPluralCategory(0, ['one', 'other'], l10n)).toEqual('one');
|
||||
expect(getPluralCategory(1, ['one', 'other'], l10n)).toEqual('one');
|
||||
@ -151,7 +186,7 @@ export function main() {
|
||||
});
|
||||
|
||||
it('should return discrete cases', () => {
|
||||
const l10n = new NgLocaleLocalization('fr');
|
||||
const l10n = new NgLocaleLocalization('fr', useV4Plurals);
|
||||
|
||||
expect(getPluralCategory(0, ['one', 'other', '=0'], l10n)).toEqual('=0');
|
||||
expect(getPluralCategory(1, ['one', 'other'], l10n)).toEqual('one');
|
||||
@ -160,7 +195,7 @@ export function main() {
|
||||
});
|
||||
|
||||
it('should fallback to other when the case is not present', () => {
|
||||
const l10n = new NgLocaleLocalization('ro');
|
||||
const l10n = new NgLocaleLocalization('ro', useV4Plurals);
|
||||
expect(getPluralCategory(1, ['one', 'other'], l10n)).toEqual('one');
|
||||
// 2 -> 'few'
|
||||
expect(getPluralCategory(2, ['one', 'other'], l10n)).toEqual('other');
|
||||
@ -169,12 +204,17 @@ export function main() {
|
||||
describe('errors', () => {
|
||||
it('should report an error when the "other" category is not present', () => {
|
||||
expect(() => {
|
||||
const l10n = new NgLocaleLocalization('ro');
|
||||
const l10n = new NgLocaleLocalization('ro', useV4Plurals);
|
||||
// 2 -> 'few'
|
||||
getPluralCategory(2, ['one'], l10n);
|
||||
}).toThrowError('No plural message found for value "2"');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe('getPluralCategory', () => {
|
||||
pluralCategoryTests(false);
|
||||
pluralCategoryTests(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user