-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
51 lines (42 loc) · 1.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
import React, { PureComponent } from 'react';
import { GlobalOutlined } from '@ant-design/icons';
import { Menu, Dropdown } from 'antd';
import { LOCALE_STORAGE_NAME } from '../../../api/locale';
import config from '../../../api/util/config';
import withContext from '../../hoc/withContext';
class SelectLang extends PureComponent {
constructor(props) {
super(props);
this.state = { languages: config.languages };
}
componentDidMount() {
const storedLocale = localStorage.getItem(LOCALE_STORAGE_NAME) || 'en';
this.props.changeLocale(storedLocale);
}
getMenuItems = () => {
const { languages } = this.state;
return languages.map(lang => (
<Menu.Item key={lang.key}>
<span role="img" aria-label={lang.name}>
{lang.code}
</span>{' '}
{lang.name}
</Menu.Item>
));
};
render() {
const { changeLocale } = this.props;
const langMenu = (
<Menu onClick={(e) => changeLocale(e.key)}>
{this.getMenuItems()}
</Menu>
);
return (
<Dropdown overlay={langMenu} placement="bottomRight" trigger={['click']}>
<GlobalOutlined title="Select a language" />
</Dropdown>
);
}
}
const mapContextToProps = ({ locale, changeLocale }) => ({ locale, changeLocale });
export default withContext(mapContextToProps)(SelectLang);