-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |