From a8b44b09a404aee477e735501b828d1b14aad311 Mon Sep 17 00:00:00 2001
From: ivarlovlie <git@ivarlovlie.no>
Date: Tue, 7 Jun 2022 01:33:52 +0200
Subject: feat: Add inital translation support

---
 apps/projects/src/app/index.svelte | 123 +++++++++++++++++++++++--------------
 1 file changed, 78 insertions(+), 45 deletions(-)

(limited to 'apps/projects/src/app/index.svelte')

diff --git a/apps/projects/src/app/index.svelte b/apps/projects/src/app/index.svelte
index 77d290d..e397de3 100644
--- a/apps/projects/src/app/index.svelte
+++ b/apps/projects/src/app/index.svelte
@@ -1,53 +1,86 @@
 <svelte:options immutable={true}/>
 <svelte:window bind:online={online}/>
 
-<script>
-	import {logout_user} from "$app/lib/services/user-service";
-	import Router from "svelte-spa-router";
-	import {wrap} from "svelte-spa-router/wrap";
-	import {QueryClient, QueryClientProvider} from "@sveltestack/svelte-query";
-	import {is_active} from "$shared/lib/session";
-	import UiWorkbench from "$app/pages/ui-workbench.svelte";
-	import NotFound from "$app/pages/not-found.svelte";
-	import Home from "$app/pages/home.svelte";
-	import Settings from "$app/pages/settings.svelte";
-	import Data from "$app/pages/data.svelte";
-	import PreHeader from "$shared/components/pre-header.svelte";
-
-	let online = true;
-
-	async function user_is_logged_in() {
-		if (!await is_active()) {
-			await logout_user("expired");
-		}
-		return true;
-	}
-
-	const queryClient = new QueryClient();
-	
-	const routes = {
-		"/home": wrap({
-			component: Home,
-			conditions: [user_is_logged_in],
-		}),
-		"/": wrap({
-			component: Home,
-			conditions: [user_is_logged_in],
-		}),
-		"/settings": wrap({
-			component: Settings,
-			conditions: [user_is_logged_in],
-		}),
-		"/data": wrap({
-			component: Data,
-			conditions: [user_is_logged_in],
-		}),
-		"/ui-workbench": UiWorkbench,
-		"*": NotFound,
-	};
+<script lang="ts">
+    import {logout_user} from "$app/lib/services/user-service";
+    import {currentLocale, preffered_or_default} from "$app/lib/stores/locale";
+    import {CookieNames} from "$shared/lib/configuration";
+    import {get_cookie} from "$shared/lib/helpers";
+    import {Temporal} from "@js-temporal/polyfill";
+    import {onMount} from "svelte";
+    import Router from "svelte-spa-router";
+    import {wrap} from "svelte-spa-router/wrap";
+    import {QueryClient, QueryClientProvider} from "@sveltestack/svelte-query";
+    import {is_active} from "$shared/lib/session";
+    import UiWorkbench from "$app/pages/ui-workbench.svelte";
+    import NotFound from "$app/pages/not-found.svelte";
+    import Home from "$app/pages/home.svelte";
+    import Settings from "$app/pages/settings.svelte";
+    import Data from "$app/pages/data.svelte";
+    import PreHeader from "$shared/components/pre-header.svelte";
+    import {setLocale} from "$app/lib/i18n/i18n-svelte";
+    import {loadLocaleAsync} from "$app/lib/i18n/i18n-util.async";
+    import {i18nObject} from "$app/lib/i18n/i18n-util";
+
+    let online = true;
+    let notOnlineText;
+    let LL;
+
+    console.log("Projects Startup Report", {
+        prefferedLocale: navigator.language,
+        timeZone: Temporal.Now.timeZone().id,
+        go_theme: get_cookie(CookieNames.theme),
+        go_locale: get_cookie(CookieNames.locale),
+        prefersColorScheme: window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"
+    });
+
+    currentLocale.subscribe(async locale => {
+        locale = locale === "preffered" ? preffered_or_default() : locale;
+        await loadLocaleAsync(locale);
+        LL = i18nObject(locale);
+        setLocale(locale);
+    });
+
+    onMount(async () => {
+        const locale = $currentLocale === "preffered" ? preffered_or_default() : $currentLocale;
+        await loadLocaleAsync(locale);
+        LL = i18nObject(locale);
+        setLocale(locale);
+        notOnlineText = LL.messages.noInternet();
+    });
+
+    async function user_is_logged_in() {
+        if (!await is_active()) {
+            await logout_user("expired");
+        }
+        return true;
+    }
+
+    const queryClient = new QueryClient();
+
+    const routes = {
+        "/home": wrap({
+            component: Home,
+            conditions: [user_is_logged_in],
+        }),
+        "/": wrap({
+            component: Home,
+            conditions: [user_is_logged_in],
+        }),
+        "/settings": wrap({
+            component: Settings,
+            conditions: [user_is_logged_in],
+        }),
+        "/data": wrap({
+            component: Data,
+            conditions: [user_is_logged_in],
+        }),
+        "/ui-workbench": UiWorkbench,
+        "*": NotFound,
+    };
 </script>
 
-<PreHeader show="{!online}">You seem to be offline, please check your internet connection.</PreHeader>
+<PreHeader show="{!online}">{notOnlineText}</PreHeader>
 
 <QueryClientProvider client={queryClient}>
     <Router
-- 
cgit v1.3

