Skip to content

Commit

Permalink
feat(common): add locale id parameter to registerLocaleData
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Nov 27, 2017
1 parent 0444e13 commit c1abc3a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
3 changes: 1 addition & 2 deletions packages/common/locales/closure-locale.ts
Expand Up @@ -4895,6 +4895,5 @@ switch (goog.LOCALE) {
}

if (l) {
l[0] = goog.LOCALE;
registerLocaleData(l);
registerLocaleData(l, goog.LOCALE);
}
20 changes: 18 additions & 2 deletions packages/common/src/i18n/locale_data.ts
Expand Up @@ -15,11 +15,27 @@ 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.
*
* The signature registerLocaleData(data: any, extraData?: any) is deprecated since v5.1
* Use registerLocaleData(data: any, localeId?: string, extraData?: any) instead.
*
* @experimental i18n support is experimental.
*/
export function registerLocaleData(data: any, extraData?: any) {
const localeId = data[LocaleDataIndex.LocaleId].toLowerCase().replace(/_/g, '-');
// we cannot overwrite the function signature like we do with classes because it doesn't
// generate the correct api file.
// See: https://github.com/angular/ts-api-guardian/issues/22
// todo(ocombe): uncomment the following lines when the bug is fixed
// export function registerLocaleData(data: any, extraData?: any): void;
// export function registerLocaleData(data: any, localeId?: string, extraData?: any): void;
export function registerLocaleData(data: any, localeId?: string | any, extraData?: any): void {
if (typeof localeId !== 'string') {
extraData = extraData || localeId;
localeId = data[LocaleDataIndex.LocaleId];
}

localeId = localeId.toLowerCase().replace(/_/g, '-');

LOCALE_DATA[localeId] = data;

if (extraData) {
LOCALE_DATA[localeId][LocaleDataIndex.ExtraData] = extraData;
}
Expand Down
23 changes: 20 additions & 3 deletions packages/common/test/i18n/locale_data_api_spec.ts
Expand Up @@ -10,8 +10,12 @@ 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 localeDeCh from '../../locales/de-CH';
import localeDeChExtra from '../../locales/extra/de-CH';
import localeIt from '../../locales/it';
import localeItExtra from '../../locales/extra/it';
import {registerLocaleData} from '../../src/i18n/locale_data';
import {findLocaleData} from '../../src/i18n/locale_data_api';

export function main() {
describe('locale data api', () => {
Expand All @@ -20,6 +24,10 @@ export function main() {
registerLocaleData(localeEn);
registerLocaleData(localeFr);
registerLocaleData(localeFrCA);
registerLocaleData(localeDeCh, undefined, localeDeChExtra);
registerLocaleData(localeIt, localeItExtra);
registerLocaleData(localeFr, 'fake-id');
registerLocaleData(localeFrCA, 'fake_Id2');
});

describe('findLocaleData', () => {
Expand All @@ -32,8 +40,11 @@ export function main() {
it('should return english data if the locale is en-US',
() => { expect(findLocaleData('en-US')).toEqual(localeEn); });

it('should return the exact LOCALE_DATA if it is available',
() => { expect(findLocaleData('fr-CA')).toEqual(localeFrCA); });
it('should return the exact LOCALE_DATA if it is available', () => {
expect(findLocaleData('fr-CA')).toEqual(localeFrCA);
expect(findLocaleData('de-CH')).toEqual(localeDeCh);
expect(findLocaleData('it')).toEqual(localeIt);
});

it('should return the parent LOCALE_DATA if it exists and exact locale is not available',
() => { expect(findLocaleData('fr-BE')).toEqual(localeFr); });
Expand All @@ -42,6 +53,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);
});
});
});
}
3 changes: 1 addition & 2 deletions tools/gulp-tasks/cldr/closure.js
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/common/common.d.ts
Expand Up @@ -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 {
Expand Down

0 comments on commit c1abc3a

Please sign in to comment.