diff --git a/npm/ng-packs/packages/core/locale/src/utils/register-locale.ts b/npm/ng-packs/packages/core/locale/src/utils/register-locale.ts index 97a49066ec..5d6ae52645 100644 --- a/npm/ng-packs/packages/core/locale/src/utils/register-locale.ts +++ b/npm/ng-packs/packages/core/locale/src/utils/register-locale.ts @@ -15,6 +15,72 @@ export interface RegisterLocaleData { errorHandlerFn?: (data: LocaleErrorHandlerData) => any; } + +function loadLocale(locale: string) { + // hard coded list works with esbuild. Source https://github.com/angular/angular-cli/issues/26904#issuecomment-1903596563 + + const list = { + 'ar': () => import('@angular/common/locales/ar'), + 'cs': () => import('@angular/common/locales/cs'), + 'en': () => import('@angular/common/locales/en'), + 'en-GB': () => import('@angular/common/locales/en-GB'), + 'es': () => import('@angular/common/locales/es'), + 'de': () => import('@angular/common/locales/de'), + 'fi': () => import('@angular/common/locales/fi'), + 'fr': () => import('@angular/common/locales/fr'), + 'hi': () => import('@angular/common/locales/hi'), + 'hu': () => import('@angular/common/locales/hu'), + 'is': () => import('@angular/common/locales/is'), + 'it': () => import('@angular/common/locales/it'), + 'pt': () => import('@angular/common/locales/pt'), + 'tr': () => import('@angular/common/locales/tr'), + 'ru': () => import('@angular/common/locales/ru'), + 'ro': () => import('@angular/common/locales/ro'), + 'sk': () => import('@angular/common/locales/sk'), + 'sl': () => import('@angular/common/locales/sl'), + 'zh-Hans': () => import('@angular/common/locales/zh-Hans'), + 'zh-Hant': () => import('@angular/common/locales/zh-Hant') + } + return list[locale](); +} + +export function registerLocaleForEsBuild( + { + cultureNameLocaleFileMap = {}, + errorHandlerFn = defaultLocalErrorHandlerFn, + } = {} as RegisterLocaleData, +) { + return (locale: string): Promise => { + localeMap = { ...differentLocales, ...cultureNameLocaleFileMap }; + const l = localeMap[locale] || locale; + const localeSupportList = "ar|cs|en|en-GB|es|de|fi|fr|hi|hu|is|it|pt|tr|ru|ro|sk|sl|zh-Hans|zh-Hant".split("|"); + + if (localeSupportList.indexOf(locale) == -1) { + return; + } + return new Promise((resolve, reject) => { + return loadLocale(l) + .then(val => { + let module = val; + while (module.default) { + module = module.default; + } + resolve({ default: module }); + }) + .catch(error => { + errorHandlerFn({ + resolve, + reject, + error, + locale, + }); + }); + }); + }; +} + + + export function registerLocale( { cultureNameLocaleFileMap = {},