index.tsx 1.29 KB
Newer Older
1 2
import { Icon, Menu } from 'antd';
import { formatMessage, getLocale, setLocale } from 'umi-plugin-react/locale';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
3

何乐's avatar
何乐 committed
4
import { ClickParam } from 'antd/es/menu';
5
import React from 'react';
6
import classNames from 'classnames';
duanledexianxianxian's avatar
duanledexianxianxian committed
7
import DefaultSettings, { localeProps } from 'config/defaultSettings';
8 9 10 11 12
import HeaderDropdown from '../HeaderDropdown';
import styles from './index.less';

interface SelectLangProps {
  className?: string;
duanledexianxianxian's avatar
duanledexianxianxian committed
13
  locales: localeProps[];
14
}
duanledexianxianxian's avatar
duanledexianxianxian committed
15

何乐's avatar
何乐 committed
16
const SelectLang: React.FC<SelectLangProps> = props => {
duanledexianxianxian's avatar
duanledexianxianxian committed
17
  const { className, locales } = props;
18
  const selectedLang = getLocale();
19
  const changeLang = ({ key }: ClickParam): void => setLocale(key, false);
20 21
  const langMenu = (
    <Menu className={styles.menu} selectedKeys={[selectedLang]} onClick={changeLang}>
duanledexianxianxian's avatar
duanledexianxianxian committed
22 23 24 25
      {locales.map(({ key, icon, label }) => (
        <Menu.Item key={key}>
          <span role="img" aria-label={label}>
            {icon}
26
          </span>{' '}
duanledexianxianxian's avatar
duanledexianxianxian committed
27
          {label}
28 29 30 31
        </Menu.Item>
      ))}
    </Menu>
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
32
  return locales.length >= 1 ? (
33 34 35 36 37
    <HeaderDropdown overlay={langMenu} placement="bottomRight">
      <span className={classNames(styles.dropDown, className)}>
        <Icon type="global" title={formatMessage({ id: 'navBar.lang' })} />
      </span>
    </HeaderDropdown>
duanledexianxianxian's avatar
duanledexianxianxian committed
38
  ) : null;
39 40 41
};

export default SelectLang;