build(common): inject deprecated `getPluralCase` for treeshaking (#19136)

Fixes #19115
PR Close #19136
This commit is contained in:
Olivier Combe 2017-09-11 11:34:30 +02:00 committed by Matias Niemelä
parent bf94f878bc
commit 1aa8401ddf
5 changed files with 46 additions and 31 deletions

View File

@ -8,7 +8,7 @@
import {NgModule} from '@angular/core';
import {COMMON_DIRECTIVES} from './directives/index';
import {NgLocaleLocalization, NgLocalization, USE_V4_PLURALS} from './i18n/localization';
import {DEPRECATED_PLURAL_FN, NgLocaleLocalization, NgLocalization, getPluralCase} from './i18n/localization';
import {COMMON_DEPRECATED_I18N_PIPES} from './pipes/deprecated/index';
import {COMMON_PIPES} from './pipes/index';
@ -38,7 +38,7 @@ export class CommonModule {
@NgModule({
declarations: [COMMON_DEPRECATED_I18N_PIPES],
exports: [COMMON_DEPRECATED_I18N_PIPES],
providers: [{provide: USE_V4_PLURALS, useValue: true}],
providers: [{provide: DEPRECATED_PLURAL_FN, useValue: getPluralCase}],
})
export class DeprecatedI18NPipesModule {
}

View File

@ -13,7 +13,7 @@ import {Plural, getLocalePluralCase} from './locale_data_api';
/**
* @deprecated from v5
*/
export const USE_V4_PLURALS = new InjectionToken<boolean>('UseV4Plurals');
export const DEPRECATED_PLURAL_FN = new InjectionToken<boolean>('UseV4Plurals');
/**
* @experimental
@ -60,13 +60,15 @@ export function getPluralCategory(
export class NgLocaleLocalization extends NgLocalization {
constructor(
@Inject(LOCALE_ID) protected locale: string,
@Optional() @Inject(USE_V4_PLURALS) protected useV4Plurals?: boolean) {
/** @deprecated from v5 */
@Optional() @Inject(DEPRECATED_PLURAL_FN) protected deprecatedPluralFn?:
((locale: string, value: number|string) => Plural)|null) {
super();
}
getPluralCategory(value: any, locale?: string): string {
const plural = this.useV4Plurals ? getPluralCase(locale || this.locale, value) :
getLocalePluralCase(locale || this.locale)(value);
const plural = this.deprecatedPluralFn ? this.deprecatedPluralFn(locale || this.locale, value) :
getLocalePluralCase(locale || this.locale)(value);
switch (plural) {
case Plural.Zero:
@ -91,7 +93,7 @@ export class NgLocaleLocalization extends NgLocalization {
* @deprecated from v5 the plural case function is in locale data files common/locales/*.ts
* @experimental
*/
function getPluralCase(locale: string, nLike: number | string): Plural {
export function getPluralCase(locale: string, nLike: number | string): Plural {
// TODO(vicb): lazy compute
if (typeof nLike === 'string') {
nLike = parseInt(<string>nLike, 10);

View File

@ -9,13 +9,18 @@
import {TestBed, inject} from '@angular/core/testing';
import {DeprecatedI18NPipesModule} from '../src/common_module';
import {USE_V4_PLURALS} from '../src/i18n/localization';
import {Plural} from '../src/i18n/locale_data_api';
import {DEPRECATED_PLURAL_FN, getPluralCase} 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); }));
it('should define the token DEPRECATED_PLURAL_FN',
inject(
[DEPRECATED_PLURAL_FN],
(injectedGetPluralCase?: (locale: string, value: number | string) => Plural) => {
expect(injectedGetPluralCase).toEqual(getPluralCase);
}));
});
}

View File

@ -12,7 +12,8 @@ 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, USE_V4_PLURALS} from '../../src/i18n/localization';
import {NgLocaleLocalization, NgLocalization, getPluralCategory, DEPRECATED_PLURAL_FN, getPluralCase} from '../../src/i18n/localization';
import {Plural} from '../../src/i18n/locale_data_api';
import {registerLocaleData} from '../../src/i18n/locale_data';
export function main() {
@ -48,8 +49,10 @@ export function main() {
describe('ro with v4 plurals', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers:
[{provide: LOCALE_ID, useValue: 'ro'}, {provide: USE_V4_PLURALS, useValue: true}],
providers: [
{provide: LOCALE_ID, useValue: 'ro'},
{provide: DEPRECATED_PLURAL_FN, useValue: getPluralCase}
],
});
});
@ -83,8 +86,10 @@ export function main() {
describe('sr with v4 plurals', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers:
[{provide: LOCALE_ID, useValue: 'sr'}, {provide: USE_V4_PLURALS, useValue: true}],
providers: [
{provide: LOCALE_ID, useValue: 'sr'},
{provide: DEPRECATED_PLURAL_FN, useValue: getPluralCase}
],
});
});
@ -93,9 +98,10 @@ export function main() {
});
describe('NgLocaleLocalization', () => {
function ngLocaleLocalizationTests(useV4Plurals: boolean) {
function ngLocaleLocalizationTests(
getPluralCase: ((locale: string, value: number | string) => Plural) | null) {
it('should return the correct values for the "en" locale', () => {
const l10n = new NgLocaleLocalization('en-US', useV4Plurals);
const l10n = new NgLocaleLocalization('en-US', getPluralCase);
expect(l10n.getPluralCategory(0)).toEqual('other');
expect(l10n.getPluralCategory(1)).toEqual('one');
@ -103,7 +109,7 @@ export function main() {
});
it('should return the correct values for the "ro" locale', () => {
const l10n = new NgLocaleLocalization('ro', useV4Plurals);
const l10n = new NgLocaleLocalization('ro', getPluralCase);
expect(l10n.getPluralCategory(0)).toEqual('few');
expect(l10n.getPluralCategory(1)).toEqual('one');
@ -115,7 +121,7 @@ export function main() {
});
it('should return the correct values for the "sr" locale', () => {
const l10n = new NgLocaleLocalization('sr', useV4Plurals);
const l10n = new NgLocaleLocalization('sr', getPluralCase);
expect(l10n.getPluralCategory(1)).toEqual('one');
expect(l10n.getPluralCategory(31)).toEqual('one');
@ -162,7 +168,7 @@ export function main() {
});
it('should return the default value for a locale with no rule', () => {
const l10n = new NgLocaleLocalization('zgh', useV4Plurals);
const l10n = new NgLocaleLocalization('zgh', getPluralCase);
expect(l10n.getPluralCategory(0)).toEqual('other');
expect(l10n.getPluralCategory(1)).toEqual('other');
@ -172,13 +178,14 @@ export function main() {
});
}
ngLocaleLocalizationTests(true);
ngLocaleLocalizationTests(false);
ngLocaleLocalizationTests(null);
ngLocaleLocalizationTests(getPluralCase);
});
function pluralCategoryTests(useV4Plurals: boolean) {
function pluralCategoryTests(
getPluralCase: ((locale: string, value: number | string) => Plural) | null) {
it('should return plural category', () => {
const l10n = new NgLocaleLocalization('fr', useV4Plurals);
const l10n = new NgLocaleLocalization('fr', getPluralCase);
expect(getPluralCategory(0, ['one', 'other'], l10n)).toEqual('one');
expect(getPluralCategory(1, ['one', 'other'], l10n)).toEqual('one');
@ -186,7 +193,7 @@ export function main() {
});
it('should return discrete cases', () => {
const l10n = new NgLocaleLocalization('fr', useV4Plurals);
const l10n = new NgLocaleLocalization('fr', getPluralCase);
expect(getPluralCategory(0, ['one', 'other', '=0'], l10n)).toEqual('=0');
expect(getPluralCategory(1, ['one', 'other'], l10n)).toEqual('one');
@ -195,7 +202,7 @@ export function main() {
});
it('should fallback to other when the case is not present', () => {
const l10n = new NgLocaleLocalization('ro', useV4Plurals);
const l10n = new NgLocaleLocalization('ro', getPluralCase);
expect(getPluralCategory(1, ['one', 'other'], l10n)).toEqual('one');
// 2 -> 'few'
expect(getPluralCategory(2, ['one', 'other'], l10n)).toEqual('other');
@ -204,7 +211,7 @@ export function main() {
describe('errors', () => {
it('should report an error when the "other" category is not present', () => {
expect(() => {
const l10n = new NgLocaleLocalization('ro', useV4Plurals);
const l10n = new NgLocaleLocalization('ro', getPluralCase);
// 2 -> 'few'
getPluralCategory(2, ['one'], l10n);
}).toThrowError('No plural message found for value "2"');
@ -213,8 +220,8 @@ export function main() {
}
describe('getPluralCategory', () => {
pluralCategoryTests(false);
pluralCategoryTests(true);
pluralCategoryTests(null);
pluralCategoryTests(getPluralCase);
});
});
}

View File

@ -286,9 +286,10 @@ export declare class NgIfContext {
/** @experimental */
export declare class NgLocaleLocalization extends NgLocalization {
/** @deprecated */ protected deprecatedPluralFn: ((locale: string, value: string | number) => Plural) | null | undefined;
protected locale: string;
protected useV4Plurals: boolean | undefined;
constructor(locale: string, useV4Plurals?: boolean | undefined);
constructor(locale: string,
deprecatedPluralFn?: ((locale: string, value: string | number) => Plural) | null | undefined);
getPluralCategory(value: any, locale?: string): string;
}