From 38a07e3dfbda798010cc7f219abec911f747eaf7 Mon Sep 17 00:00:00 2001
From: ivarlovlie <git@ivarlovlie.no>
Date: Mon, 3 Oct 2022 16:45:26 +0800
Subject: feat: Fully functioning i18n

---
 apps/kit/src/lib/components/locale-switcher.svelte | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

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

diff --git a/apps/kit/src/lib/components/locale-switcher.svelte b/apps/kit/src/lib/components/locale-switcher.svelte
index 1f9eb37..477e2d7 100644
--- a/apps/kit/src/lib/components/locale-switcher.svelte
+++ b/apps/kit/src/lib/components/locale-switcher.svelte
@@ -1,16 +1,25 @@
 <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) {
@@ -21,11 +30,7 @@
 <ul>
     {#each locales as aLocale}
         <li>
-            <button
-                type="button"
-                class:active={aLocale === $locale}
-                on:click={() => switchLocale(aLocale)}
-            >
+            <button type="button" class:active={aLocale === $locale} on:click={() => switchLocale(aLocale)}>
                 {aLocale}
             </button>
         </li>
-- 
cgit v1.3

