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
`ng.common.locale`.

This commit changes `findLocaleData()` so that it will
attempt to read the local from the global if the locale
has not already been registered.
  • Loading branch information
petebacondarwin committed Nov 1, 2019
1 parent 8561186 commit bdddf78
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 231 deletions.
1 change: 0 additions & 1 deletion packages/common/public_api.ts
Expand Up @@ -12,6 +12,5 @@
* Entry point for all public APIs of this package.
*/
export * from './src/common';
export {registerLocaleData as ɵregisterLocaleData} from './src/i18n/locale_data';

// This file only reexports content of the `src` folder. Keep it that way.
2 changes: 1 addition & 1 deletion packages/common/src/common.ts
Expand Up @@ -16,7 +16,7 @@ export * from './location/index';
export {formatDate} from './i18n/format_date';
export {formatCurrency, formatNumber, formatPercent} from './i18n/format_number';
export {NgLocaleLocalization, NgLocalization} from './i18n/localization';
export {registerLocaleData} from './i18n/locale_data';
export {ɵregisterLocaleData as registerLocaleData} from '@angular/core';
export {Plural, NumberFormatStyle, FormStyle, Time, TranslationWidth, FormatWidth, NumberSymbol, WeekDay, getNumberOfCurrencyDigits, getCurrencySymbol, getLocaleDayPeriods, getLocaleDayNames, getLocaleMonthNames, getLocaleId, getLocaleEraNames, getLocaleWeekEndRange, getLocaleFirstDayOfWeek, getLocaleDateFormat, getLocaleDateTimeFormat, getLocaleExtraDayPeriodRules, getLocaleExtraDayPeriods, getLocalePluralCase, getLocaleTimeFormat, getLocaleNumberSymbol, getLocaleNumberFormat, getLocaleCurrencyName, getLocaleCurrencySymbol} from './i18n/locale_data_api';
export {parseCookieValue as ɵparseCookieValue} from './cookie';
export {CommonModule} from './common_module';
Expand Down
45 changes: 0 additions & 45 deletions packages/common/src/i18n/locale_data.ts

This file was deleted.

85 changes: 42 additions & 43 deletions packages/common/src/i18n/locale_data_api.ts
Expand Up @@ -6,9 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ɵLocaleDataIndex as LocaleDataIndex, ɵfindLocaleData as findLocaleData, ɵgetLocalePluralCase} from '@angular/core';
import {ɵCurrencyIndex, ɵExtraLocaleDataIndex, ɵLocaleDataIndex, ɵfindLocaleData, ɵgetLocalePluralCase} from '@angular/core';
import {CURRENCIES_EN, CurrenciesSymbols} from './currencies';
import {CurrencyIndex, ExtraLocaleDataIndex} from './locale_data';

/**
* Format styles that can be used to represent numbers.
Expand Down Expand Up @@ -217,7 +216,7 @@ export enum WeekDay {
* @publicApi
*/
export function getLocaleId(locale: string): string {
return findLocaleData(locale)[LocaleDataIndex.LocaleId];
return ɵfindLocaleData(locale)[ɵLocaleDataIndex.LocaleId];
}

/**
Expand All @@ -233,10 +232,10 @@ export function getLocaleId(locale: string): string {
*/
export function getLocaleDayPeriods(
locale: string, formStyle: FormStyle, width: TranslationWidth): [string, string] {
const data = findLocaleData(locale);
const data = ɵfindLocaleData(locale);
const amPmData = <[
string, string
][][]>[data[LocaleDataIndex.DayPeriodsFormat], data[LocaleDataIndex.DayPeriodsStandalone]];
][][]>[data[ɵLocaleDataIndex.DayPeriodsFormat], data[ɵLocaleDataIndex.DayPeriodsStandalone]];
const amPm = getLastDefinedValue(amPmData, formStyle);
return getLastDefinedValue(amPm, width);
}
Expand All @@ -255,9 +254,9 @@ export function getLocaleDayPeriods(
*/
export function getLocaleDayNames(
locale: string, formStyle: FormStyle, width: TranslationWidth): string[] {
const data = findLocaleData(locale);
const data = ɵfindLocaleData(locale);
const daysData =
<string[][][]>[data[LocaleDataIndex.DaysFormat], data[LocaleDataIndex.DaysStandalone]];
<string[][][]>[data[ɵLocaleDataIndex.DaysFormat], data[ɵLocaleDataIndex.DaysStandalone]];
const days = getLastDefinedValue(daysData, formStyle);
return getLastDefinedValue(days, width);
}
Expand All @@ -276,9 +275,9 @@ export function getLocaleDayNames(
*/
export function getLocaleMonthNames(
locale: string, formStyle: FormStyle, width: TranslationWidth): string[] {
const data = findLocaleData(locale);
const data = ɵfindLocaleData(locale);
const monthsData =
<string[][][]>[data[LocaleDataIndex.MonthsFormat], data[LocaleDataIndex.MonthsStandalone]];
<string[][][]>[data[ɵLocaleDataIndex.MonthsFormat], data[ɵLocaleDataIndex.MonthsStandalone]];
const months = getLastDefinedValue(monthsData, formStyle);
return getLastDefinedValue(months, width);
}
Expand All @@ -296,8 +295,8 @@ export function getLocaleMonthNames(
* @publicApi
*/
export function getLocaleEraNames(locale: string, width: TranslationWidth): [string, string] {
const data = findLocaleData(locale);
const erasData = <[string, string][]>data[LocaleDataIndex.Eras];
const data = ɵfindLocaleData(locale);
const erasData = <[string, string][]>data[ɵLocaleDataIndex.Eras];
return getLastDefinedValue(erasData, width);
}

Expand All @@ -313,8 +312,8 @@ export function getLocaleEraNames(locale: string, width: TranslationWidth): [str
* @publicApi
*/
export function getLocaleFirstDayOfWeek(locale: string): WeekDay {
const data = findLocaleData(locale);
return data[LocaleDataIndex.FirstDayOfWeek];
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.FirstDayOfWeek];
}

/**
Expand All @@ -327,8 +326,8 @@ export function getLocaleFirstDayOfWeek(locale: string): WeekDay {
* @publicApi
*/
export function getLocaleWeekEndRange(locale: string): [WeekDay, WeekDay] {
const data = findLocaleData(locale);
return data[LocaleDataIndex.WeekendRange];
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.WeekendRange];
}

/**
Expand All @@ -343,8 +342,8 @@ export function getLocaleWeekEndRange(locale: string): [WeekDay, WeekDay] {
* @publicApi
*/
export function getLocaleDateFormat(locale: string, width: FormatWidth): string {
const data = findLocaleData(locale);
return getLastDefinedValue(data[LocaleDataIndex.DateFormat], width);
const data = ɵfindLocaleData(locale);
return getLastDefinedValue(data[ɵLocaleDataIndex.DateFormat], width);
}

/**
Expand All @@ -359,8 +358,8 @@ export function getLocaleDateFormat(locale: string, width: FormatWidth): string
* @publicApi
*/
export function getLocaleTimeFormat(locale: string, width: FormatWidth): string {
const data = findLocaleData(locale);
return getLastDefinedValue(data[LocaleDataIndex.TimeFormat], width);
const data = ɵfindLocaleData(locale);
return getLastDefinedValue(data[ɵLocaleDataIndex.TimeFormat], width);
}

/**
Expand All @@ -375,8 +374,8 @@ export function getLocaleTimeFormat(locale: string, width: FormatWidth): string
* @publicApi
*/
export function getLocaleDateTimeFormat(locale: string, width: FormatWidth): string {
const data = findLocaleData(locale);
const dateTimeFormatData = <string[]>data[LocaleDataIndex.DateTimeFormat];
const data = ɵfindLocaleData(locale);
const dateTimeFormatData = <string[]>data[ɵLocaleDataIndex.DateTimeFormat];
return getLastDefinedValue(dateTimeFormatData, width);
}

Expand All @@ -391,13 +390,13 @@ export function getLocaleDateTimeFormat(locale: string, width: FormatWidth): str
* @publicApi
*/
export function getLocaleNumberSymbol(locale: string, symbol: NumberSymbol): string {
const data = findLocaleData(locale);
const res = data[LocaleDataIndex.NumberSymbols][symbol];
const data = ɵfindLocaleData(locale);
const res = data[ɵLocaleDataIndex.NumberSymbols][symbol];
if (typeof res === 'undefined') {
if (symbol === NumberSymbol.CurrencyDecimal) {
return data[LocaleDataIndex.NumberSymbols][NumberSymbol.Decimal];
return data[ɵLocaleDataIndex.NumberSymbols][NumberSymbol.Decimal];
} else if (symbol === NumberSymbol.CurrencyGroup) {
return data[LocaleDataIndex.NumberSymbols][NumberSymbol.Group];
return data[ɵLocaleDataIndex.NumberSymbols][NumberSymbol.Group];
}
}
return res;
Expand Down Expand Up @@ -439,8 +438,8 @@ export function getLocaleNumberSymbol(locale: string, symbol: NumberSymbol): str
* @publicApi
*/
export function getLocaleNumberFormat(locale: string, type: NumberFormatStyle): string {
const data = findLocaleData(locale);
return data[LocaleDataIndex.NumberFormats][type];
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.NumberFormats][type];
}

/**
Expand All @@ -455,8 +454,8 @@ export function getLocaleNumberFormat(locale: string, type: NumberFormatStyle):
* @publicApi
*/
export function getLocaleCurrencySymbol(locale: string): string|null {
const data = findLocaleData(locale);
return data[LocaleDataIndex.CurrencySymbol] || null;
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.CurrencySymbol] || null;
}

/**
Expand All @@ -470,8 +469,8 @@ export function getLocaleCurrencySymbol(locale: string): string|null {
* @publicApi
*/
export function getLocaleCurrencyName(locale: string): string|null {
const data = findLocaleData(locale);
return data[LocaleDataIndex.CurrencyName] || null;
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.CurrencyName] || null;
}

/**
Expand All @@ -481,8 +480,8 @@ export function getLocaleCurrencyName(locale: string): string|null {
* @see [Internationalization (i18n) Guide](https://angular.io/guide/i18n)
*/
function getLocaleCurrencies(locale: string): {[code: string]: CurrenciesSymbols} {
const data = findLocaleData(locale);
return data[LocaleDataIndex.Currencies];
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.Currencies];
}

/**
Expand All @@ -493,9 +492,9 @@ export const getLocalePluralCase: (locale: string) => ((value: number) => Plural
ɵgetLocalePluralCase;

function checkFullData(data: any) {
if (!data[LocaleDataIndex.ExtraData]) {
if (!data[ɵLocaleDataIndex.ExtraData]) {
throw new Error(
`Missing extra locale data for the locale "${data[LocaleDataIndex.LocaleId]}". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.`);
`Missing extra locale data for the locale "${data[ɵLocaleDataIndex.LocaleId]}". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.`);
}
}

Expand All @@ -522,9 +521,9 @@ function checkFullData(data: any) {
* @publicApi
*/
export function getLocaleExtraDayPeriodRules(locale: string): (Time | [Time, Time])[] {
const data = findLocaleData(locale);
const data = ɵfindLocaleData(locale);
checkFullData(data);
const rules = data[LocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodsRules] || [];
const rules = data[ɵLocaleDataIndex.ExtraData][ɵExtraLocaleDataIndex.ExtraDayPeriodsRules] || [];
return rules.map((rule: string | [string, string]) => {
if (typeof rule === 'string') {
return extractTime(rule);
Expand Down Expand Up @@ -552,11 +551,11 @@ export function getLocaleExtraDayPeriodRules(locale: string): (Time | [Time, Tim
*/
export function getLocaleExtraDayPeriods(
locale: string, formStyle: FormStyle, width: TranslationWidth): string[] {
const data = findLocaleData(locale);
const data = ɵfindLocaleData(locale);
checkFullData(data);
const dayPeriodsData = <string[][][]>[
data[LocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodFormats],
data[LocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodStandalone]
data[ɵLocaleDataIndex.ExtraData][ɵExtraLocaleDataIndex.ExtraDayPeriodFormats],
data[ɵLocaleDataIndex.ExtraData][ɵExtraLocaleDataIndex.ExtraDayPeriodStandalone]
];
const dayPeriods = getLastDefinedValue(dayPeriodsData, formStyle) || [];
return getLastDefinedValue(dayPeriods, width) || [];
Expand Down Expand Up @@ -621,13 +620,13 @@ function extractTime(time: string): Time {
*/
export function getCurrencySymbol(code: string, format: 'wide' | 'narrow', locale = 'en'): string {
const currency = getLocaleCurrencies(locale)[code] || CURRENCIES_EN[code] || [];
const symbolNarrow = currency[CurrencyIndex.SymbolNarrow];
const symbolNarrow = currency[ɵCurrencyIndex.SymbolNarrow];

if (format === 'narrow' && typeof symbolNarrow === 'string') {
return symbolNarrow;
}

return currency[CurrencyIndex.Symbol] || code;
return currency[ɵCurrencyIndex.Symbol] || code;
}

// Most currencies have cents, that's why the default is 2
Expand All @@ -647,7 +646,7 @@ export function getNumberOfCurrencyDigits(code: string): number {
let digits;
const currency = CURRENCIES_EN[code];
if (currency) {
digits = currency[CurrencyIndex.NbOfDigits];
digits = currency[ɵCurrencyIndex.NbOfDigits];
}
return typeof digits === 'number' ? digits : DEFAULT_NB_OF_CURRENCY_DIGITS;
}

0 comments on commit bdddf78

Please sign in to comment.