From a911fcc2202b22d2ccd3a5b8b386cd3f8143faae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89mile=20R=C3=A9?= Date: Wed, 6 May 2026 15:32:38 +0400 Subject: [PATCH] Add seconds duration translations to cookie banner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Émile Ré --- packages/cookie-banner/src/cookie-utils.ts | 15 ++++++-- packages/helpers/src/duration.ts | 44 +++++++++++++++------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/packages/cookie-banner/src/cookie-utils.ts b/packages/cookie-banner/src/cookie-utils.ts index 5a1ab0d63..9bab3e496 100644 --- a/packages/cookie-banner/src/cookie-utils.ts +++ b/packages/cookie-banner/src/cookie-utils.ts @@ -32,6 +32,8 @@ const durationTextsByLanguage: Record = { duration_hour_other: "{{count}} hours", duration_minute_one: "{{count}} minute", duration_minute_other: "{{count}} minutes", + duration_second_one: "{{count}} second", + duration_second_other: "{{count}} seconds", duration_session: "session", }, fr: { @@ -47,6 +49,8 @@ const durationTextsByLanguage: Record = { duration_hour_other: "{{count}} heures", duration_minute_one: "{{count}} minute", duration_minute_other: "{{count}} minutes", + duration_second_one: "{{count}} seconde", + duration_second_other: "{{count}} secondes", duration_session: "session", }, de: { @@ -62,6 +66,8 @@ const durationTextsByLanguage: Record = { duration_hour_other: "{{count}} Stunden", duration_minute_one: "{{count}} Minute", duration_minute_other: "{{count}} Minuten", + duration_second_one: "{{count}} Sekunde", + duration_second_other: "{{count}} Sekunden", duration_session: "Sitzung", }, es: { @@ -77,6 +83,8 @@ const durationTextsByLanguage: Record = { duration_hour_other: "{{count}} horas", duration_minute_one: "{{count}} minuto", duration_minute_other: "{{count}} minutos", + duration_second_one: "{{count}} segundo", + duration_second_other: "{{count}} segundos", duration_session: "sesión", }, }; @@ -93,9 +101,10 @@ const DURATION_UNITS: [number, string, number][] = [ [365 * 24 * 3600, "duration_year", 21 * 24 * 3600], [30 * 24 * 3600, "duration_month", 2 * 24 * 3600], [7 * 24 * 3600, "duration_week", 12 * 3600], - [24 * 3600, "duration_day", 0], + [24 * 3600, "duration_day", 2 * 3600], [3600, "duration_hour", 5 * 60], - [60, "duration_minute", 15], + [60, "duration_minute", 5], + [1, "duration_second", 0], ]; export function humanizeDuration(seconds: number, lang?: string): string { @@ -106,7 +115,7 @@ export function humanizeDuration(seconds: number, lang?: string): string { const parts: string[] = []; for (const [unit, key, snap] of DURATION_UNITS) { - if (remaining >= unit) { + if (remaining >= unit - snap) { let count = Math.floor(remaining / unit); const leftover = remaining - count * unit; diff --git a/packages/helpers/src/duration.ts b/packages/helpers/src/duration.ts index 9c7c46648..24ae14b29 100644 --- a/packages/helpers/src/duration.ts +++ b/packages/helpers/src/duration.ts @@ -12,25 +12,41 @@ // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -export const DURATION_UNITS: { value: string; label: string; singular: string; seconds: number }[] = [ - { value: "seconds", label: "seconds", singular: "second", seconds: 1 }, - { value: "minutes", label: "minutes", singular: "minute", seconds: 60 }, - { value: "hours", label: "hours", singular: "hour", seconds: 3600 }, - { value: "days", label: "days", singular: "day", seconds: 86400 }, - { value: "weeks", label: "weeks", singular: "week", seconds: 604800 }, - { value: "months", label: "months", singular: "month", seconds: 2592000 }, - { value: "years", label: "years", singular: "year", seconds: 31536000 }, -]; +export const DURATION_UNITS: { value: string; label: string; singular: string; seconds: number, snap: number }[] = [ + { value: "seconds", label: "seconds", singular: "second", seconds: 1, snap: 0 }, + { value: "minutes", label: "minutes", singular: "minute", seconds: 60, snap: 5 }, + { value: "hours", label: "hours", singular: "hour", seconds: 3600, snap: 5 * 60 }, + { value: "days", label: "days", singular: "day", seconds: 86400, snap: 2 * 3600 }, + { value: "weeks", label: "weeks", singular: "week", seconds: 604800, snap: 12 * 3600 }, + { value: "months", label: "months", singular: "month", seconds: 2592000, snap: 2 * 24 * 3600 }, + { value: "years", label: "years", singular: "year", seconds: 31536000, snap: 21 * 24 * 3600 }, +] as const; export function humanizeSeconds(seconds: number | null): string { if (seconds === null || seconds <= 0) return "session"; - for (const u of [...DURATION_UNITS].reverse()) { - if (seconds >= u.seconds && seconds % u.seconds === 0) { - const count = seconds / u.seconds; - return `${count} ${count === 1 ? u.singular : u.label}`; + + let remaining = seconds; + const parts: string[] = []; + + for (const {label, singular, seconds: durationInSeconds, snap} of [...DURATION_UNITS].reverse()) { + if (remaining >= durationInSeconds - snap) { + let count = Math.floor(remaining / durationInSeconds); + const leftover = remaining - count * durationInSeconds; + + if (leftover >= durationInSeconds - snap) { + count++; + remaining = 0; + } else if (leftover <= snap) { + remaining = 0; + } else { + remaining = leftover; + } + + parts.push(`${count} ${count === 1 ? singular : label}`); } } - return `${seconds} ${seconds === 1 ? "second" : "seconds"}`; + + return parts.length > 0 ? parts.join(", ") : "session"; } export function toMaxAgeSeconds(value: string, unit: string): number | null {