diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-10-02 11:35:06 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-10-02 11:35:06 +0200 |
| commit | 70347722fa1f959a5b224ed43b6b64bf289f36cf (patch) | |
| tree | eb13a0cb69788191dc81732baa824c6adc3c6c50 /apps/kit/src/lib/components/locale-switcher.svelte | |
| parent | d953fc6decd8365dd054a971af6a4cf25b9439f0 (diff) | |
| download | greatoffice-70347722fa1f959a5b224ed43b6b64bf289f36cf.tar.xz greatoffice-70347722fa1f959a5b224ed43b6b64bf289f36cf.zip | |
feat/refactor: Initial surroundings for (app) and normalise icon names
Diffstat (limited to 'apps/kit/src/lib/components/locale-switcher.svelte')
| -rw-r--r-- | apps/kit/src/lib/components/locale-switcher.svelte | 45 |
1 files changed, 13 insertions, 32 deletions
diff --git a/apps/kit/src/lib/components/locale-switcher.svelte b/apps/kit/src/lib/components/locale-switcher.svelte index 39d6168..1f9eb37 100644 --- a/apps/kit/src/lib/components/locale-switcher.svelte +++ b/apps/kit/src/lib/components/locale-switcher.svelte @@ -1,51 +1,32 @@ <script lang="ts"> - import {browser} from "$app/environment"; - import {page} from "$app/stores"; - import {setLocale, locale} from "$lib/i18n/i18n-svelte"; - import type {Locales} from "$lib/i18n/i18n-types"; - import {locales} from "$lib/i18n/i18n-util"; - import {loadLocaleAsync} from "$lib/i18n/i18n-util.async"; + import { browser } from "$app/environment"; + import { page } from "$app/stores"; + import { setLocale, locale } from "$lib/i18n/i18n-svelte"; + import type { Locales } from "$lib/i18n/i18n-types"; + import { locales } from "$lib/i18n/i18n-util"; + import { loadLocaleAsync } from "$lib/i18n/i18n-util.async"; - const switchLocale = async ( - newLocale: Locales, - updateHistoryState = true, - ) => { + const switchLocale = async (newLocale: Locales) => { 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); - - //TODO set cookie that persists the locale }; - // 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); + switchLocale($page.params.lang as Locales); } </script> -<svelte:window on:popstate={handlePopStateEvent}/> - <ul> - {#each locales as l} + {#each locales as aLocale} <li> <button - type="button" - class:active={l === $locale} - on:click={() => switchLocale(l)} + type="button" + class:active={aLocale === $locale} + on:click={() => switchLocale(aLocale)} > - {l} + {aLocale} </button> </li> {/each} |
