From c13b6770d437ae178a804083fe64d300634810b0 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Thu, 31 Oct 2019 12:55:54 +0000 Subject: [PATCH] feat(common): support loading locales from a global To support compile time localization, we need to be able to provide the locales via a well known global property. This commit changes `getLocaleData()` so that it will attempt to read the local from the global `ng.common.locale` if the locale has not already been registered via `registerLocaleData()`. --- packages/core/src/i18n/locale_data_api.ts | 8 ++++++- .../core/test/i18n/locale_data_api_spec.ts | 22 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/core/src/i18n/locale_data_api.ts b/packages/core/src/i18n/locale_data_api.ts index cdc2bd20d76c1d..e5fc3aaf735804 100644 --- a/packages/core/src/i18n/locale_data_api.ts +++ b/packages/core/src/i18n/locale_data_api.ts @@ -8,6 +8,7 @@ import {LOCALE_DATA, LocaleDataIndex} from './locale_data'; import localeEn from './locale_en'; +import {global} from '../util/global'; /** * Register locale data to be used internally by Angular. See the @@ -75,9 +76,14 @@ export function getLocalePluralCase(locale: string): (value: number) => number { /** - * Helper function to get the given `normalizedLocale` from `LOCALE_DATA`. + * Helper function to get the given `normalizedLocale` from `LOCALE_DATA` + * or from the global `ng.common.locale`. */ export function getLocaleData(normalizedLocale: string): any { + if (!(normalizedLocale in LOCALE_DATA)) { + LOCALE_DATA[normalizedLocale] = global.ng && global.ng.common && global.ng.common.locale && + global.ng.common.locale[normalizedLocale]; + } return LOCALE_DATA[normalizedLocale]; } diff --git a/packages/core/test/i18n/locale_data_api_spec.ts b/packages/core/test/i18n/locale_data_api_spec.ts index 00ab67726f3b23..07f663cb707b8c 100644 --- a/packages/core/test/i18n/locale_data_api_spec.ts +++ b/packages/core/test/i18n/locale_data_api_spec.ts @@ -6,15 +6,19 @@ * found in the LICENSE file at https://angular.io/license */ import {findLocaleData, registerLocaleData, unregisterLocaleData} from '../../src/i18n/locale_data_api'; +import {global} from '../../src/util/global'; { describe('locale data api', () => { const localeCaESVALENCIA: any[] = ['ca-ES-VALENCIA']; + const localeDe: any[] = ['de']; + const localeDeCH: any[] = ['de-CH']; const localeEn: any[] = ['en']; const localeFr: any[] = ['fr']; const localeFrCA: any[] = ['fr-CA']; const localeZh: any[] = ['zh']; const localeEnAU: any[] = ['en-AU']; + const fakeGlobalFr: any[] = ['fr']; beforeAll(() => { registerLocaleData(localeCaESVALENCIA); @@ -25,9 +29,16 @@ import {findLocaleData, registerLocaleData, unregisterLocaleData} from '../../sr registerLocaleData(localeFrCA, 'fake_Id2'); registerLocaleData(localeZh); registerLocaleData(localeEnAU); + global.ng = {common: {locale: {}}}; + global.ng.common.locale['fr'] = fakeGlobalFr; + global.ng.common.locale['de'] = localeDe; + global.ng.common.locale['de-ch'] = localeDeCH; }); - afterAll(() => unregisterLocaleData()); + afterAll(() => { + unregisterLocaleData(); + delete global.ng.common.locale; + }); describe('findLocaleData', () => { it('should throw if the LOCALE_DATA for the chosen locale or its parent locale is not available', @@ -55,6 +66,15 @@ import {findLocaleData, registerLocaleData, unregisterLocaleData} from '../../sr expect(findLocaleData('fake_iD')).toEqual(localeFr); expect(findLocaleData('fake-id2')).toEqual(localeFrCA); }); + + it('should find the exact LOCALE_DATA if the locale is on the global object', + () => { expect(findLocaleData('de-CH')).toEqual(localeDeCH); }); + + it('should find the parent LOCALE_DATA if the exact locale is not available and the parent locale is on the global object', + () => { expect(findLocaleData('de-BE')).toEqual(localeDe); }); + + it('should find the registered LOCALE_DATA even if the same locale is on the global object', + () => { expect(findLocaleData('fr')).not.toBe(fakeGlobalFr); }); }); }); }