中英文切换 初始化
This commit is contained in:
46
src/components/LanguageSwitcher.tsx
Normal file
46
src/components/LanguageSwitcher.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
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>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user