From 0eb14c3a0da93bce510e9b6721a66cf05517ae9a Mon Sep 17 00:00:00 2001
From: ivarlovlie <git@ivarlovlie.no>
Date: Tue, 21 Feb 2023 23:53:39 +0100
Subject: feat: Force page reload when changing locale

---
 src/components/locale-switcher.svelte | 36 +++--------------------------------
 1 file changed, 3 insertions(+), 33 deletions(-)

(limited to 'src/components/locale-switcher.svelte')

diff --git a/src/components/locale-switcher.svelte b/src/components/locale-switcher.svelte
index aa4e0c4..6201d06 100644
--- a/src/components/locale-switcher.svelte
+++ b/src/components/locale-switcher.svelte
@@ -1,42 +1,12 @@
 <script lang="ts">
-	import { browser } from "$app/environment";
-	import { invalidateAll } from "$app/navigation";
 	import { page } from "$app/stores";
-	import { setLocale, locale } from "$i18n/i18n-svelte";
-	import type { Locales } from "$i18n/i18n-types";
-	import { locales } from "$i18n/i18n-util";
-	import { loadLocaleAsync } from "$i18n/i18n-util.async";
+	import { locale } from "$i18n/i18n-svelte";
 	import { replaceLocaleInUrl } from "$lib/utils";
 
-	const switchLocale = async (newLocale: Locales, updateHistoryState = true) => {
-		if (!newLocale || $locale === newLocale) return;
-		// load new dictionary from server
-		await loadLocaleAsync(newLocale);
-		// select locale
-		setLocale(newLocale);
-		// update `lang` attribute
-		document.querySelector("html")?.setAttribute("lang", newLocale);
-		if (updateHistoryState) {
-			// update url to reflect locale changes
-			history.pushState({ locale: newLocale }, "", replaceLocaleInUrl($page.url, newLocale));
-		}
-		// run the `load` function again
-		invalidateAll();
-	};
-	// update locale when navigating via browser back/forward buttons
-	const handlePopStateEvent = async ({ state }: PopStateEvent) => switchLocale(state.locale, false);
-	// update locale when page store changes
-	$: if (browser) {
-		const lang = $page.params.lang as Locales;
-		switchLocale(lang, false);
-		history.replaceState({ ...history.state, locale: lang }, "", replaceLocaleInUrl($page.url, lang));
-	}
 </script>
 
-<svelte:window on:popstate={handlePopStateEvent} />
-
 {#if $locale == "en"}
-	<a href={replaceLocaleInUrl($page.url, "nb")}>Norsk Bokmål</a>
+	<a href={replaceLocaleInUrl($page.url, "nb")} data-sveltekit-reload>Norsk Bokmål</a>
 {:else if $locale == "nb"}
-	<a href={replaceLocaleInUrl($page.url, "en")}>English</a>
+	<a href={replaceLocaleInUrl($page.url, "en")} data-sveltekit-reload>English</a>
 {/if}
-- 
cgit v1.3

