FIX des bugs du merge request 884

This commit is contained in:
Boris Waaub
2025-09-30 13:49:04 +00:00
committed by Julien Fastré
parent 056e2dcc5f
commit b43aeebc3c
15 changed files with 127 additions and 81 deletions

View File

@@ -1,4 +1,5 @@
import { TranslatableString } from "ChillMainAssets/types";
import { DateTime, TranslatableString } from "ChillMainAssets/types";
import { getLocale } from "translator";
/**
* Localizes a translatable string object based on the current locale.
@@ -17,11 +18,10 @@ import { TranslatableString } from "ChillMainAssets/types";
* @returns The localized URL
*/
export function localizedUrl(url: string): string {
const lang =
document.documentElement.lang || navigator.language.split("-")[0] || "fr";
const locale = getLocale();
// Ensure url starts with a slash and does not already start with /{lang}/
const normalizedUrl = url.startsWith("/") ? url : `/${url}`;
const langPrefix = `/${lang}`;
const langPrefix = `/${locale}`;
if (normalizedUrl.startsWith(langPrefix + "/")) {
return normalizedUrl;
}
@@ -36,7 +36,7 @@ export function localizeString(
return "";
}
const currentLocale = locale || navigator.language.split("-")[0] || "fr";
const currentLocale = locale || getLocale();
if (translatableString[currentLocale]) {
return translatableString[currentLocale];
@@ -59,3 +59,47 @@ export function localizeString(
return "";
}
const datetimeFormats: Record<
string,
Record<string, Intl.DateTimeFormatOptions>
> = {
fr: {
short: {
year: "numeric",
month: "numeric",
day: "numeric",
},
text: {
year: "numeric",
month: "long",
day: "numeric",
},
long: {
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: false,
},
hoursOnly: {
hour: "numeric",
minute: "numeric",
hour12: false,
},
},
};
export function localizeDateTimeFormat(
dateTime: DateTime,
format: keyof typeof datetimeFormats.fr = "short",
): string {
const locale = getLocale();
const options =
datetimeFormats[locale]?.[format] || datetimeFormats.fr[format];
return new Intl.DateTimeFormat(locale, options).format(
new Date(dateTime.datetime),
);
}
export default datetimeFormats;