39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { ChangeEvent } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useLanguageStore, type Locale } from '@/stores/languageStore';
|
|
|
|
const AVAILABLE_LOCALES: { code: Locale; label: string }[] = [
|
|
{ code: 'cs', label: 'Čeština' },
|
|
{ code: 'en', label: 'English' },
|
|
];
|
|
|
|
export default function LanguageSwitcher() {
|
|
const { i18n } = useTranslation();
|
|
const locale = useLanguageStore((s) => s.locale);
|
|
const setLocale = useLanguageStore((s) => s.setLocale);
|
|
|
|
const handleChange = async (event: ChangeEvent<HTMLSelectElement>) => {
|
|
const newLocale = event.target.value as Locale;
|
|
|
|
// 1) přepni i18next
|
|
await i18n.changeLanguage(newLocale);
|
|
|
|
// 2) aktualizuj globální store (ten nastaví <html lang> + cookie)
|
|
setLocale(newLocale);
|
|
};
|
|
|
|
return (
|
|
<select
|
|
value={locale}
|
|
onChange={handleChange}
|
|
className="border rounded px-2 py-1 text-sm bg-white dark:bg-gray-900"
|
|
>
|
|
{AVAILABLE_LOCALES.map((l) => (
|
|
<option key={l.code} value={l.code}>
|
|
{l.label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
);
|
|
}
|