aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/locale-switcher.svelte
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2023-02-21 23:53:39 +0100
committerivarlovlie <git@ivarlovlie.no>2023-02-21 23:53:39 +0100
commit0eb14c3a0da93bce510e9b6721a66cf05517ae9a (patch)
treef7a03eca2818b0ed67ae672e9509c66225434687 /src/components/locale-switcher.svelte
parent6cb399e7267ae78e3e498bdbf5f51678ffb2cd45 (diff)
downloadauroraklinikken.no-0eb14c3a0da93bce510e9b6721a66cf05517ae9a.tar.xz
auroraklinikken.no-0eb14c3a0da93bce510e9b6721a66cf05517ae9a.zip
feat: Force page reload when changing locale
Diffstat (limited to 'src/components/locale-switcher.svelte')
-rw-r--r--src/components/locale-switcher.svelte36
1 files changed, 3 insertions, 33 deletions
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}