Files
web-Iot/src/components/LanguageSwitcher.tsx

47 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-08-12 11:29:44 +08:00
import React from 'react';
import { Button, Dropdown } from 'antd';
import { GlobalOutlined } from '@ant-design/icons';
import { useTranslation } from 'react-i18next';
import type { MenuProps } from 'antd';
export default function LanguageSwitcher() {
const { i18n } = useTranslation();
const currentLng = i18n.language?.startsWith('en') ? 'en' : 'zh';
const items: MenuProps['items'] = [
{
key: 'zh',
label: (
<span style={{ fontWeight: currentLng === 'zh' ? 600 : 400 }}>
中文
</span>
),
},
{
key: 'en',
label: (
<span style={{ fontWeight: currentLng === 'en' ? 600 : 400 }}>
English
</span>
),
},
];
const handleChange: MenuProps['onClick'] = ({ key }) => {
i18n.changeLanguage(key);
localStorage.setItem('i18nextLng', key);
};
return (
<Dropdown menu={{ items, onClick: handleChange }} placement="bottomRight">
<Button
type="text"
icon={<GlobalOutlined />}
style={{ color: '#fff', fontSize: 'clamp(12px, 1vw, 14px)' }}
>
{currentLng === 'zh' ? '中文' : 'EN'}
</Button>
</Dropdown>
);
}