refactor(common): refactor i18n code to ease tree shaking (#18907)

PR Close #18907
This commit is contained in:
Victor Berchet
2017-08-23 17:10:27 -07:00
committed by Miško Hevery
parent 4c45347635
commit 832876d0a1
9 changed files with 85 additions and 98 deletions

View File

@ -13,8 +13,8 @@
*/
export * from './location/index';
export {NgLocaleLocalization, NgLocalization} from './i18n/localization';
export {Plural, LOCALE_DATA} from './i18n/locale_data';
export {findLocaleData, registerLocaleData, NumberFormatStyle, FormStyle, Time, TranslationWidth, FormatWidth, NumberSymbol, WeekDay, getLocaleDayPeriods, getLocaleDayNames, getLocaleMonthNames, getLocaleId, getLocaleEraNames, getLocaleWeekEndRange, getLocaleFirstDayOfWeek, getLocaleDateFormat, getLocaleDateTimeFormat, getLocaleExtraDayPeriodRules, getLocaleExtraDayPeriods, getLocalePluralCase, getLocaleTimeFormat, getLocaleNumberSymbol, getLocaleNumberFormat, getLocaleCurrencyName, getLocaleCurrencySymbol} from './i18n/locale_data_api';
export {registerLocaleData} from './i18n/locale_data';
export {Plural, NumberFormatStyle, FormStyle, Time, TranslationWidth, FormatWidth, NumberSymbol, WeekDay, getLocaleDayPeriods, getLocaleDayNames, getLocaleMonthNames, getLocaleId, getLocaleEraNames, getLocaleWeekEndRange, getLocaleFirstDayOfWeek, getLocaleDateFormat, getLocaleDateTimeFormat, getLocaleExtraDayPeriodRules, getLocaleExtraDayPeriods, getLocalePluralCase, getLocaleTimeFormat, getLocaleNumberSymbol, getLocaleNumberFormat, getLocaleCurrencyName, getLocaleCurrencySymbol} from './i18n/locale_data_api';
export {CURRENCIES} from './i18n/currencies';
export {parseCookieValue as ɵparseCookieValue} from './cookie';
export {CommonModule, DeprecatedI18NPipesModule} from './common_module';

View File

@ -6,17 +6,55 @@
* found in the LICENSE file at https://angular.io/license
*/
/** @experimental */
export enum Plural {
Zero,
One,
Two,
Few,
Many,
Other,
}
/**
* @experimental i18n support is experimental.
*/
export const LOCALE_DATA: {[localeId: string]: any} = {};
/**
* Register global data to be used internally by Angular. See the
* {@linkDocs guide/i18n#i18n-pipes "I18n guide"} to know how to import additional locale data.
*
* @experimental i18n support is experimental.
*/
export function registerLocaleData(data: any, extraData?: any) {
const localeId = data[LocaleDataIndex.LocaleId].toLowerCase();
LOCALE_DATA[localeId] = data;
if (extraData) {
LOCALE_DATA[localeId][LocaleDataIndex.ExtraData] = extraData;
}
}
/**
* Index of each type of locale data from the locale data array
*/
export const enum LocaleDataIndex {
LocaleId = 0,
DayPeriodsFormat,
DayPeriodsStandalone,
DaysFormat,
DaysStandalone,
MonthsFormat,
MonthsStandalone,
Eras,
FirstDayOfWeek,
WeekendRange,
DateFormat,
TimeFormat,
DateTimeFormat,
NumberSymbols,
NumberFormats,
CurrencySymbol,
CurrencyName,
PluralCase,
ExtraData
}
/**
* Index of each type of locale data from the extra locale data array
*/
export const enum ExtraLocaleDataIndex {
ExtraDayPeriodFormats = 0,
ExtraDayPeriodStandalone,
ExtraDayPeriodsRules
}

View File

@ -8,7 +8,7 @@
import {CURRENCIES} from './currencies';
import localeEn from './locale_en';
import {LOCALE_DATA, Plural} from './locale_data';
import {LOCALE_DATA, LocaleDataIndex, ExtraLocaleDataIndex} from './locale_data';
/**
* The different format styles that can be used to represent numbers.
@ -23,6 +23,16 @@ export enum NumberFormatStyle {
Scientific
}
/** @experimental */
export enum Plural {
Zero = 0,
One = 1,
Two = 2,
Few = 3,
Many = 4,
Other = 5,
}
/**
* Some languages use two different forms of strings (standalone and format) depending on the
* context.
@ -130,40 +140,6 @@ export enum WeekDay {
Saturday
}
/**
* Use this enum to find the index of each type of locale data from the locale data array
*/
enum LocaleDataIndex {
LocaleId = 0,
DayPeriodsFormat,
DayPeriodsStandalone,
DaysFormat,
DaysStandalone,
MonthsFormat,
MonthsStandalone,
Eras,
FirstDayOfWeek,
WeekendRange,
DateFormat,
TimeFormat,
DateTimeFormat,
NumberSymbols,
NumberFormats,
CurrencySymbol,
CurrencyName,
PluralCase,
ExtraData
}
/**
* Use this enum to find the index of each type of locale data from the extra locale data array
*/
enum ExtraLocaleDataIndex {
ExtraDayPeriodFormats = 0,
ExtraDayPeriodStandalone,
ExtraDayPeriodsRules
}
/**
* The locale id for the chosen locale (e.g `en-GB`).
*
@ -537,6 +513,7 @@ export function findLocaleData(locale: string): any {
// let's try to find a parent locale
const parentLocale = normalizedLocale.split('-')[0];
match = LOCALE_DATA[parentLocale];
if (match) {
return match;
}
@ -545,8 +522,7 @@ export function findLocaleData(locale: string): any {
return localeEn;
}
throw new Error(
`Missing locale data for the locale "${locale}". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.`);
throw new Error(`Missing locale data for the locale "${locale}".`);
}
/**
@ -559,18 +535,4 @@ export function findCurrencySymbol(code: string, format: 'wide' | 'narrow') {
const currency = CURRENCIES[code] || {};
const symbol = currency[0] || code;
return format === 'wide' ? symbol : currency[1] || symbol;
}
/**
* Register global data to be used internally by Angular. See the
* {@linkDocs guide/i18n#i18n-pipes "I18n guide"} to know how to import additional locale data.
*
* @experimental i18n support is experimental.
*/
export function registerLocaleData(data: any, extraData?: any) {
const localeId = data[LocaleDataIndex.LocaleId].toLowerCase();
LOCALE_DATA[localeId] = data;
if (extraData) {
LOCALE_DATA[localeId][LocaleDataIndex.ExtraData] = extraData;
}
}
}

View File

@ -7,8 +7,7 @@
*/
import {Inject, Injectable, LOCALE_ID} from '@angular/core';
import {Plural} from './locale_data';
import {getLocalePluralCase} from './locale_data_api';
import {Plural, getLocalePluralCase} from './locale_data_api';
/**
* @experimental

View File

@ -10,7 +10,8 @@ import localeCaESVALENCIA from '../../i18n_data/locale_ca-ES-VALENCIA';
import localeEn from '../../i18n_data/locale_en';
import localeFr from '../../i18n_data/locale_fr';
import localeFrCA from '../../i18n_data/locale_fr-CA';
import {registerLocaleData, findLocaleData} from '../../src/i18n/locale_data_api';
import {findLocaleData} from '../../src/i18n/locale_data_api';
import {registerLocaleData} from '../../src/i18n/locale_data';
export function main() {
describe('locale data api', () => {

View File

@ -13,7 +13,7 @@ import localeFr from '../../i18n_data/locale_fr';
import {LOCALE_ID} from '@angular/core';
import {TestBed, inject} from '@angular/core/testing';
import {NgLocaleLocalization, NgLocalization, getPluralCategory} from '../../src/i18n/localization';
import {registerLocaleData} from '../../src/i18n/locale_data_api';
import {registerLocaleData} from '../../src/i18n/locale_data';
export function main() {
describe('l10n', () => {

View File

@ -56,7 +56,7 @@ export function main() {
});
describe('transform with custom locales', () => {
it('should return the correct format for es-US in IE11', () => {
it('should return the correct format for es-US', () => {
const pipe = new DecimalPipe('es-US');
expect(pipe.transform('9999999.99', '1.2-2')).toEqual('9,999,999.99');
});