"use client"; import { useLocale } from "next-intl"; import { useRouter } from "next/navigation"; import { useTransition, useState, useRef, useEffect } from "react"; const LOCALES: { code: string; label: string; lang: string }[] = [ { code: "nl", label: "Nederlands", lang: "nl" }, { code: "en", label: "English", lang: "en" }, { code: "de", label: "Deutsch", lang: "de" }, { code: "fr", label: "Français", lang: "fr" }, { code: "es", label: "Español", lang: "es" }, { code: "it", label: "Italiano", lang: "it" }, ]; export function LanguageSwitcher() { const locale = useLocale(); const router = useRouter(); const [pending, startTransition] = useTransition(); const [open, setOpen] = useState(false); const ref = useRef(null); const current = LOCALES.find((l) => l.code === locale) ?? LOCALES[0]; useEffect(() => { function handleClick(e: MouseEvent) { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false); } document.addEventListener("mousedown", handleClick); return () => document.removeEventListener("mousedown", handleClick); }, []); function switchLocale(code: string) { if (code === locale) return; document.cookie = `NEXT_LOCALE=${code};path=/;max-age=31536000;samesite=lax`; startTransition(() => router.refresh()); setOpen(false); } return (
{open && (
{LOCALES.map((l) => ( ))}
)}
); }