Files
bewell-in-ua/components/shared/locale-switcher.tsx
2025-02-07 08:34:42 +02:00

40 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use client'
import cookies from 'js-cookie'
import {useLocale} from 'next-intl'
import {useRouter} from '@/i18n/routing'
import {Link} from '@/i18n/routing'
import {Label} from '@/ui/label'
import {Switch} from '@/ui/switch'
export default function LocaleSwitcher() {
const router = useRouter()
const locale = useLocale()
const initialState = locale !== 'uk'
//const [localeState, setLocaleState] = useState(initialState)
const handleLocaleChange = (state: boolean) => {
const locale = cookies.get('NEXT_LOCALE') === 'ru' ? 'uk' : 'ru'
cookies.set('NEXT_LOCALE', locale, {
expires: 7,
path: '/',
sameSite: 'Lax'
})
router.replace(window.location.pathname.replace(/^\/ru/, ''), {locale})
}
return (
<div className='flex items-center space-x-2'>
<Label htmlFor='locale-switcher'>Укр</Label>
<Switch
className='bg-brand-violet-900'
id='locale-switcher'
defaultChecked={initialState}
onCheckedChange={handleLocaleChange}
/>
<Label htmlFor='locale-switcher'>Рус</Label>
</div>
)
}