Files
gpx.studio/website/src/lib/components/LanguageSelect.svelte
2024-07-05 01:02:53 +02:00

47 lines
1.2 KiB
Svelte

<script lang="ts">
import { goto } from '$app/navigation';
import * as Select from '$lib/components/ui/select';
import { languages } from '$lib/languages';
import { Languages } from 'lucide-svelte';
import { _, locale } from 'svelte-i18n';
let selected = {
value: '',
label: ''
};
$: if ($locale) {
selected = {
value: $locale,
label: languages[$locale]
};
}
function getURLForLanguage(lang?: string): string {
let currentPath = window.location.pathname;
let currentPathArray = currentPath.split('/');
if (currentPathArray.length > 1 && languages.hasOwnProperty(currentPathArray[1])) {
currentPathArray.splice(1, 1);
}
if (lang !== undefined && lang !== 'en') {
currentPathArray.splice(1, 0, lang);
}
return currentPathArray.join('/');
}
</script>
<Select.Root bind:selected onSelectedChange={(s) => goto(getURLForLanguage(s?.value))}>
<Select.Trigger class="w-[180px] {$$props.class ?? ''}">
<Languages size="16" />
<Select.Value class="ml-2 mr-auto" />
</Select.Trigger>
<Select.Content>
{#each Object.entries(languages) as [key, value]}
<Select.Item value={key}>{value}</Select.Item>
{/each}
</Select.Content>
</Select.Root>