move language selection to store to ensure alignment between different language selectors (mobile and desktop)
Some checks failed
CI / update (push) Failing after 1m9s

This commit is contained in:
2025-12-27 12:11:53 +01:00
parent 4356af4e0a
commit abc0d03e01
2 changed files with 37 additions and 9 deletions

View File

@@ -1,9 +1,9 @@
<script lang="ts"> <script lang="ts">
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { recipeTranslationStore } from '$lib/stores/recipeTranslation'; import { recipeTranslationStore } from '$lib/stores/recipeTranslation';
import { languageStore } from '$lib/stores/language';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
let currentLang = $state('de');
let currentPath = $state(''); let currentPath = $state('');
let langButton: HTMLButtonElement; let langButton: HTMLButtonElement;
let langOptions: HTMLDivElement; let langOptions: HTMLDivElement;
@@ -14,13 +14,13 @@
const path = window.location.pathname; const path = window.location.pathname;
currentPath = path; currentPath = path;
if (path.startsWith('/recipes')) { if (path.startsWith('/recipes')) {
currentLang = 'en'; languageStore.set('en');
} else if (path.startsWith('/rezepte')) { } else if (path.startsWith('/rezepte')) {
currentLang = 'de'; languageStore.set('de');
} else if (path === '/') { } else if (path === '/') {
// On main page, read from localStorage // On main page, read from localStorage
const preferredLanguage = localStorage.getItem('preferredLanguage'); const preferredLanguage = localStorage.getItem('preferredLanguage');
currentLang = preferredLanguage === 'en' ? 'en' : 'de'; languageStore.set(preferredLanguage === 'en' ? 'en' : 'de');
} }
} }
}); });
@@ -32,8 +32,8 @@
} }
async function switchLanguage(lang: 'de' | 'en') { async function switchLanguage(lang: 'de' | 'en') {
// Update the current language state immediately // Update the shared language store immediately
currentLang = lang; languageStore.set(lang);
// Store preference // Store preference
if (typeof localStorage !== 'undefined') { if (typeof localStorage !== 'undefined') {
@@ -144,17 +144,17 @@
<div class="language-selector"> <div class="language-selector">
<button bind:this={langButton} onclick={toggle_language_options} class="language-button"> <button bind:this={langButton} onclick={toggle_language_options} class="language-button">
{currentLang.toUpperCase()} {$languageStore.toUpperCase()}
</button> </button>
<div bind:this={langOptions} class="language-options" hidden> <div bind:this={langOptions} class="language-options" hidden>
<button <button
class:active={currentLang === 'de'} class:active={$languageStore === 'de'}
onclick={() => switchLanguage('de')} onclick={() => switchLanguage('de')}
> >
DE DE
</button> </button>
<button <button
class:active={currentLang === 'en'} class:active={$languageStore === 'en'}
onclick={() => switchLanguage('en')} onclick={() => switchLanguage('en')}
> >
EN EN

View File

@@ -0,0 +1,28 @@
import { writable } from 'svelte/store';
type Language = 'de' | 'en';
function createLanguageStore() {
const { subscribe, set } = writable<Language>('de');
return {
subscribe,
set,
init: () => {
if (typeof window !== 'undefined') {
const path = window.location.pathname;
if (path.startsWith('/recipes')) {
set('en');
} else if (path.startsWith('/rezepte')) {
set('de');
} else if (path === '/') {
// On main page, read from localStorage
const preferredLanguage = localStorage.getItem('preferredLanguage');
set(preferredLanguage === 'en' ? 'en' : 'de');
}
}
}
};
}
export const languageStore = createLanguageStore();