From 24bf3e2a251634811096b939e61d63297934579e Mon Sep 17 00:00:00 2001 From: Olivier Combe Date: Fri, 24 Nov 2017 18:47:24 +0100 Subject: [PATCH] feat(common): add locale id parameter to `registerLocaleData` (#20623) PR Close #20623 --- packages/common/locales/closure-locale.ts | 3 +-- packages/common/src/i18n/locale_data.ts | 12 ++++++++++-- packages/common/test/i18n/locale_data_api_spec.ts | 10 +++++++++- tools/gulp-tasks/cldr/closure.js | 3 +-- tools/public_api_guard/common/common.d.ts | 2 +- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/common/locales/closure-locale.ts b/packages/common/locales/closure-locale.ts index f3652ae80d..6caf61e94e 100644 --- a/packages/common/locales/closure-locale.ts +++ b/packages/common/locales/closure-locale.ts @@ -4895,6 +4895,5 @@ switch (goog.LOCALE) { } if (l) { - l[0] = goog.LOCALE; - registerLocaleData(l); + registerLocaleData(l, goog.LOCALE); } diff --git a/packages/common/src/i18n/locale_data.ts b/packages/common/src/i18n/locale_data.ts index dd657ff71b..8dd54bcbc3 100644 --- a/packages/common/src/i18n/locale_data.ts +++ b/packages/common/src/i18n/locale_data.ts @@ -17,9 +17,17 @@ export const LOCALE_DATA: {[localeId: string]: any} = {}; * * @experimental i18n support is experimental. */ -export function registerLocaleData(data: any, extraData?: any) { - const localeId = data[LocaleDataIndex.LocaleId].toLowerCase().replace(/_/g, '-'); +// The signature registerLocaleData(data: any, extraData?: any) is deprecated since v5.1 +export function registerLocaleData(data: any, localeId?: string | any, extraData?: any): void { + if (typeof localeId !== 'string') { + extraData = localeId; + localeId = data[LocaleDataIndex.LocaleId]; + } + + localeId = localeId.toLowerCase().replace(/_/g, '-'); + LOCALE_DATA[localeId] = data; + if (extraData) { LOCALE_DATA[localeId][LocaleDataIndex.ExtraData] = extraData; } diff --git a/packages/common/test/i18n/locale_data_api_spec.ts b/packages/common/test/i18n/locale_data_api_spec.ts index a8053ea5e8..7d659446e3 100644 --- a/packages/common/test/i18n/locale_data_api_spec.ts +++ b/packages/common/test/i18n/locale_data_api_spec.ts @@ -10,8 +10,8 @@ import localeCaESVALENCIA from '../../locales/ca-ES-VALENCIA'; import localeEn from '../../locales/en'; import localeFr from '../../locales/fr'; import localeFrCA from '../../locales/fr-CA'; -import {findLocaleData} from '../../src/i18n/locale_data_api'; import {registerLocaleData} from '../../src/i18n/locale_data'; +import {findLocaleData} from '../../src/i18n/locale_data_api'; export function main() { describe('locale data api', () => { @@ -20,6 +20,8 @@ export function main() { registerLocaleData(localeEn); registerLocaleData(localeFr); registerLocaleData(localeFrCA); + registerLocaleData(localeFr, 'fake-id'); + registerLocaleData(localeFrCA, 'fake_Id2'); }); describe('findLocaleData', () => { @@ -42,6 +44,12 @@ export function main() { expect(findLocaleData('ca-ES-VALENCIA')).toEqual(localeCaESVALENCIA); expect(findLocaleData('CA_es_Valencia')).toEqual(localeCaESVALENCIA); }); + + it(`should find the LOCALE_DATA if the locale id was registered`, () => { + expect(findLocaleData('fake-id')).toEqual(localeFr); + expect(findLocaleData('fake_iD')).toEqual(localeFr); + expect(findLocaleData('fake-id2')).toEqual(localeFrCA); + }); }); }); } diff --git a/tools/gulp-tasks/cldr/closure.js b/tools/gulp-tasks/cldr/closure.js index 4650e6bcce..3b887c5fe3 100644 --- a/tools/gulp-tasks/cldr/closure.js +++ b/tools/gulp-tasks/cldr/closure.js @@ -143,8 +143,7 @@ switch (goog.LOCALE) { ${LOCALES.map(locale => generateCases(locale)).join('')}} if(l) { - l[0] = goog.LOCALE; - registerLocaleData(l); + registerLocaleData(l, goog.LOCALE); } `; // clang-format on diff --git a/tools/public_api_guard/common/common.d.ts b/tools/public_api_guard/common/common.d.ts index afaa89eaaf..07ccaf97dd 100644 --- a/tools/public_api_guard/common/common.d.ts +++ b/tools/public_api_guard/common/common.d.ts @@ -422,7 +422,7 @@ export interface PopStateEvent { } /** @experimental */ -export declare function registerLocaleData(data: any, extraData?: any): void; +export declare function registerLocaleData(data: any, localeId?: string | any, extraData?: any): void; /** @stable */ export declare class SlicePipe implements PipeTransform {