From f42a68dfc904125588705d7fefa1482d83b9f487 Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Thu, 5 Sep 2024 21:01:26 +0200 Subject: [PATCH] Added date computing translations --- i18n.config.ts | 16 +++++++++++++++ stores/CalendarStore.ts | 45 ++++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/i18n.config.ts b/i18n.config.ts index c0ee626..f8da943 100644 --- a/i18n.config.ts +++ b/i18n.config.ts @@ -66,6 +66,14 @@ export default defineI18nConfig(() => ({ beforeYesterday: "2 days ago", fromTo: "From {startDate} to {endDate}", while: "During {duration}", + xDaysNext: "In {days} days", + xMonthsNext: "In {months} month(s)", + xYearsNext: "In {years} year(s)", + xYearsAndMonthsNext: "In {years} year(s) and {months} month(s)", + xDaysAgo: "{days} days ago", + xMonthsAgo: "{months} month(s) ago", + xYearsAgo: "{years} year(s) ago", + xYearsAndMonthsAgo: "{years} year(s) and {months} month(s) ago", }, event: { nameSingular: "Event", @@ -210,6 +218,14 @@ export default defineI18nConfig(() => ({ beforeYesterday: "Avant-hier", fromTo: "Du {startDate} au {endDate}", while: "Pendant {duration}", + xDaysNext: "Dans {days} jours", + xMonthsNext: "Dans {months} mois", + xYearsNext: "Dans {years} an(s)", + xYearsAndMonthsNext: "Dans {years} an(s) et {months} mois", + xDaysAgo: "Il y a {days} jours", + xMonthsAgo: "Il y a {months} mois", + xYearsAgo: "Il y a {years} an(s)", + xYearsAndMonthsAgo: "Il y a {years} an(s) et {months} mois", }, event: { nameSingular: "Évènement", diff --git a/stores/CalendarStore.ts b/stores/CalendarStore.ts index 9f99234..c575d1c 100644 --- a/stores/CalendarStore.ts +++ b/stores/CalendarStore.ts @@ -24,6 +24,13 @@ type CalendarCurrentDate = { currentDateTitle: ComputedRef } +type DateDirectionTranslationKeys = { + days: string + months: string + years: string + yearsAndMonths: string +} + export const useCalendar = defineStore("calendar", () => { const { t } = useI18n() @@ -483,7 +490,19 @@ export const useCalendar = defineStore("calendar", () => { const differenceInDays: number = getDifferenceInDays(baseDate, relativeDate) let output: string = "" let direction: "past" | "present" | "future" = "present" - let directionPrefix: string = "" + const futureKeys: DateDirectionTranslationKeys = { + days: "xDaysNext", + months: "xMonthsNext", + years: "xYearsNext", + yearsAndMonths: "xYearsAndMonthsNext", + } + const pastKeys: DateDirectionTranslationKeys = { + days: "xDaysAgo", + months: "xMonthsAgo", + years: "xYearsAgo", + yearsAndMonths: "xYearsAndMonthsAgo", + } + let directionKeys: DateDirectionTranslationKeys = pastKeys // Check whether it's a past or future date if (differenceInDays > 0) { @@ -493,7 +512,7 @@ export const useCalendar = defineStore("calendar", () => { } if (formatting === "complex") { - // Handle if it's the same date + // Handle if it's the same date if (direction === "present") { return t("entity.calendar.date.today") } @@ -512,12 +531,10 @@ export const useCalendar = defineStore("calendar", () => { // Get relevant prefix for the string if (direction === "future") { - directionPrefix = "Dans " - } else if (direction === "past") { - directionPrefix = "Il y a " + directionKeys = futureKeys + } else { + directionKeys = pastKeys } - - output += directionPrefix } const isSameMonth = baseDate.month === relativeDate.month @@ -546,11 +563,7 @@ export const useCalendar = defineStore("calendar", () => { if (isSameMonth && isSameYear) { dateAcc.day = futureDate.day - datePivot.day - if (dateAcc.day === 1) { - output += ` ${dateAcc.day} jour` - } else { - output += ` ${dateAcc.day} jours` - } + output = t(`entity.calendar.date.${directionKeys.days}`, { days: dateAcc.day }) return output } @@ -559,7 +572,7 @@ export const useCalendar = defineStore("calendar", () => { else if (isSameYear) { dateAcc.month = futureDate.month - datePivot.month - output += ` ${dateAcc.month} mois` + output = t(`entity.calendar.date.${directionKeys.months}`, { months: dateAcc.month }) return output } @@ -602,13 +615,13 @@ export const useCalendar = defineStore("calendar", () => { const remainderMonths = dateAcc.month % monthsPerYear.value if (computedYear >= 1 && remainderMonths) { - output += ` ${computedYear} an(s) et ${remainderMonths} mois` + output = t(`entity.calendar.date.${directionKeys.yearsAndMonths}`, { years: computedYear, months: remainderMonths }) } else if (computedYear >= 1 && !remainderMonths) { - output += ` ${computedYear} an(s)`; + output = t(`entity.calendar.date.${directionKeys.years}`, { years: computedYear }) } else { - output += ` ${remainderMonths} mois` + output = t(`entity.calendar.date.${directionKeys.months}`, { months: remainderMonths }) } return output