'use client' import i18nConfig from '@/i18nConfig' import { usePathname, useRouter } from 'next/navigation' import { useEffect, useTransition } from 'react' import { Globe } from 'lucide-react' export function LanguageSwitcher() { const router = useRouter() const pathname = usePathname() const [isPending, startTransition] = useTransition() const currentLocale = pathname.split('/')[1] || i18nConfig.defaultLocale useEffect(() => { if (typeof window !== 'undefined') { const saved = localStorage.getItem('preferred-language') if (!saved || saved !== currentLocale) { localStorage.setItem('preferred-language', currentLocale) } } }, [currentLocale]) const handleChange = (e: React.ChangeEvent) => { const newLocale = e.target.value let segments = pathname === '/' ? [] : pathname.split('/').filter(Boolean) const isLocaleInPath = i18nConfig.locales.includes(segments[0] as any) if (isLocaleInPath) { segments[0] = newLocale } else { segments.unshift(newLocale) } const newPath = '/' + segments.join('/') startTransition(() => { if (pathname !== newPath) { router.push(newPath) } else { router.refresh() } }) } return (
) }