-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: redirect to same page on language change (#188)
* chore: language selector now is pretty * fix: redirect to exact page on lang change * fix: eslint
- Loading branch information
1 parent
373de0c
commit 5bad9fb
Showing
7 changed files
with
114 additions
and
32 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
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
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,24 +1,73 @@ | ||
<script lang='ts'> | ||
import Globe from 'lucide-svelte/icons/globe' | ||
import { page } from '$app/stores' | ||
import { supportedLocales } from '$lib/translations' | ||
import { type Locale, supportedLocales } from '$lib/translations' | ||
const locale = $page.data.locale | ||
let selected = locale | ||
const dictionary = { | ||
ru: { | ||
short: 'Рус', | ||
long: 'Русский', | ||
}, | ||
en: { | ||
short: 'Eng', | ||
long: 'English', | ||
}, | ||
} | ||
const locale = $page.data.locale as Locale | ||
const redirectUrl = $derived($page.url.pathname.substring(3)) | ||
let isOpened = $state(false) | ||
const handleChange = ({ currentTarget }: { currentTarget: HTMLSelectElement }) => { | ||
const { value } = currentTarget | ||
window.location.href = `/${value}` | ||
const onfocusout = ({ relatedTarget, currentTarget }: { relatedTarget: EventTarget | null, currentTarget: HTMLElement }) => { | ||
if (relatedTarget instanceof HTMLElement && currentTarget.contains(relatedTarget)) { | ||
return | ||
} | ||
isOpened = false | ||
} | ||
</script> | ||
|
||
<select bind:value={selected} onchange={handleChange}> | ||
{#each supportedLocales as value} | ||
<option value={value} selected={selected === value}>{value}</option> | ||
{/each} | ||
</select> | ||
<div class='block' {onfocusout}> | ||
<button class='select-lang' onclick={() => isOpened = !isOpened}> | ||
<div class='icon'> | ||
<Globe /> | ||
</div> | ||
<span>{dictionary[locale].short}</span> | ||
</button> | ||
|
||
{#if isOpened} | ||
<div class='dropdown-menu'> | ||
{#each supportedLocales as locale} | ||
<button onclick={() => window.location.href = `/${locale}${redirectUrl}`}>{dictionary[locale].long}</button> | ||
{/each} | ||
</div> | ||
{/if} | ||
</div> | ||
|
||
<style> | ||
select { | ||
margin: 0; | ||
.block { | ||
position: relative; | ||
} | ||
.select-lang { | ||
padding: 0.25em 0.35em 0.25em 0.25em; | ||
background-color: var(--color-background-2); | ||
display: flex; | ||
flex-direction: row; | ||
align-items: center; | ||
gap: 0.25em; | ||
cursor: pointer; | ||
border: 0; | ||
transition: color 0.2s; | ||
} | ||
.select-lang:hover { | ||
color: var(--color-bg-accent-2) | ||
} | ||
.select-lang .icon { | ||
pointer-events: none; | ||
height: 24px; | ||
} | ||
</style> |
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
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
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
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