aboutsummaryrefslogtreecommitdiffstats
path: root/code/frontend/src/components/locale-switcher.svelte
blob: fc03f396e8fc5361a8bfa2519cde46996f19c385 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<script lang="ts">
    import pwKey from "$actions/pwKey";
    import {browser} from "$app/environment";
    import {page} from "$app/stores";
    import {CookieNames} from "$configuration";
    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 Cookies from "js-cookie";

    export let _pwKey: string | undefined = undefined;
    export let tabindex: number | undefined = undefined;
    let currentLocale = Cookies.get(CookieNames.locale);

    async function switch_locale(newLocale: Locales) {
        if (!newLocale || $locale === newLocale) return;
        await loadLocaleAsync(newLocale);
        setLocale(newLocale);
        document.querySelector("html")?.setAttribute("lang", newLocale);
        Cookies.set(CookieNames.locale, newLocale);
        currentLocale = newLocale;
        console.log("Switched to: " + newLocale);
    }

    function on_change(event: Event) {
        const target = event.target as HTMLSelectElement;
        switch_locale(target.options[target.selectedIndex].value as Locales);
    }

    $: if (browser) {
        switch_locale($page.params.lang as Locales);
    }

    function get_locale_name(iso: string) {
        switch (iso) {
            case "nb": {
                return "Norsk Bokmål";
            }
            case "en": {
                return "English";
            }
        }
    }
</script>

<select
        {tabindex}
        use:pwKey={_pwKey}
        on:change={on_change}
        class="mt-1 mr-1 block border-none py-2 pl-3 pr-10 text-base rounded-md right-0 absolute focus:outline-none focus:ring-teal-500 sm:text-sm"
>
    {#each locales as aLocale}
        <option value={aLocale} selected={aLocale === currentLocale}>{get_locale_name(aLocale)}</option>
    {/each}
</select>