Add seconds duration translations to cookie banner

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-05-06 15:32:38 +04:00
parent bd8ec12876
commit a911fcc220
2 changed files with 42 additions and 17 deletions

View File

@@ -32,6 +32,8 @@ const durationTextsByLanguage: Record<string, DurationTexts> = {
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<string, DurationTexts> = {
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<string, DurationTexts> = {
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<string, DurationTexts> = {
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;

View File

@@ -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 {