File size: 2,473 Bytes
81438f1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | <script lang="ts">
import { language } from '$lib/i18n';
import { Globe } from 'lucide-svelte';
let currentLang: 'en' | 'ru' = 'en';
let showMenu = false;
language.subscribe(lang => {
currentLang = lang;
});
function setLanguage(lang: 'en' | 'ru') {
language.set(lang);
showMenu = false;
}
function handleClickOutside(event: MouseEvent) {
const target = event.target as HTMLElement;
if (!target.closest('.language-switcher')) {
showMenu = false;
}
}
</script>
<svelte:window on:click={handleClickOutside} />
<div class="language-switcher">
<button class="lang-btn" on:click|stopPropagation={() => showMenu = !showMenu}>
<Globe size={20} />
<span class="lang-code">{currentLang.toUpperCase()}</span>
</button>
{#if showMenu}
<div class="lang-menu">
<button
class="lang-option"
class:active={currentLang === 'en'}
on:click={() => setLanguage('en')}
>
<span class="flag">🇺🇸</span>
<span>English</span>
</button>
<button
class="lang-option"
class:active={currentLang === 'ru'}
on:click={() => setLanguage('ru')}
>
<span class="flag">🇷🇺</span>
<span>Русский</span>
</button>
</div>
{/if}
</div>
<style>
.language-switcher {
position: relative;
}
.lang-btn {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
background: none;
border: none;
border-radius: 8px;
color: var(--text-primary);
cursor: pointer;
transition: background 0.2s;
}
.lang-btn:hover {
background: var(--bg-hover);
}
.lang-code {
font-size: 14px;
font-weight: 500;
}
.lang-menu {
position: absolute;
top: 100%;
right: 0;
margin-top: 8px;
background: var(--bg-secondary);
border: 1px solid var(--border);
border-radius: 8px;
padding: 8px;
min-width: 180px;
z-index: 1000;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}
.lang-option {
display: flex;
align-items: center;
gap: 12px;
width: 100%;
padding: 10px 12px;
background: none;
border: none;
border-radius: 6px;
color: var(--text-primary);
cursor: pointer;
font-size: 14px;
text-align: left;
transition: background 0.2s;
}
.lang-option:hover {
background: var(--bg-hover);
}
.lang-option.active {
background: rgba(155, 89, 182, 0.2);
color: var(--accent);
}
.flag {
font-size: 20px;
}
@media (max-width: 768px) {
.lang-code {
display: none;
}
.lang-menu {
right: auto;
left: 0;
}
}
</style>
|