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
    );
  }
}