/** * Language switcher dropdown — mirrors the blog's * `pro/blog/src/components/LanguageSwitcher.astro`. Clicking a locale * rewrites the current URL under the chosen locale's prefix; the * `LocaleSync` wrapper at the top of the router picks up the change, * lazy-loads the resource bundle, and persists the choice to the * `velxio_locale` cookie shared with the blog. */ import { useEffect, useRef, useState } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import { Globe } from 'lucide-react'; import { LOCALES, LOCALE_META, type Locale, } from '../../i18n/config'; import { getLocaleFromPath, switchLocale, } from '../../i18n/path'; export const LanguageSwitcher: React.FC = () => { const location = useLocation(); const navigate = useNavigate(); const rootRef = useRef(null); const [open, setOpen] = useState(false); const current: Locale = getLocaleFromPath(location.pathname); useEffect(() => { const handler = (e: MouseEvent) => { if (rootRef.current && !rootRef.current.contains(e.target as Node)) { setOpen(false); } }; const keyHandler = (e: KeyboardEvent) => { if (e.key === 'Escape') setOpen(false); }; document.addEventListener('mousedown', handler); document.addEventListener('keydown', keyHandler); return () => { document.removeEventListener('mousedown', handler); document.removeEventListener('keydown', keyHandler); }; }, []); const choose = (target: Locale) => { setOpen(false); if (target === current) return; const nextPath = switchLocale(location.pathname, target); navigate(nextPath + location.search + location.hash); }; return (
{open && ( )}
); };