1
0

feat: make salutation i18n static

This commit is contained in:
Dominik Pschenitschni 2022-10-16 15:26:02 +02:00
parent ed56176f2d
commit c20de51a3c
No known key found for this signature in database
GPG Key ID: B257AC0149F43A77
4 changed files with 48 additions and 43 deletions

View File

@ -1,31 +1,31 @@
import {describe, it, expect} from 'vitest' import {describe, it, expect} from 'vitest'
import {hourToSalutation} from './useDateTimeSalutation' import {hourToDaytime} from './useDateTimeSalutation'
const dateWithHour = (hours: number): Date => { function dateWithHour(hours: number): Date {
const date = new Date() const newDate = new Date()
date.setHours(hours) newDate.setHours(hours, 0, 0,0 )
return date return newDate
} }
describe('Salutation', () => { describe('Salutation', () => {
it('shows the right salutation in the night', () => { it('shows the right salutation in the night', () => {
const salutation = hourToSalutation(dateWithHour(4)) const salutation = hourToDaytime(dateWithHour(4))
expect(salutation).toBe('home.welcomeNight') expect(salutation).toBe('night')
}) })
it('shows the right salutation in the morning', () => { it('shows the right salutation in the morning', () => {
const salutation = hourToSalutation(dateWithHour(8)) const salutation = hourToDaytime(dateWithHour(8))
expect(salutation).toBe('home.welcomeMorning') expect(salutation).toBe('morning')
}) })
it('shows the right salutation in the day', () => { it('shows the right salutation in the day', () => {
const salutation = hourToSalutation(dateWithHour(13)) const salutation = hourToDaytime(dateWithHour(13))
expect(salutation).toBe('home.welcomeDay') expect(salutation).toBe('day')
}) })
it('shows the right salutation in the night', () => { it('shows the right salutation in the night', () => {
const salutation = hourToSalutation(dateWithHour(20)) const salutation = hourToDaytime(dateWithHour(20))
expect(salutation).toBe('home.welcomeEvening') expect(salutation).toBe('evening')
}) })
it('shows the right salutation in the night again', () => { it('shows the right salutation in the night again', () => {
const salutation = hourToSalutation(dateWithHour(23)) const salutation = hourToDaytime(dateWithHour(23))
expect(salutation).toBe('home.welcomeNight') expect(salutation).toBe('night')
}) })
}) })

View File

@ -1,31 +1,35 @@
import {computed} from 'vue' import {computed, unref} from 'vue'
import {useNow} from '@vueuse/core' 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() const hours = now.getHours()
if (hours < 5) { const daytimeMap = {
return `${TRANSLATION_KEY_PREFIX}Night` night: hours < 5 || hours > 23,
morning: hours < 11,
day: hours < 18,
evening: hours < 23,
} as Record<Daytime, boolean>
return (Object.keys(daytimeMap) as Daytime[]).find((daytime) => daytimeMap[daytime]) || 'night'
} }
if (hours < 11) { export function useDateTimeSalutation(username: MaybeRef<string>) {
return `${TRANSLATION_KEY_PREFIX}Morning` const {t} = useI18n({useScope: 'global'})
}
if (hours < 18) {
return `${TRANSLATION_KEY_PREFIX}Day`
}
if (hours < 23) {
return `${TRANSLATION_KEY_PREFIX}Evening`
}
return `${TRANSLATION_KEY_PREFIX}Night`
}
export function useDateTimeSalutation() {
const now = useNow() 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]())
} }

View File

@ -1,9 +1,9 @@
{ {
"home": { "home": {
"welcomeNight": "Good Night {username}", "welcomeNight": "Good Night {username}!",
"welcomeMorning": "Good Morning {username}", "welcomeMorning": "Good Morning {username}!",
"welcomeDay": "Hi {username}", "welcomeDay": "Hi {username}!",
"welcomeEvening": "Good Evening {username}", "welcomeEvening": "Good Evening {username}!",
"lastViewed": "Last viewed", "lastViewed": "Last viewed",
"list": { "list": {
"newText": "You can create a new list for your new tasks:", "newText": "You can create a new list for your new tasks:",

View File

@ -1,7 +1,7 @@
<template> <template>
<div class="content has-text-centered"> <div class="content has-text-centered">
<h2 v-if="userInfo"> <h2 v-if="userInfo">
{{ $t(welcome, {username: userInfo.name !== '' ? userInfo.name : userInfo.username}) }}! {{ salutation }}
</h2> </h2>
<message variant="danger" v-if="deletionScheduledAt !== null" class="mb-4"> <message variant="danger" v-if="deletionScheduledAt !== null" class="mb-4">
{{ {{
@ -78,7 +78,8 @@ import {useNamespaceStore} from '@/stores/namespaces'
import {useAuthStore} from '@/stores/auth' import {useAuthStore} from '@/stores/auth'
import {useTaskStore} from '@/stores/tasks' import {useTaskStore} from '@/stores/tasks'
const welcome = useDateTimeSalutation() const username = computed(() => userInfo.name !== '' ? userInfo.name : userInfo.username)
const salutation = useDateTimeSalutation(username)
const baseStore = useBaseStore() const baseStore = useBaseStore()
const authStore = useAuthStore() const authStore = useAuthStore()