A modern and elegant dashboard for network traffic visualization and analysis.
"use client";
import { useRouter, usePathname } from "next/navigation";
import { useLocale, useTranslations } from "next-intl";
import { Globe } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
const locales = ["en", "zh"] as const;
type Locale = (typeof locales)[number];
const localeLabels: Record<Locale, string> = {
en: "English",
zh: "中文",
};
export function LanguageSwitcher() {
const router = useRouter();
const pathname = usePathname();
const currentLocale = useLocale() as Locale;
const t = useTranslations("language");
const handleLocaleChange = (newLocale: Locale) => {
const newPathname = pathname.replace(`/${currentLocale}`, `/${newLocale}`);
router.push(newPathname);
};
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon" className="h-9 w-9 rounded-full">
<Globe className="h-4 w-4" />
<span className="sr-only">{t("select")}</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{locales.map((locale) => (
<DropdownMenuItem
key={locale}
onClick={() => handleLocaleChange(locale)}
className={currentLocale === locale ? "bg-muted" : ""}
>
{localeLabels[locale]}
</DropdownMenuItem>
))}
</DropdownMenuContent>
</DropdownMenu>
);
}