-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathLanguageSwitcher.tsx
More file actions
62 lines (56 loc) · 1.92 KB
/
LanguageSwitcher.tsx
File metadata and controls
62 lines (56 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React from "react";
import { useTranslation } from "react-i18next";
import { Globe } from "lucide-react";
const LANGUAGES = [
{ code: "en", label: "English" },
{ code: "zh-CN", label: "简体中文" },
] as const;
interface LanguageSwitcherProps {
className?: string;
}
export const LanguageSwitcher: React.FC<LanguageSwitcherProps> = ({ className }) => {
const { i18n, t } = useTranslation();
const [open, setOpen] = React.useState(false);
const ref = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (ref.current && !ref.current.contains(event.target as Node)) {
setOpen(false);
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => document.removeEventListener("mousedown", handleClickOutside);
}, []);
const handleChange = (code: string) => {
i18n.changeLanguage(code);
setOpen(false);
};
return (
<div className={`relative ${className || ""}`} ref={ref}>
<button
onClick={() => setOpen(!open)}
className="p-2 rounded-md hover:bg-accent hover:text-accent-foreground transition-colors"
title={t("titlebar.language")}
>
<Globe size={16} />
</button>
{open && (
<div className="absolute right-0 mt-2 w-36 bg-popover border border-border rounded-lg shadow-lg z-[250]">
<div className="py-1">
{LANGUAGES.map((lang) => (
<button
key={lang.code}
onClick={() => handleChange(lang.code)}
className={`w-full px-4 py-2 text-left text-sm hover:bg-accent hover:text-accent-foreground transition-colors ${
i18n.language === lang.code ? "bg-accent/50 font-medium" : ""
}`}
>
{lang.label}
</button>
))}
</div>
</div>
)}
</div>
);
};