index.js 1.47 KB
Newer Older
1
import React, { PureComponent } from 'react';
2
import { formatMessage, setLocale, getLocale } from 'umi/locale';
3
import { Menu, Icon } from 'antd';
4
import classNames from 'classnames';
5
import HeaderDropdown from '../HeaderDropdown';
6 7 8
import styles from './index.less';

export default class SelectLang extends PureComponent {
David Z. Han's avatar
David Z. Han committed
9
  changeLang = ({ key }) => {
10 11 12 13 14
    setLocale(key);
  };

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