From 9a09b5d7448b32af7bc9f7972d1ef61c631ef436 Mon Sep 17 00:00:00 2001
From: ivarlovlie
Date: Tue, 20 Sep 2022 16:23:51 +0800
Subject: feat: Loading locales now work, needs a bit of a polish
---
apps/kit/src/lib/locale.ts | 9 ++++++++-
apps/kit/src/routes/(public)/+layout.svelte | 3 ---
apps/kit/src/routes/(public)/login/+page.svelte | 11 +++++++----
apps/kit/src/routes/+layout.svelte | 7 +++++++
apps/kit/src/routes/+layout.ts | 9 +++++++++
apps/kit/src/routes/+page.svelte | 1 +
6 files changed, 32 insertions(+), 8 deletions(-)
create mode 100644 apps/kit/src/routes/+layout.ts
create mode 100644 apps/kit/src/routes/+page.svelte
diff --git a/apps/kit/src/lib/locale.ts b/apps/kit/src/lib/locale.ts
index 002f874..e1fb6d6 100644
--- a/apps/kit/src/lib/locale.ts
+++ b/apps/kit/src/lib/locale.ts
@@ -1,3 +1,5 @@
+import {setLocale} from "src/lib/i18n/i18n-svelte";
+import {loadLocaleAsync} from "src/lib/i18n/i18n-util.async";
import {writable} from "svelte/store";
import {base_domain, CookieNames} from "./configuration";
import {get_cookie, set_cookie} from "./helpers";
@@ -12,9 +14,14 @@ export function preffered_or_default() {
return "en";
}
-type Locales = "en"|"nb";
+type Locales = "en" | "nb";
export const currentLocale = writable((get_cookie(CookieNames.locale) === "preffered" ? preffered_or_default() : get_cookie(CookieNames.locale) ?? preffered_or_default()) as Locales);
currentLocale.subscribe(locale => {
// @ts-ignore
set_cookie(CookieNames.locale, locale, base_domain());
});
+
+export async function load_and_set_locale(locale: Locales) {
+ await loadLocaleAsync(locale);
+ setLocale(locale);
+}
diff --git a/apps/kit/src/routes/(public)/+layout.svelte b/apps/kit/src/routes/(public)/+layout.svelte
index 1301e50..84cd442 100644
--- a/apps/kit/src/routes/(public)/+layout.svelte
+++ b/apps/kit/src/routes/(public)/+layout.svelte
@@ -1,6 +1,3 @@
\ No newline at end of file
diff --git a/apps/kit/src/routes/(public)/login/+page.svelte b/apps/kit/src/routes/(public)/login/+page.svelte
index 800575e..36230e3 100644
--- a/apps/kit/src/routes/(public)/login/+page.svelte
+++ b/apps/kit/src/routes/(public)/login/+page.svelte
@@ -2,23 +2,26 @@
import {goto} from "$app/navigation";
import {login} from "$lib/api/user";
import LL from "$lib/i18n/i18n-svelte";
+ import {setLocale} from "$lib/i18n/i18n-svelte";
+ import {loadLocaleAsync} from "$lib/i18n/i18n-util.async";
import type {ErrorResult} from "$lib/models/ErrorResult";
import type {LoginPayload} from "$lib/models/LoginPayload";
const data = {
username: "",
- password: ""
+ password: "",
} as LoginPayload;
+
let error = {
text: "",
- title: ""
+ title: "",
} as ErrorResult;
async function submitFormAsync() {
error = {text: "", title: ""};
const loginResponse = await login(data);
if (loginResponse.ok) {
- await goto("/home")
+ await goto("/home");
} else {
error.title = loginResponse.data.title;
error.text = loginResponse.data.text;
@@ -34,7 +37,7 @@
class="font-medium text-indigo-600 hover:text-indigo-500">{$LL.login.createANewAccount()}
-
+
{#if error.text || error.title}
diff --git a/apps/kit/src/routes/+layout.svelte b/apps/kit/src/routes/+layout.svelte
index ee76da9..688673b 100644
--- a/apps/kit/src/routes/+layout.svelte
+++ b/apps/kit/src/routes/+layout.svelte
@@ -3,6 +3,12 @@
import {afterNavigate, beforeNavigate, goto} from "$app/navigation";
import {is_active} from "$lib/session";
import type {Navigation} from "@sveltejs/kit";
+ import {setLocale} from "$lib/i18n/i18n-svelte";
+ import {onMount} from "svelte";
+ import type {LayoutData} from "./$types";
+
+ export let data: LayoutData;
+ onMount(() => setLocale(data.locale));
async function redirect_if_necessary(ticket: Navigation) {
const sessionIsValid = await is_active();
@@ -20,4 +26,5 @@
afterNavigate(redirect_if_necessary);
beforeNavigate(redirect_if_necessary);
+
\ No newline at end of file
diff --git a/apps/kit/src/routes/+layout.ts b/apps/kit/src/routes/+layout.ts
new file mode 100644
index 0000000..de8a5c0
--- /dev/null
+++ b/apps/kit/src/routes/+layout.ts
@@ -0,0 +1,9 @@
+import type {Locales} from "$lib/i18n/i18n-types";
+import {loadLocaleAsync} from "$lib/i18n/i18n-util.async";
+import type {LayoutLoad} from "./$types";
+
+export const load: LayoutLoad<{ locale: Locales }> = async ({url, params}) => {
+ let lang = "en" as Locales;
+ await loadLocaleAsync(lang);
+ return {locale: lang};
+};
\ No newline at end of file
diff --git a/apps/kit/src/routes/+page.svelte b/apps/kit/src/routes/+page.svelte
new file mode 100644
index 0000000..ca15e44
--- /dev/null
+++ b/apps/kit/src/routes/+page.svelte
@@ -0,0 +1 @@
+
Hold on...
\ No newline at end of file
--
cgit v1.3