diff --git a/frontend/src/lib/components/ModelTable/ModelTable.svelte b/frontend/src/lib/components/ModelTable/ModelTable.svelte index ce4607256..fe3e78c09 100644 --- a/frontend/src/lib/components/ModelTable/ModelTable.svelte +++ b/frontend/src/lib/components/ModelTable/ModelTable.svelte @@ -14,6 +14,7 @@ import * as m from '$paraglide/messages'; import { localItems, toCamelCase } from '$lib/utils/locales'; import { languageTag } from '$paraglide/runtime'; + import { ISO_8601_REGEX } from '$lib/utils/constants'; // Event Dispatcher type TableEvent = { selected: string[]; @@ -229,6 +230,9 @@ {value.str ?? '-'} {/if} {:else if value && value.hexcolor} +

{value.name ?? value.str ?? '-'}

+ {:else if ISO_8601_REGEX.test(value)} + {new Date(value).toLocaleString(languageTag())} {#if localItems(languageTag())[toCamelCase(value.name ?? value.str)]}

{localItems(languageTag())[toCamelCase(value.name ?? value.str)]}

{:else} diff --git a/frontend/src/lib/components/SideBar/SideBarFooter.svelte b/frontend/src/lib/components/SideBar/SideBarFooter.svelte index f2d3777d5..87834085c 100644 --- a/frontend/src/lib/components/SideBar/SideBarFooter.svelte +++ b/frontend/src/lib/components/SideBar/SideBarFooter.svelte @@ -6,6 +6,7 @@ import { availableLanguageTags, languageTag, setLanguageTag } from '$paraglide/runtime'; import { LOCALE_MAP } from '$lib/utils/locales'; import * as m from '$paraglide/messages'; + import { setCookie } from '$lib/utils/cookies'; const language: any = { french: m.french(), @@ -20,7 +21,8 @@ event.preventDefault(); value = event?.target?.value; setLanguageTag(value); - sessionStorage.setItem('lang', value); + // sessionStorage.setItem('lang', value); + setCookie('lang', value); } const popupUser: PopupSettings = { diff --git a/frontend/src/lib/components/fragments/WatchlistExceptions.svelte b/frontend/src/lib/components/fragments/WatchlistExceptions.svelte index 5eed164c3..c61b8b895 100644 --- a/frontend/src/lib/components/fragments/WatchlistExceptions.svelte +++ b/frontend/src/lib/components/fragments/WatchlistExceptions.svelte @@ -3,6 +3,7 @@ import * as m from '$paraglide/messages'; import type { User } from '$lib/utils/types'; import { formatStringToDate } from '$lib/utils/helpers'; + import { languageTag } from '$paraglide/runtime'; let request_path: string | null; $: request_path = $page.route.id; @@ -66,7 +67,7 @@ {:else if acceptanceState(acceptance.expiry_date) === 'today'} {m.today()} {/if} - {formatStringToDate(acceptance.expiry_date)} + {formatStringToDate(acceptance.expiry_date,languageTag())} {/each} diff --git a/frontend/src/lib/components/fragments/WatchlistMeasures.svelte b/frontend/src/lib/components/fragments/WatchlistMeasures.svelte index b7ce1b875..9facc349e 100644 --- a/frontend/src/lib/components/fragments/WatchlistMeasures.svelte +++ b/frontend/src/lib/components/fragments/WatchlistMeasures.svelte @@ -3,6 +3,7 @@ import type { SecurityMeasureSchema } from '$lib/utils/schemas'; import * as m from '$paraglide/messages'; import { formatStringToDate } from '$lib/utils/helpers'; + import { languageTag } from '$paraglide/runtime'; let request_path: string | null; $: request_path = $page.route.id; @@ -60,7 +61,7 @@ {:else if measureState(measure.expiry_date) === 'today'} {m.today()} {/if} - {formatStringToDate(measure.expiry_date)} + {formatStringToDate(measure.expiry_date,languageTag())} {/each} diff --git a/frontend/src/lib/utils/cookies.ts b/frontend/src/lib/utils/cookies.ts new file mode 100644 index 000000000..4275b4537 --- /dev/null +++ b/frontend/src/lib/utils/cookies.ts @@ -0,0 +1,39 @@ + +function getCookieDict(): {[key: string]: string} { + let cookies = {} + document.cookie.split(";").forEach(cookie_string => { + cookie_string = cookie_string.trim(); // Remove possible whitespaces + const cookie_key = cookie_string.split("=")[0]; + const cookie_value = cookie_string.substring(cookie_key.length+1); + cookies[cookie_key] = cookie_value; + }) + return cookies; +} + +function getCookie(cookie_name: string): string | undefined { + const cookie_dict = getCookieDict(); + for (const [_cookie_name,_cookie_value] of Object.entries(cookie_dict)) { + if (_cookie_name === cookie_name) { + return _cookie_value; + } + } +} + +function setCookie(cookie_name: string,cookie_value: string,secure: boolean=true,samesite_lax: boolean=true,path: string="/") { + let cookie_info = [`${cookie_name}=${cookie_value}`]; + if (secure) cookie_info.push("Secure"); + if (samesite_lax) cookie_info.push("SameSite=Lax"); + cookie_info.push(`path=${path}`); + + document.cookie = cookie_info.join("; "); +} + +function deleteCookie(cookie_name: string) { + document.cookie = cookie_name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; +} + +export { + setCookie, + getCookie, + deleteCookie +}; diff --git a/frontend/src/lib/utils/helpers.ts b/frontend/src/lib/utils/helpers.ts index d0c9951e1..579b66381 100644 --- a/frontend/src/lib/utils/helpers.ts +++ b/frontend/src/lib/utils/helpers.ts @@ -1,6 +1,6 @@ -export function formatStringToDate(inputString: string) { +export function formatStringToDate(inputString: string,locale: string="en") { const date = new Date(inputString); - return date.toLocaleDateString('en-US', { + return date.toLocaleDateString(locale, { year: 'numeric', month: 'short', day: 'numeric' diff --git a/frontend/src/routes/(app)/[model=urlmodel]/[id=uuid]/+layout.server.ts b/frontend/src/routes/(app)/[model=urlmodel]/[id=uuid]/+layout.server.ts index adcae5b30..f9f6c60ed 100644 --- a/frontend/src/routes/(app)/[model=urlmodel]/[id=uuid]/+layout.server.ts +++ b/frontend/src/routes/(app)/[model=urlmodel]/[id=uuid]/+layout.server.ts @@ -10,16 +10,14 @@ import type { LayoutServerLoad } from './$types'; import type { SuperValidated } from 'sveltekit-superforms'; import type { urlModel } from '$lib/utils/types'; +import { languageTag } from '$paraglide/runtime'; + export const load: LayoutServerLoad = async ({ fetch, params }) => { const endpoint = `${BASE_API_URL}/${params.model}/${params.id}/`; const res = await fetch(endpoint); const data = await res.json(); - processObject(data, ISO_8601_REGEX, (matchedString: string): string => - new Date(matchedString).toLocaleString() - ); - type RelatedModel = { urlModel: urlModel; info: ModelMapEntry; diff --git a/frontend/src/routes/(app)/[model=urlmodel]/[id=uuid]/+page.svelte b/frontend/src/routes/(app)/[model=urlmodel]/[id=uuid]/+page.svelte index fc42bf41e..756daf576 100644 --- a/frontend/src/routes/(app)/[model=urlmodel]/[id=uuid]/+page.svelte +++ b/frontend/src/routes/(app)/[model=urlmodel]/[id=uuid]/+page.svelte @@ -18,6 +18,7 @@ import { localItems, toCamelCase, capitalizeFirstLetter } from '$lib/utils/locales.js'; import { languageTag } from '$paraglide/runtime.js'; import * as m from '$paraglide/messages.js'; + import { ISO_8601_REGEX } from '$lib/utils/constants'; const modalStore: ModalStore = getModalStore(); const toastStore: ToastStore = getToastStore(); @@ -216,6 +217,8 @@ {value.str} {:else if isURL(value)} {value} + {:else if ISO_8601_REGEX.test(value)} + {new Date(value).toLocaleString(languageTag())} {:else} {#if localItems(languageTag())[toCamelCase((value.str || value.name) ?? value)]} {localItems(languageTag())[toCamelCase((value.str || value.name) ?? value)]} diff --git a/frontend/src/routes/(app)/my-profile/+page.svelte b/frontend/src/routes/(app)/my-profile/+page.svelte index 3754f9c2e..6de7ba460 100644 --- a/frontend/src/routes/(app)/my-profile/+page.svelte +++ b/frontend/src/routes/(app)/my-profile/+page.svelte @@ -13,7 +13,7 @@ sortedKeys.forEach((key) => { if (!filter.includes(key) && Object.prototype.hasOwnProperty.call($page.data.user, key)) { const str = toCamelCase(key); - if (key === 'date_joined') filtered[str] = new Date($page.data.user[key]).toLocaleString(); + if (key === 'date_joined') filtered[str] = new Date($page.data.user[key]).toLocaleString(languageTag()); else filtered[str] = $page.data.user[key]; } }); diff --git a/frontend/src/routes/(app)/risk-assessments/[id=uuid]/+page.svelte b/frontend/src/routes/(app)/risk-assessments/[id=uuid]/+page.svelte index 948844bc5..24ce08c3a 100644 --- a/frontend/src/routes/(app)/risk-assessments/[id=uuid]/+page.svelte +++ b/frontend/src/routes/(app)/risk-assessments/[id=uuid]/+page.svelte @@ -18,6 +18,7 @@ import RiskScenarioItem from '$lib/components/RiskMatrix/RiskScenarioItem.svelte'; import * as m from '$paraglide/messages'; + import { languageTag } from '$paraglide/runtime'; export let data; const showRisks = true; @@ -221,11 +222,11 @@
  • {m.createdAt()}: - {new Date(risk_assessment.created_at).toLocaleString()} + {new Date(risk_assessment.created_at).toLocaleString(languageTag())}
  • {m.updatedAt()}: - {new Date(risk_assessment.updated_at).toLocaleString()} + {new Date(risk_assessment.updated_at).toLocaleString(languageTag())}
  • diff --git a/frontend/src/routes/(app)/risk-matrices/[id=uuid]/+layout.server.ts b/frontend/src/routes/(app)/risk-matrices/[id=uuid]/+layout.server.ts index 101035381..f50cbf62b 100644 --- a/frontend/src/routes/(app)/risk-matrices/[id=uuid]/+layout.server.ts +++ b/frontend/src/routes/(app)/risk-matrices/[id=uuid]/+layout.server.ts @@ -16,10 +16,6 @@ export const load: LayoutServerLoad = async ({ fetch, params }) => { const res = await fetch(endpoint); const data = await res.json(); - processObject(data, ISO_8601_REGEX, (matchedString: string): string => - new Date(matchedString).toLocaleString() - ); - const model = getModelInfo(URLModel); const relatedModels: Record = {}; diff --git a/frontend/src/routes/(app)/risk-scenarios/[id=uuid]/edit/+page.svelte b/frontend/src/routes/(app)/risk-scenarios/[id=uuid]/edit/+page.svelte index cc48d647c..3b09e460c 100644 --- a/frontend/src/routes/(app)/risk-scenarios/[id=uuid]/edit/+page.svelte +++ b/frontend/src/routes/(app)/risk-scenarios/[id=uuid]/edit/+page.svelte @@ -165,7 +165,7 @@

    {m.lastUpdate()}

    - {new Date(data.scenario.updated_at).toLocaleString()} + {new Date(data.scenario.updated_at).toLocaleString(languageTag())}