blob: 477e2d706db1049050958fad4bd0649dbce70736 (
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
|
<script lang="ts">
import { browser } from "$app/environment";
import { page } from "$app/stores";
import { CookieNames } from "$lib/configuration";
import { set_cookie } from "$lib/helpers";
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 Cookies from "js-cookie";
const switchLocale = async (newLocale: Locales) => {
if (!newLocale || $locale === newLocale) return;
await loadLocaleAsync(newLocale);
setLocale(newLocale);
document.querySelector("html")?.setAttribute("lang", newLocale);
set_cookie(CookieNames.locale, newLocale);
Cookies.set(CookieNames.locale, newLocale, {
sameSite: "strict",
domain: location.hostname,
});
console.log("Switched to: " + newLocale);
};
$: if (browser) {
switchLocale($page.params.lang as Locales);
}
</script>
<ul>
{#each locales as aLocale}
<li>
<button type="button" class:active={aLocale === $locale} on:click={() => switchLocale(aLocale)}>
{aLocale}
</button>
</li>
{/each}
</ul>
|