Browse Source

Merge pull request #20096 from mahmut-gundogdu/bugfix/19607

Add registerLocale function that supported esbuild
pull/20256/head
Masum ULU 2 years ago
committed by GitHub
parent
commit
0563943d97
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 66
      npm/ng-packs/packages/core/locale/src/utils/register-locale.ts

66
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<any> => {
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 = {},

Loading…
Cancel
Save