feat: make salutation i18n static
This commit is contained in:
@ -1,31 +1,31 @@
|
||||
import {describe, it, expect} from 'vitest'
|
||||
import {hourToSalutation} from './useDateTimeSalutation'
|
||||
import {hourToDaytime} from './useDateTimeSalutation'
|
||||
|
||||
const dateWithHour = (hours: number): Date => {
|
||||
const date = new Date()
|
||||
date.setHours(hours)
|
||||
return date
|
||||
function dateWithHour(hours: number): Date {
|
||||
const newDate = new Date()
|
||||
newDate.setHours(hours, 0, 0,0 )
|
||||
return newDate
|
||||
}
|
||||
|
||||
describe('Salutation', () => {
|
||||
it('shows the right salutation in the night', () => {
|
||||
const salutation = hourToSalutation(dateWithHour(4))
|
||||
expect(salutation).toBe('home.welcomeNight')
|
||||
const salutation = hourToDaytime(dateWithHour(4))
|
||||
expect(salutation).toBe('night')
|
||||
})
|
||||
it('shows the right salutation in the morning', () => {
|
||||
const salutation = hourToSalutation(dateWithHour(8))
|
||||
expect(salutation).toBe('home.welcomeMorning')
|
||||
const salutation = hourToDaytime(dateWithHour(8))
|
||||
expect(salutation).toBe('morning')
|
||||
})
|
||||
it('shows the right salutation in the day', () => {
|
||||
const salutation = hourToSalutation(dateWithHour(13))
|
||||
expect(salutation).toBe('home.welcomeDay')
|
||||
const salutation = hourToDaytime(dateWithHour(13))
|
||||
expect(salutation).toBe('day')
|
||||
})
|
||||
it('shows the right salutation in the night', () => {
|
||||
const salutation = hourToSalutation(dateWithHour(20))
|
||||
expect(salutation).toBe('home.welcomeEvening')
|
||||
const salutation = hourToDaytime(dateWithHour(20))
|
||||
expect(salutation).toBe('evening')
|
||||
})
|
||||
it('shows the right salutation in the night again', () => {
|
||||
const salutation = hourToSalutation(dateWithHour(23))
|
||||
expect(salutation).toBe('home.welcomeNight')
|
||||
const salutation = hourToDaytime(dateWithHour(23))
|
||||
expect(salutation).toBe('night')
|
||||
})
|
||||
})
|
@ -1,31 +1,35 @@
|
||||
import {computed} from 'vue'
|
||||
import {useNow} from '@vueuse/core'
|
||||
import {computed, unref} from 'vue'
|
||||
import {useI18n} from 'vue-i18n'
|
||||
import {useNow, type MaybeRef} from '@vueuse/core'
|
||||
|
||||
const TRANSLATION_KEY_PREFIX = 'home.welcome'
|
||||
type Daytime = 'night' | 'morning' | 'day' | 'evening'
|
||||
|
||||
export function hourToSalutation(now: Date) {
|
||||
export function hourToDaytime(now: Date): Daytime {
|
||||
const hours = now.getHours()
|
||||
|
||||
if (hours < 5) {
|
||||
return `${TRANSLATION_KEY_PREFIX}Night`
|
||||
}
|
||||
const daytimeMap = {
|
||||
night: hours < 5 || hours > 23,
|
||||
morning: hours < 11,
|
||||
day: hours < 18,
|
||||
evening: hours < 23,
|
||||
} as Record<Daytime, boolean>
|
||||
|
||||
if (hours < 11) {
|
||||
return `${TRANSLATION_KEY_PREFIX}Morning`
|
||||
}
|
||||
|
||||
if (hours < 18) {
|
||||
return `${TRANSLATION_KEY_PREFIX}Day`
|
||||
}
|
||||
|
||||
if (hours < 23) {
|
||||
return `${TRANSLATION_KEY_PREFIX}Evening`
|
||||
}
|
||||
|
||||
return `${TRANSLATION_KEY_PREFIX}Night`
|
||||
return (Object.keys(daytimeMap) as Daytime[]).find((daytime) => daytimeMap[daytime]) || 'night'
|
||||
}
|
||||
|
||||
export function useDateTimeSalutation() {
|
||||
export function useDateTimeSalutation(username: MaybeRef<string>) {
|
||||
const {t} = useI18n({useScope: 'global'})
|
||||
const now = useNow()
|
||||
return computed(() => hourToSalutation(now.value))
|
||||
|
||||
const daytime = computed(() => hourToDaytime(now.value))
|
||||
const name = computed(() => unref(username))
|
||||
|
||||
const salutations = {
|
||||
'night': () => t('home.welcomeNight', {username: name.value}),
|
||||
'morning': () => t('home.welcomeMorning', {username: name.value}),
|
||||
'day': () => t('home.welcomeDay', {username: name.value}),
|
||||
'evening': () => t('home.welcomeEvening', {username: name.value}),
|
||||
} as Record<Daytime, () => string>
|
||||
|
||||
return computed(() => salutations[daytime.value]())
|
||||
}
|
Reference in New Issue
Block a user