"use client"; import { useRouter, usePathname } from "next/navigation"; import { locales, type Locale } from "@/lib/i18n/config"; const LABELS: Record = { en: "EN", zh: "中文" }; export function LocaleSwitcher({ current }: { current: string }) { const router = useRouter(); const pathname = usePathname(); const switchTo = current === "zh" ? "en" : "zh"; const other = LABELS[switchTo as Locale]; const handleClick = () => { // Replace locale segment in path const segments = pathname.split("/"); if (locales.includes(segments[1] as Locale)) { segments[1] = switchTo; } else { segments.splice(1, 0, switchTo); } const newPath = segments.join("/") || `/${switchTo}`; document.cookie = `NEXT_LOCALE=${switchTo};path=/;max-age=${60 * 60 * 24 * 365}`; router.push(newPath); }; return ( ); }