From c20de51a3c98792580c0a2f2751648582ac5ac0c Mon Sep 17 00:00:00 2001 From: Dominik Pschenitschni Date: Sun, 16 Oct 2022 15:26:02 +0200 Subject: [PATCH] feat: make salutation i18n static --- src/composables/useDateTimeSalutation.test.ts | 30 ++++++------ src/composables/useDateTimeSalutation.ts | 48 ++++++++++--------- src/i18n/lang/en.json | 8 ++-- src/views/Home.vue | 5 +- 4 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/composables/useDateTimeSalutation.test.ts b/src/composables/useDateTimeSalutation.test.ts index ecd48024f..15087036c 100644 --- a/src/composables/useDateTimeSalutation.test.ts +++ b/src/composables/useDateTimeSalutation.test.ts @@ -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') }) }) \ No newline at end of file diff --git a/src/composables/useDateTimeSalutation.ts b/src/composables/useDateTimeSalutation.ts index 001c5d80c..af1f0cd9c 100644 --- a/src/composables/useDateTimeSalutation.ts +++ b/src/composables/useDateTimeSalutation.ts @@ -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 - 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) { + 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 string> + + return computed(() => salutations[daytime.value]()) } \ No newline at end of file diff --git a/src/i18n/lang/en.json b/src/i18n/lang/en.json index a4712a277..c7320af8b 100644 --- a/src/i18n/lang/en.json +++ b/src/i18n/lang/en.json @@ -1,9 +1,9 @@ { "home": { - "welcomeNight": "Good Night {username}", - "welcomeMorning": "Good Morning {username}", - "welcomeDay": "Hi {username}", - "welcomeEvening": "Good Evening {username}", + "welcomeNight": "Good Night {username}!", + "welcomeMorning": "Good Morning {username}!", + "welcomeDay": "Hi {username}!", + "welcomeEvening": "Good Evening {username}!", "lastViewed": "Last viewed", "list": { "newText": "You can create a new list for your new tasks:", diff --git a/src/views/Home.vue b/src/views/Home.vue index 5ab42bbcb..c8dcd0d3a 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -1,7 +1,7 @@