49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
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';
|
|
|
|
export default function LanguageSwitcher() {
|
|
const { i18n, t } = useTranslation();
|
|
const dispatch = useAppDispatch();
|
|
const currentLng = i18n.language?.startsWith('en') ? 'en' : 'zh';
|
|
|
|
const items: MenuProps['items'] = [
|
|
{
|
|
key: 'zh',
|
|
label: (
|
|
<span style={{ fontWeight: currentLng === 'zh' ? 600 : 400 }}>{t('systemSetting.chinese')}</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);
|
|
dispatch(setLocale(key as 'zh' | 'en'));
|
|
};
|
|
|
|
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>
|
|
);
|
|
}
|