Skip to content

Commit dd82b64

Browse files
committed
refactor(common): remove AVAILABLE_LOCALES
Fixes #18855
1 parent 3db84c8 commit dd82b64

File tree

6 files changed

+15
-254
lines changed

6 files changed

+15
-254
lines changed

packages/common/src/common.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export * from './location/index';
1515
export {NgLocaleLocalization, NgLocalization} from './i18n/localization';
1616
export {Plural, LOCALE_DATA} from './i18n/locale_data';
1717
export {findLocaleData, registerLocaleData, NumberFormatStyle, FormStyle, Time, TranslationWidth, FormatWidth, NumberSymbol, WeekDay, getLocaleDayPeriods, getLocaleDayNames, getLocaleMonthNames, getLocaleId, getLocaleEraNames, getLocaleWeekEndRange, getLocaleFirstDayOfWeek, getLocaleDateFormat, getLocaleDateTimeFormat, getLocaleExtraDayPeriodRules, getLocaleExtraDayPeriods, getLocalePluralCase, getLocaleTimeFormat, getLocaleNumberSymbol, getLocaleNumberFormat, getLocaleCurrencyName, getLocaleCurrencySymbol} from './i18n/locale_data_api';
18-
export {AVAILABLE_LOCALES} from './i18n/available_locales';
1918
export {CURRENCIES} from './i18n/currencies';
2019
export {parseCookieValue as ɵparseCookieValue} from './cookie';
2120
export {CommonModule, DeprecatedI18NPipesModule} from './common_module';

packages/common/src/i18n/available_locales.ts

Lines changed: 0 additions & 186 deletions
This file was deleted.

packages/common/src/i18n/locale_data_api.ts

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {AVAILABLE_LOCALES} from './available_locales';
109
import {CURRENCIES} from './currencies';
1110
import localeEn from './locale_en';
1211
import {LOCALE_DATA, Plural} from './locale_data';
@@ -528,55 +527,26 @@ function extractTime(time: string): Time {
528527
* @experimental i18n support is experimental.
529528
*/
530529
export function findLocaleData(locale: string): any {
531-
const normalizedLocale = getNormalizedLocale(locale);
532-
533-
if (normalizedLocale === 'en') {
534-
return LOCALE_DATA['en'] || localeEn;
535-
}
530+
const normalizedLocale = locale.toLowerCase().replace(/_/g, '-');
536531

537-
const match = LOCALE_DATA[toCamelCase(normalizedLocale)];
532+
let match = LOCALE_DATA[normalizedLocale];
538533
if (match) {
539534
return match;
540535
}
541536

542-
throw new Error(
543-
`Missing locale data for the locale "${locale}". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.`);
544-
}
545-
546-
const NORMALIZED_LOCALES: any = {};
547-
548-
/**
549-
* Returns the closest matching locale that exists or throw
550-
* e.g.: "en-US" will return "en", and "fr_ca" will return "fr-CA"
551-
* Rules for locale id equivalences are defined in
552-
* http://cldr.unicode.org/index/cldr-spec/language-tag-equivalences
553-
* and in https://tools.ietf.org/html/rfc4647#section-3.4
554-
*/
555-
function getNormalizedLocale(locale: string): string {
556-
if (NORMALIZED_LOCALES[locale]) {
557-
return NORMALIZED_LOCALES[locale];
558-
}
559-
560-
const normalizedLocale = locale.toLowerCase().replace(/_/g, '-');
561-
const match = AVAILABLE_LOCALES.find((l: string) => l.toLowerCase() === normalizedLocale);
562-
537+
// let's try to find a parent locale
538+
const parentLocale = normalizedLocale.split('-')[0];
539+
match = LOCALE_DATA[parentLocale];
563540
if (match) {
564-
NORMALIZED_LOCALES[locale] = match;
565541
return match;
566542
}
567543

568-
const parentLocale = normalizedLocale.split('-')[0];
569-
if (AVAILABLE_LOCALES.find((l: string) => l.toLowerCase() === parentLocale)) {
570-
NORMALIZED_LOCALES[locale] = parentLocale;
571-
return parentLocale;
544+
if (parentLocale === 'en') {
545+
return localeEn;
572546
}
573547

574548
throw new Error(
575-
`"${locale}" is not a valid LOCALE_ID value. See https://github.com/unicode-cldr/cldr-core/blob/master/availableLocales.json for a list of valid locales`);
576-
}
577-
578-
function toCamelCase(str: string): string {
579-
return str.replace(/-+([a-z0-9A-Z])/g, (...m: string[]) => m[1].toUpperCase());
549+
`Missing locale data for the locale "${locale}". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.`);
580550
}
581551

582552
/**
@@ -598,7 +568,7 @@ export function findCurrencySymbol(code: string, format: 'wide' | 'narrow') {
598568
* @experimental i18n support is experimental.
599569
*/
600570
export function registerLocaleData(data: any, extraData?: any) {
601-
const localeId = toCamelCase(data[LocaleDataIndex.LocaleId]);
571+
const localeId = data[LocaleDataIndex.LocaleId].toLowerCase();
602572
LOCALE_DATA[localeId] = data;
603573
if (extraData) {
604574
LOCALE_DATA[localeId][LocaleDataIndex.ExtraData] = extraData;

packages/common/test/i18n/locale_data_api_spec.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,11 @@ export function main() {
2222
});
2323

2424
describe('findLocaleData', () => {
25-
it('should throw if the locale provided is not a valid LOCALE_ID', () => {
26-
expect(() => findLocaleData('invalid'))
27-
.toThrow(new Error(
28-
`"invalid" is not a valid LOCALE_ID value. See https://github.com/unicode-cldr/cldr-core/blob/master/availableLocales.json for a list of valid locales`));
29-
});
30-
31-
it('should throw if the LOCALE_DATA for the chosen locale if not available', () => {
32-
expect(() => findLocaleData('fr-BE'))
33-
.toThrowError(/Missing locale data for the locale "fr-BE"/);
34-
});
25+
it('should throw if the LOCALE_DATA for the chosen locale or its parent locale is not available',
26+
() => {
27+
expect(() => findLocaleData('pt-AO'))
28+
.toThrowError(/Missing locale data for the locale "pt-AO"/);
29+
});
3530

3631
it('should return english data if the locale is en-US',
3732
() => { expect(findLocaleData('en-US')).toEqual(localeEn); });
@@ -40,7 +35,7 @@ export function main() {
4035
() => { expect(findLocaleData('fr-CA')).toEqual(localeFrCA); });
4136

4237
it('should return the parent LOCALE_DATA if it exists and exact locale is not available',
43-
() => { expect(findLocaleData('fr-FR')).toEqual(localeFr); });
38+
() => { expect(findLocaleData('fr-BE')).toEqual(localeFr); });
4439

4540
it(`should find the LOCALE_DATA even if the locale id is badly formatted`, () => {
4641
expect(findLocaleData('ca-ES-VALENCIA')).toEqual(localeCaESVALENCIA);

tools/gulp-tasks/cldr/extract.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ module.exports = (gulp, done) => {
6868
console.log(`Writing file ${I18N_FOLDER}/locale_en.ts`);
6969
fs.writeFileSync(`${RELATIVE_I18N_FOLDER}/locale_en.ts`, generateLocale('en', new cldrJs('en'), './locale_data'));
7070

71-
console.log(`Writing file ${I18N_FOLDER}/available_locales.ts`);
72-
fs.writeFileSync(`${RELATIVE_I18N_FOLDER}/available_locales.ts`, generateAvailableLocales(LOCALES));
73-
7471
console.log(`Writing file ${I18N_FOLDER}/currencies.ts`);
7572
fs.writeFileSync(`${RELATIVE_I18N_FOLDER}/currencies.ts`, generateCurrencies());
7673

@@ -80,7 +77,6 @@ module.exports = (gulp, done) => {
8077
return gulp
8178
.src([
8279
`${I18N_DATA_FOLDER}/**/*.ts`,
83-
`${I18N_FOLDER}/available_locales.ts`,
8480
`${I18N_FOLDER}/currencies.ts`,
8581
`${I18N_FOLDER}/locale_en.ts`
8682
], {base: '.'})
@@ -151,16 +147,6 @@ export default ${stringify(dayPeriodsSupplemental).replace(/undefined/g, '')};
151147
`;
152148
}
153149

154-
/**
155-
* Generate a file that contains the complete list of locales
156-
*/
157-
function generateAvailableLocales(LOCALES) {
158-
return `${HEADER}
159-
/** @experimental */
160-
export const AVAILABLE_LOCALES = ${stringify(LOCALES)};
161-
`;
162-
}
163-
164150
/**
165151
* Generate a file that contains the list of currencies and their symbols
166152
*/

tools/public_api_guard/common/common.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ export declare class AsyncPipe implements OnDestroy, PipeTransform {
1111
transform<T>(obj: null): null;
1212
}
1313

14-
/** @experimental */
15-
export declare const AVAILABLE_LOCALES: string[];
16-
1714
/** @stable */
1815
export declare class CommonModule {
1916
}

0 commit comments

Comments
 (0)