Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 5 additions & 26 deletions core/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {
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()

Expand Down
35 changes: 35 additions & 0 deletions core/src/tests/utils/momentLocale.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
42 changes: 42 additions & 0 deletions core/src/utils/momentLocale.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> = {
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<void> {
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)
}
Loading