"use client"; import { usePathname, useRouter } from "next/navigation"; import { locales, defaultLocale } from "@/middleware"; export default function LanguageSwitcher() { const pathname = usePathname(); const router = useRouter(); // Extract the language from the url const currentLocale = pathname.split('/')[1] || defaultLocale; const switchLanguage = (newLocale: string) => { if (currentLocale === newLocale) return; // Switch language in the url const segments = pathname.split('/'); segments[1] = newLocale; const newPath = segments.join('/'); // Redirect user router.push(newPath); }; return (
{locales.map((language, index) => (
{index < locales.length - 1 && |}
))}
); }