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

49 lines
1.3 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';
import { useAppDispatch } from '../store';
import { setLocale } from '../store/preferencesSlice';
2026-08-12 11:29:44 +08:00
export default function LanguageSwitcher() {
const { i18n, t } = useTranslation();
const dispatch = useAppDispatch();
2026-08-12 11:29:44 +08:00
const currentLng = i18n.language?.startsWith('en') ? 'en' : 'zh';
const items: MenuProps['items'] = [
{
key: 'zh',
label: (
2026-08-14 09:02:52 +08:00
<span style={{ fontWeight: currentLng === 'zh' ? 600 : 400 }}>{t('systemSetting.chinese')}</span>
2026-08-12 11:29:44 +08:00
),
},
{
key: 'en',
label: (
<span style={{ fontWeight: currentLng === 'en' ? 600 : 400 }}>
English
</span>
),
},
];
const handleChange: MenuProps['onClick'] = ({ key }) => {
i18n.changeLanguage(key);
localStorage.setItem('i18nextLng', key);
dispatch(setLocale(key as 'zh' | 'en'));
2026-08-12 11:29:44 +08:00
};
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>
);
}