Skip to content

Commit

Permalink
feat(common): support loading locales from a global
Browse files Browse the repository at this point in the history
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()`.
  • Loading branch information
petebacondarwin committed Nov 1, 2019
1 parent 6cf443b commit 321b6a8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/core/src/i18n/locale_data_api.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}

Expand Down
24 changes: 23 additions & 1 deletion packages/core/test/i18n/locale_data_api_spec.ts
Expand Up @@ -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);
Expand All @@ -25,9 +29,18 @@ import {findLocaleData, registerLocaleData, unregisterLocaleData} from '../../sr
registerLocaleData(localeFrCA, 'fake_Id2');
registerLocaleData(localeZh);
registerLocaleData(localeEnAU);
global.ng = global.ng || {};
global.ng.common = global.ng.common || {locale: {}};
global.ng.common.locale = 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',
Expand Down Expand Up @@ -55,6 +68,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); });
});
});
}

0 comments on commit 321b6a8

Please sign in to comment.