6c93b1d995
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import React from "react";
|
|
import { Globe } from "lucide-react";
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu";
|
|
import { Button } from "@/components/ui/button";
|
|
import { useLocale } from "next-intl";
|
|
import { usePathname, useRouter } from "@/i18n/navigation";
|
|
import { i18n, Locale, languageDisplayNames } from "@/constants/i18n-config";
|
|
|
|
const LanguageSwitcher = () => {
|
|
const locale = useLocale();
|
|
const pathname = usePathname();
|
|
const router = useRouter();
|
|
|
|
const switchLanguage = (nextLocale: Locale) => {
|
|
if (nextLocale === locale) return;
|
|
router.replace(pathname, { locale: nextLocale });
|
|
};
|
|
|
|
return (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button variant="ghost" size="sm" className="h-8 w-8 px-0">
|
|
<Globe className="h-4 w-4" />
|
|
<span className="sr-only">Switch language</span>
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end">
|
|
{i18n.locales.map((locale) => (
|
|
<DropdownMenuItem
|
|
key={locale}
|
|
onClick={() => switchLanguage(locale)}
|
|
className="cursor-pointer"
|
|
>
|
|
{languageDisplayNames[locale]}
|
|
</DropdownMenuItem>
|
|
))}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
);
|
|
};
|
|
|
|
export default LanguageSwitcher;
|