diff --git a/src/routes/cv/+page.svelte b/src/routes/cv/+page.svelte index 930b76f..2737aa6 100644 --- a/src/routes/cv/+page.svelte +++ b/src/routes/cv/+page.svelte @@ -32,20 +32,36 @@ // Print detection setup import onMount from "@e/onMount"; + import { goto } from "$app/navigation"; let debug = false; onMount(() => { - // Check if the query parameter exists in the URL const urlParams = new URLSearchParams(window.location.search); - const hideOnPrintParam = urlParams.get("hideOnPrint"); - const locale = urlParams.get("locale"); + let needsUrlUpdate = false; - // If the query parameter is not detected, reload the page with the parameter added - if (!hideOnPrintParam) { - window.location.href = `${window.location.href}${window.location.href.indexOf("?") > 0 ? "&" : "?"}hideOnPrint=1`; + // 1. Safely handle hideOnPrint without reloading + if (!urlParams.has("hideOnPrint")) { + urlParams.set("hideOnPrint", "1"); + needsUrlUpdate = true; } - if (locale && locale != getLocale()) { - setLocale(locale); + // 2. Check for locale in URL, fallback to localStorage + const urlLocale = urlParams.get("locale"); + const savedLocale = localStorage.getItem("cv_locale"); + + const targetLocale = urlLocale || savedLocale; + + if (targetLocale && targetLocale !== getLocale()) { + setLocale(targetLocale as any); // Update Paraglide + if (!urlLocale) { + urlParams.set("locale", targetLocale); + needsUrlUpdate = true; + } + } + + // 3. Update the URL silently if we changed anything + if (needsUrlUpdate) { + const newUrl = `${window.location.pathname}?${urlParams.toString()}`; + window.history.replaceState({}, "", newUrl); } const params = new URLSearchParams(window.location.search); @@ -54,6 +70,14 @@ loadMotivation(); }); + function changeLanguage(tag: string) { + setLocale(tag as any); + localStorage.setItem("cv_locale", tag); + const url = new URL(window.location.href); + url.searchParams.set("locale", tag); + window.location.href = url.toString(); + } + let motivationInput: HTMLTextAreaElement; let motivation: string | null = ""; function loadMotivation() { @@ -115,7 +139,8 @@ ? 'btn-filled btn-primary text-primary-content' : 'btn-outline'}" onclick={() => { - setLocale(tag); + changeLanguage(tag); + // window.location.reload(); }} > {tag.toUpperCase()}