Initial commit
This commit is contained in:
38
resources/js/components/LanguageSwitcher.tsx
Normal file
38
resources/js/components/LanguageSwitcher.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
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>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user