Skip to content

Commit

Permalink
fix switch being off
Browse files Browse the repository at this point in the history
  • Loading branch information
Ell1ott committed Jun 15, 2024
1 parent 1b17430 commit 683996a
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions src/lib/components/LanguageSwitcher.svelte
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
<script lang="ts">
import { goto } from "$app/navigation";
import { i18n } from "$lib/i18n";
import { setLanguageTag, languageTag, onSetLanguageTag, isAvailableLanguageTag } from "$lib/paraglide/runtime.js"
import { onMount } from "svelte";
import { goto } from '$app/navigation';
import { i18n } from '$lib/i18n';
import {
setLanguageTag,
languageTag,
onSetLanguageTag,
isAvailableLanguageTag
} from '$lib/paraglide/runtime.js';
import { onMount } from 'svelte';
let currentLang = languageTag();
let currentLang = languageTag();
onSetLanguageTag((tag) => {
currentLang = tag;
// Reroute to the correct language
let currentPath = window.location.pathname;
// Only remove the language tag if it is present
let currentPathNoLang = currentPath.replace(/\/(en|dk)/, '');
let newRouteWithLang = i18n.resolveRoute(currentPathNoLang, tag);
console.log(newRouteWithLang);
if (newRouteWithLang !== window.location.pathname) {
goto(newRouteWithLang);
}
});
onSetLanguageTag((tag) => {
currentLang = tag;
// Reroute to the correct language
let currentPath = window.location.pathname;
// Only remove the language tag if it is present
let currentPathNoLang = currentPath.replace(/\/(en|dk)/, '');
let newRouteWithLang = i18n.resolveRoute(currentPathNoLang, tag);
console.log(newRouteWithLang);
if (newRouteWithLang !== window.location.pathname) {
goto(newRouteWithLang);
}
});
onMount(() => {
setLanguageTag(isAvailableLanguageTag(document.documentElement.lang) ? document.documentElement.lang : 'en');
});
onMount(() => {
setLanguageTag(
isAvailableLanguageTag(document.documentElement.lang) ? document.documentElement.lang : 'en'
);
});
</script>

<div>
<div class="flex items-center gap-2 text-navigation">
<button on:click={() => setLanguageTag('en')}>EN</button>
<button on:click={() => setLanguageTag(languageTag() == 'dk' ? 'en' : 'dk')} aria-label="Toggle language" class="w-8 h-4 rounded-full border">
<div class={"relative h-[15px] w-[15px] rounded-full bg-black transition-transform duration-1000 ease-out dark:bg-white transform" + (currentLang === 'dk' ? ' translate-x-full' : ' translate-x-0')} />
</button>
<button on:click={() => setLanguageTag('dk')}>DK</button>
</div>
</div>
<div class="flex items-center gap-2 text-navigation">
<button on:click={() => setLanguageTag('en')}>EN</button>
<button
on:click={() => setLanguageTag(languageTag() == 'dk' ? 'en' : 'dk')}
aria-label="Toggle language"
class="w-8 h-4 rounded-full border border-muted-foreground/50 bg-muted relative"
>
<div
class={'absolute h-full aspect-square top-0 rounded-full bg-black transition-transform duration-1000 ease-out dark:bg-white transform' +
(currentLang === 'dk' ? ' right-0' : ' translate-x-0')}
/>
</button>
<button on:click={() => setLanguageTag('dk')}>DK</button>
</div>
</div>

0 comments on commit 683996a

Please sign in to comment.