diff --git a/src/composables/useFormatDateTime.ts b/src/composables/useFormatDateTime.ts index 1158a1497f..3ca694bb31 100644 --- a/src/composables/useFormatDateTime.ts +++ b/src/composables/useFormatDateTime.ts @@ -20,7 +20,7 @@ * */ -import { getCanonicalLocale } from '@nextcloud/l10n' +import { getCanonicalLocale, getLanguage } from '@nextcloud/l10n' import { computed, onUnmounted, ref, onMounted, watch, unref, type Ref } from 'vue' import { t } from '../l10n.js' @@ -48,7 +48,7 @@ interface FormatDateOptions { type MaybeRef = T | Ref /** - * Composable for formatting time stamps using current users locale + * Composable for formatting time stamps using current users locale and language * * @param {Date | number | import('vue').Ref | import('vue').Ref} timestamp Current timestamp * @param {object} opts Optional options @@ -87,7 +87,7 @@ export function useFormatDateTime( /** Time string formatted for main text */ const formattedTime = computed(() => { if (wrappedOptions.value.relativeTime !== false) { - const formatter = new Intl.RelativeTimeFormat(getCanonicalLocale(), { numeric: 'auto', style: wrappedOptions.value.relativeTime }) + const formatter = new Intl.RelativeTimeFormat(getLanguage(), { numeric: 'auto', style: wrappedOptions.value.relativeTime }) const diff = date.value.getTime() - currentTime.value const seconds = diff / 1000 diff --git a/tests/unit/components/NcDateTime/NcDateTime.spec.js b/tests/unit/components/NcDateTime/NcDateTime.spec.js index 0cd58f1200..8df30d29a4 100644 --- a/tests/unit/components/NcDateTime/NcDateTime.spec.js +++ b/tests/unit/components/NcDateTime/NcDateTime.spec.js @@ -84,7 +84,7 @@ describe('NcDateTime.vue', () => { /** * Use German locale as it uses a different date format than English */ - it('', () => { + it('Works with absolute timestamps', () => { const time = Date.UTC(2023, 5, 23, 14, 30) jest.setSystemTime(time) const wrapper = mount(NcDateTime, { @@ -98,6 +98,31 @@ describe('NcDateTime.vue', () => { }) }) + describe('Work with different languages', () => { + beforeAll(() => { + // mock the language + document.documentElement.lang = 'de' + }) + afterAll(() => { + // revert mock + document.documentElement.lang = 'en' + }) + + it('Works with relative timestamps', () => { + const now = Date.UTC(2023, 5, 24, 14, 0) + jest.setSystemTime(now) + + const yesterday = Date.UTC(2023, 5, 23, 13, 59) + const wrapper = mount(NcDateTime, { + propsData: { + timestamp: yesterday, + }, + }) + + expect(wrapper.text()).toMatch('gestern') + }) + }) + describe('Shows relative time', () => { it('works with currentTime == timestamp', () => { const time = Date.UTC(2023, 5, 23, 14, 30)