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 e6accdeb8c3..ea5d34ea2e0 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 @@ -11,6 +11,35 @@ export interface LocaleErrorHandlerData { let localeMap = {} as { [key: string]: string }; +const localeLoaders: Record Promise> = { + 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'), + ja: () => import('@angular/common/locales/ja'), + ko: () => import('@angular/common/locales/ko'), + pt: () => import('@angular/common/locales/pt'), + ro: () => import('@angular/common/locales/ro'), + ru: () => import('@angular/common/locales/ru'), + sk: () => import('@angular/common/locales/sk'), + sl: () => import('@angular/common/locales/sl'), + th: () => import('@angular/common/locales/th'), + tr: () => import('@angular/common/locales/tr'), + vi: () => import('@angular/common/locales/vi'), + 'zh-Hans': () => import('@angular/common/locales/zh-Hans'), + 'zh-Hant': () => import('@angular/common/locales/zh-Hant'), +}; + +const localeSupportList = Object.keys(localeLoaders); + export interface RegisterLocaleData { cultureNameLocaleFileMap?: Record; errorHandlerFn?: (data: LocaleErrorHandlerData) => any; @@ -19,29 +48,11 @@ export interface RegisterLocaleData { 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](); + if (localeSupportList.indexOf(locale) === -1) { + return Promise.reject(new Error(`Cannot find the ${locale} locale file.`)); + } + + return localeLoaders[locale](); } export function registerLocaleForEsBuild( @@ -53,12 +64,6 @@ export function registerLocaleForEsBuild( 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(l) == -1) { - return; - } return new Promise((resolve, reject) => { return loadLocale(l) .then(val => { @@ -93,7 +98,7 @@ export function registerLocale( return import( /* webpackMode: "lazy-once" */ /* webpackChunkName: "locales"*/ - /* webpackInclude: /[/\\](ar|cs|en|en-GB|es|de|fi|fr|hi|hu|is|it|pt|tr|ru|ro|sk|sl|zh-Hans|zh-Hant)\.(mjs|js)$/ */ + /* webpackInclude: /[/\\](ar|cs|en|en-GB|es|de|fi|fr|hi|hu|is|it|ja|ko|pt|ro|ru|sk|sl|th|tr|vi|zh-Hans|zh-Hant)\.(mjs|js)$/ */ /* webpackExclude: /[/\\]global|extra/ */ /* @vite-ignore */ `@angular/common${localePath}` diff --git a/npm/ng-packs/packages/core/src/lib/tests/register-locale.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/register-locale.spec.ts new file mode 100644 index 00000000000..451f55cb024 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tests/register-locale.spec.ts @@ -0,0 +1,29 @@ +import { registerLocaleForEsBuild, storeLocaleData } from '../../../locale/src/utils/register-locale'; + +describe('registerLocaleForEsBuild', () => { + it('should always return a promise for unsupported locales', async () => { + const errorHandlerFn = vi.fn(({ resolve }) => resolve({ default: null })); + + const result = registerLocaleForEsBuild({ errorHandlerFn })('xx'); + + expect(result).toBeInstanceOf(Promise); + await expect(result).resolves.toEqual({ default: null }); + expect(errorHandlerFn).toHaveBeenCalledWith( + expect.objectContaining({ locale: 'xx', error: expect.any(Error) }), + ); + }); + + it('should load supported locales', async () => { + await expect(registerLocaleForEsBuild()('ko')).resolves.toEqual( + expect.objectContaining({ default: expect.anything() }), + ); + }); + + it('should resolve stored locale data for unsupported locales', async () => { + const locale = 'xx-extra'; + const data = { hello: 'world' }; + storeLocaleData(data, locale); + + await expect(registerLocaleForEsBuild()(locale)).resolves.toEqual({ default: data }); + }); +});