commerce/components/ui/locale-switcher.tsx
2023-05-03 09:58:35 +02:00

30 lines
680 B
TypeScript

'use client'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import { i18n } from '../../i18n-config'
export default function LocaleSwitcher() {
const pathName = usePathname()
const redirectedPathName = (locale: string) => {
if (!pathName) return '/'
const segments = pathName.split('/')
segments[1] = locale
return segments.join('/')
}
return (
<div>
<p>Locale switcher:</p>
<ul>
{i18n.locales.map((locale) => {
return (
<li key={locale}>
<Link href={redirectedPathName(locale)}>{locale}</Link>
</li>
)
})}
</ul>
</div>
)
}