Skip to content

Commit

Permalink
Merge pull request #5501 from nextcloud-libraries/fix/date-time-lang
Browse files Browse the repository at this point in the history
fix(NcDateTime): Respect language
  • Loading branch information
Pytal authored Apr 22, 2024
2 parents 8dbc23e + 4f57f6d commit e7ef548
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/composables/useFormatDateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -48,7 +48,7 @@ interface FormatDateOptions {
type MaybeRef<T> = T | Ref<T>

/**
* 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<Date> | import('vue').Ref<number>} timestamp Current timestamp
* @param {object} opts Optional options
Expand Down Expand Up @@ -87,7 +87,7 @@ export function useFormatDateTime(
/** Time string formatted for main text */
const formattedTime = computed<string>(() => {
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
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/components/NcDateTime/NcDateTime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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)
Expand Down

0 comments on commit e7ef548

Please sign in to comment.