From 57801e1a81be3578c32f069c29f7fe91cf341c10 Mon Sep 17 00:00:00 2001
From: phoebus-84 <83974413+phoebus-84@users.noreply.github.com>
Date: Sat, 17 Feb 2024 03:31:51 +0100
Subject: [PATCH] feat: add language settings (#19)
* feat: add settings
* fix: add missing translations key
* fix: remove old language switcher
---
messages/en.json | 8 +++
messages/it.json | 4 ++
src/lib/components/atoms/Header.svelte | 25 ---------
src/lib/components/languageSwitcher.svelte | 20 --------
src/lib/components/molecules/Header.svelte | 51 +++++++++++++++++++
src/lib/components/molecules/Settings.svelte | 24 +++++++++
src/routes/+layout.ts | 13 ++---
src/routes/[[lang]]/+page.svelte | 19 +++----
src/routes/[[lang]]/+page.ts | 18 +++++++
.../[id]/credential-detail/+page.svelte | 7 +--
src/routes/[[lang]]/[id]/verify/+page.svelte | 6 +--
11 files changed, 125 insertions(+), 70 deletions(-)
create mode 100644 messages/en.json
create mode 100644 messages/it.json
delete mode 100644 src/lib/components/atoms/Header.svelte
delete mode 100644 src/lib/components/languageSwitcher.svelte
create mode 100644 src/lib/components/molecules/Header.svelte
create mode 100644 src/lib/components/molecules/Settings.svelte
create mode 100644 src/routes/[[lang]]/+page.ts
diff --git a/messages/en.json b/messages/en.json
new file mode 100644
index 0000000..9433935
--- /dev/null
+++ b/messages/en.json
@@ -0,0 +1,8 @@
+{
+ "$schema": "https://inlang.com/schema/inlang-message-format",
+ "Verify_credential": "Verify credential",
+ "VERIFICATION_QR": "VERIFICATION QR",
+ "Generate_QR": "Generate QR",
+ "CREDENTIAL_DETAIL": "CREDENTIAL DETAIL",
+ "VERIFIER": "VERIFIER"
+}
\ No newline at end of file
diff --git a/messages/it.json b/messages/it.json
new file mode 100644
index 0000000..e173475
--- /dev/null
+++ b/messages/it.json
@@ -0,0 +1,4 @@
+{
+ "$schema": "https://inlang.com/schema/inlang-message-format",
+ "Verify_credential": "Verifica credenziali"
+}
\ No newline at end of file
diff --git a/src/lib/components/atoms/Header.svelte b/src/lib/components/atoms/Header.svelte
deleted file mode 100644
index 3f0ba78..0000000
--- a/src/lib/components/atoms/Header.svelte
+++ /dev/null
@@ -1,25 +0,0 @@
-
-
-
-
- {#if backButton}
-
- window.history.back()} on:keydown={() => window.history.back()} aria-hidden>
-
-
-
- {/if}
-
-
-
- {#if backButton}
-
-
-
- {/if}
-
-
diff --git a/src/lib/components/languageSwitcher.svelte b/src/lib/components/languageSwitcher.svelte
deleted file mode 100644
index f27cc9f..0000000
--- a/src/lib/components/languageSwitcher.svelte
+++ /dev/null
@@ -1,20 +0,0 @@
-
-
- {
- await setLanguagePreference(e.detail.value);
- await goto(i18n.route($page.url.pathname));
- }}
- class="w-fit"
->
- {#each availableLanguageTags as tag}
- {tag}
- {/each}
-
diff --git a/src/lib/components/molecules/Header.svelte b/src/lib/components/molecules/Header.svelte
new file mode 100644
index 0000000..45cc4cf
--- /dev/null
+++ b/src/lib/components/molecules/Header.svelte
@@ -0,0 +1,51 @@
+
+
+
+
+ {#if backButton}
+
+ window.history.back()}
+ on:keydown={() => window.history.back()}
+ aria-hidden
+ >
+
+
+
+ {/if}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Settings
+
+
+ Close
+
+
+
+
+
+
+
diff --git a/src/lib/components/molecules/Settings.svelte b/src/lib/components/molecules/Settings.svelte
new file mode 100644
index 0000000..07b7eab
--- /dev/null
+++ b/src/lib/components/molecules/Settings.svelte
@@ -0,0 +1,24 @@
+
+
+
+ Language
+ {
+ await setLanguagePreference(e.detail.value);
+ await goto(i18n.route($page.url.pathname), e.detail.value);
+ }}
+ >
+ {#each availableLanguageTags as language}
+ {language}
+
+ {/each}
+
+
+
diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts
index 99e7dc5..e4a4f79 100644
--- a/src/routes/+layout.ts
+++ b/src/routes/+layout.ts
@@ -1,27 +1,20 @@
export const ssr = false;
+
//
-// import { r, type Locale } from '$lib/i18n';
-// import { redirect } from '@sveltejs/kit';
import { Device } from '@capacitor/device';
import { getLanguagePreference, setLanguagePreference } from '$lib/preferences/lang';
+
const setInitialLanguage = async () => {
if (!(await getLanguagePreference())) {
const lang = await Device.getLanguageTag();
+ //@ts-ignore
await setLanguagePreference(lang.value.split('-')[0]);
}
};
-// const getLang = async () => {
-// const lang = await getLanguagePreference();
-// if (lang && ['it', 'en'].includes(lang)) return lang as Locale;
-// return 'en' as Locale;
-// };
-
export const load = async () => {
await setInitialLanguage();
- // const lang = await getLang();
- // redirect(303, r('/', lang));
};
diff --git a/src/routes/[[lang]]/+page.svelte b/src/routes/[[lang]]/+page.svelte
index 9890e81..b973d04 100644
--- a/src/routes/[[lang]]/+page.svelte
+++ b/src/routes/[[lang]]/+page.svelte
@@ -1,26 +1,27 @@
-
+
+
{#await getServices()}
{:then res}
{@const services = res.result.items}
- Verify credential
+ {m.Verify_credential()}
{#each services as service}
-
+
{/each}
{/await}
-
-
-
diff --git a/src/routes/[[lang]]/+page.ts b/src/routes/[[lang]]/+page.ts
new file mode 100644
index 0000000..5bb9956
--- /dev/null
+++ b/src/routes/[[lang]]/+page.ts
@@ -0,0 +1,18 @@
+//
+import { r, type Locale } from '$lib/i18n';
+import { redirect } from '@sveltejs/kit';
+import { getLanguagePreference} from '$lib/preferences/lang';
+import { availableLanguageTags } from '$paraglide/runtime';
+
+const getLang = async () => {
+ const lang = await getLanguagePreference();
+ if (lang && availableLanguageTags.includes(lang)) return lang as Locale;
+ return 'en' as Locale;
+};
+
+export const load = async ({url}) => {
+ const lang = await getLang();
+ if (url.toString().split('/')[3] !== lang) {
+ redirect(301, r('/', lang));
+ }
+};
diff --git a/src/routes/[[lang]]/[id]/credential-detail/+page.svelte b/src/routes/[[lang]]/[id]/credential-detail/+page.svelte
index e7e5396..eab2588 100644
--- a/src/routes/[[lang]]/[id]/credential-detail/+page.svelte
+++ b/src/routes/[[lang]]/[id]/credential-detail/+page.svelte
@@ -1,15 +1,16 @@
-
+
- Generate QR
+ {m.Generate_QR()}
diff --git a/src/routes/[[lang]]/[id]/verify/+page.svelte b/src/routes/[[lang]]/[id]/verify/+page.svelte
index 6d7bf43..1284437 100644
--- a/src/routes/[[lang]]/[id]/verify/+page.svelte
+++ b/src/routes/[[lang]]/[id]/verify/+page.svelte
@@ -7,7 +7,8 @@
import { qrcode } from '@slangroom/qrcode';
import { onMount } from 'svelte';
import { thumbsDownOutline, thumbsUpOutline } from 'ionicons/icons';
- import Header from '$lib/components/atoms/Header.svelte';
+ import Header from '$lib/components/molecules/Header.svelte';
+ import { m } from '$lib/i18n';
export let data: any;
@@ -72,9 +73,8 @@ Then print data
addListeners();
-
+
- back
{#if incomingNotification}