index.tsx 1.51 KB
Newer Older
陈帅's avatar
陈帅 committed
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';
陈帅's avatar
陈帅 committed
5
import React from 'react';
陈小聪's avatar
陈小聪 committed
6 7 8 9 10 11 12
import classNames from 'classnames';
import HeaderDropdown from '../HeaderDropdown';
import styles from './index.less';

interface SelectLangProps {
  className?: string;
}
何乐's avatar
何乐 committed
13
const SelectLang: React.FC<SelectLangProps> = props => {
陈小聪's avatar
陈小聪 committed
14 15
  const { className } = props;
  const selectedLang = getLocale();
陈帅's avatar
陈帅 committed
16
  const changeLang = ({ key }: ClickParam): void => setLocale(key, false);
陈小聪's avatar
陈小聪 committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  const locales = ['zh-CN', 'zh-TW', 'en-US', 'pt-BR'];
  const languageLabels = {
    'zh-CN': '简体中文',
    'zh-TW': '繁体中文',
    'en-US': 'English',
    'pt-BR': 'Português',
  };
  const languageIcons = {
    'zh-CN': '🇨🇳',
    'zh-TW': '🇭🇰',
    'en-US': '🇬🇧',
    'pt-BR': '🇧🇷',
  };
  const langMenu = (
    <Menu className={styles.menu} selectedKeys={[selectedLang]} onClick={changeLang}>
      {locales.map(locale => (
        <Menu.Item key={locale}>
          <span role="img" aria-label={languageLabels[locale]}>
            {languageIcons[locale]}
          </span>{' '}
          {languageLabels[locale]}
        </Menu.Item>
      ))}
    </Menu>
  );
  return (
    <HeaderDropdown overlay={langMenu} placement="bottomRight">
      <span className={classNames(styles.dropDown, className)}>
        <Icon type="global" title={formatMessage({ id: 'navBar.lang' })} />
      </span>
    </HeaderDropdown>
  );
};

export default SelectLang;