Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NcDateTime): Respect language #5501

Merged
merged 2 commits into from
Apr 22, 2024
Merged
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
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
Loading