diff --git a/core/src/init.ts b/core/src/init.ts index e2144e1dd0730..09068e1fa7ccc 100644 --- a/core/src/init.ts +++ b/core/src/init.ts @@ -4,42 +4,21 @@ */ import { getLocale } from '@nextcloud/l10n' -import moment from 'moment' import { setUp as setUpContactsMenu } from './components/ContactsMenu.ts' import { setUp as setUpUserMenu } from './components/UserMenu.ts' import { initSessionHeartBeat } from './session-heartbeat.ts' import { initFallbackClipboardAPI } from './utils/ClipboardFallback.ts' +import { logger } from './utils/logger.ts' +import { loadMomentLocale } from './utils/momentLocale.ts' import { interceptRequests } from './utils/xhr-request.js' -// moment resolves its locales through require(), which ES modules do not provide -import 'moment/min/locales.js' - -/** - * Moment doesn't have aliases for every locale and doesn't parse some locale IDs correctly so we need to alias them - */ -const localeAliases: Record = { - zh: 'zh-cn', - zh_Hans: 'zh-cn', - zh_Hans_CN: 'zh-cn', - zh_Hans_HK: 'zh-cn', - zh_Hans_MO: 'zh-cn', - zh_Hans_SG: 'zh-cn', - zh_Hant: 'zh-hk', - zh_Hant_HK: 'zh-hk', - zh_Hant_MO: 'zh-mo', - zh_Hant_TW: 'zh-tw', -} -const locale = localeAliases[getLocale()] ?? getLocale() - -/** - * Set users locale to moment.js as soon as possible - */ -moment.locale(locale) - /** * Initializes core */ export function initCore() { + // needs `OC` to resolve the locale chunk URL, so it runs here and not on import + loadMomentLocale(getLocale()) + .catch((error) => logger.error('Could not load the moment.js locale', { error })) interceptRequests() initFallbackClipboardAPI() diff --git a/core/src/tests/utils/momentLocale.spec.ts b/core/src/tests/utils/momentLocale.spec.ts new file mode 100644 index 0000000000000..057a52152c1f7 --- /dev/null +++ b/core/src/tests/utils/momentLocale.spec.ts @@ -0,0 +1,35 @@ +/*! + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import moment from 'moment' +import { afterEach, describe, expect, it } from 'vitest' +import { loadMomentLocale } from '../../utils/momentLocale.ts' + +describe('loadMomentLocale', () => { + afterEach(() => { + moment.locale('en') + }) + + it('loads the locale on the moment instance used by core', async () => { + await loadMomentLocale('de_DE') + expect(moment.locale()).toBe('de') + expect(moment.utc('2026-11-28T12:00:00Z').format('dddd, D. MMMM YYYY')).toBe('Samstag, 28. November 2026') + }) + + it('prefers the regional variant when moment has it', async () => { + await loadMomentLocale('en_GB') + expect(moment.locale()).toBe('en-gb') + }) + + it('maps Nextcloud Chinese locales to moment locales', async () => { + await loadMomentLocale('zh_Hant_TW') + expect(moment.locale()).toBe('zh-tw') + }) + + it('falls back to English for unknown locales', async () => { + await loadMomentLocale('xx_YY') + expect(moment.locale()).toBe('en') + }) +}) diff --git a/core/src/utils/momentLocale.ts b/core/src/utils/momentLocale.ts new file mode 100644 index 0000000000000..5639a97f8c3bc --- /dev/null +++ b/core/src/utils/momentLocale.ts @@ -0,0 +1,42 @@ +/*! + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import moment from 'moment' + +/** + * Moment doesn't have aliases for every locale and doesn't parse some locale IDs correctly so we need to alias them + */ +const localeAliases: Record = { + zh: 'zh-cn', + zh_Hans: 'zh-cn', + zh_Hans_CN: 'zh-cn', + zh_Hans_HK: 'zh-cn', + zh_Hans_MO: 'zh-cn', + zh_Hans_SG: 'zh-cn', + zh_Hant: 'zh-hk', + zh_Hant_HK: 'zh-hk', + zh_Hant_MO: 'zh-mo', + zh_Hant_TW: 'zh-tw', +} + +// `moment` resolves to its ES build, whose locale files register on that same instance +const momentLocales = import.meta.glob('/node_modules/moment/dist/locale/*.js') + +/** + * Load a locale into moment.js and make it the global locale. + * Only the requested locale is fetched instead of bundling all of them. + * + * @param nextcloudLocale - The Nextcloud locale, e.g. `de_DE` + */ +export async function loadMomentLocale(nextcloudLocale: string): Promise { + const locale = localeAliases[nextcloudLocale] ?? nextcloudLocale + const file = [locale.toLowerCase().replace('_', '-'), locale.split(/[_-]/)[0]!.toLowerCase()] + .map((name) => `/node_modules/moment/dist/locale/${name}.js`) + .find((file) => file in momentLocales) + if (file) { + await momentLocales[file]!() + } + moment.locale(locale) +}