feat(common): add locale id parameter to `registerLocaleData` (#20623)

PR Close #20623
This commit is contained in:
Olivier Combe 2017-11-24 18:47:24 +01:00 committed by Miško Hevery
parent 8ecda94899
commit 24bf3e2a25
5 changed files with 22 additions and 8 deletions

View File

@ -4895,6 +4895,5 @@ switch (goog.LOCALE) {
}
if (l) {
l[0] = goog.LOCALE;
registerLocaleData(l);
registerLocaleData(l, goog.LOCALE);
}

View File

@ -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;
}

View File

@ -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);
});
});
});
}

View File

@ -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

View File

@ -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 {