New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
build(common): add global UMD export to locale files #33504
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ | |||||
|
||||||
import {LOCALE_DATA, LocaleDataIndex} from './locale_data'; | ||||||
import localeEn from './locale_en'; | ||||||
import {global} from '../util/global'; | ||||||
|
||||||
/** | ||||||
* Retrieves the plural function used by ICU expressions to determine the plural case to use | ||||||
|
@@ -30,16 +31,16 @@ export function getLocalePluralCase(locale: string): (value: number) => number { | |||||
* @see [Internationalization (i18n) Guide](https://angular.io/guide/i18n) | ||||||
*/ | ||||||
export function findLocaleData(locale: string): any { | ||||||
const normalizedLocale = locale.toLowerCase().replace(/_/g, '-'); | ||||||
const normalizedLocale = normalizeLocale(locale); | ||||||
|
||||||
let match = LOCALE_DATA[normalizedLocale]; | ||||||
let match = getLocaleData(normalizedLocale); | ||||||
if (match) { | ||||||
return match; | ||||||
} | ||||||
|
||||||
// let's try to find a parent locale | ||||||
const parentLocale = normalizedLocale.split('-')[0]; | ||||||
match = LOCALE_DATA[parentLocale]; | ||||||
match = getLocaleData(parentLocale); | ||||||
|
||||||
if (match) { | ||||||
return match; | ||||||
|
@@ -51,3 +52,33 @@ export function findLocaleData(locale: string): any { | |||||
|
||||||
throw new Error(`Missing locale data for the locale "${locale}".`); | ||||||
} | ||||||
|
||||||
function getLocaleData(normalizedLocale: string): any { | ||||||
if (normalizedLocale in LOCALE_DATA) { | ||||||
return LOCALE_DATA[normalizedLocale]; | ||||||
} | ||||||
|
||||||
if (typeof global.ng === 'undefined') global.ng = {}; | ||||||
if (typeof global.ng.common === 'undefined') global.ng.common = {}; | ||||||
if (typeof global.ng.common.locale === 'undefined') global.ng.common.locale = {}; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these checks just bail if the locale object is not present? How strict should these checks be? If someone decides to set ng.common.locale to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you are right - we should just bail. And we don't need to worry about using |
||||||
|
||||||
// The locale names on the global object are not normalized, so we have to do a search. | ||||||
// This is only once per requested locale; after that it is cached on LOCALE_DATA. | ||||||
// Also generally only one or very few locales should be loaded onto the global. | ||||||
for (const l in global.ng.common.locale) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Probably won't be a problem but |
||||||
if (normalizeLocale(l) === normalizedLocale) { | ||||||
const localeData = LOCALE_DATA[normalizedLocale] = global.ng.common.locale[l]; | ||||||
if (localeData !== undefined) { | ||||||
localeData[LocaleDataIndex.ExtraData] = global.ng.common.locale[`extra/${l}`]; | ||||||
return localeData; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return undefined; | ||||||
} | ||||||
|
||||||
|
||||||
function normalizeLocale(locale: string): string { | ||||||
return locale.toLowerCase().replace(/_/g, '-'); | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than exporting
ɵLOCALE_DATA
from the@angular/core
could we:clearLocaleData()
method and use it in theafterAll
functionsgetLocaleData
asɵgetLocaleData
in case it is needed. (optional)registerLocaleData
from@angular/common
to@angular/core
and than export it asɵregisterLocaleData
.@angular/common
re-exportɵregisterLocaleData
asregisterLocaleData
so that it maintains backwards compatibility with public API.