-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathLanguageSwitcher.js
62 lines (54 loc) · 1.65 KB
/
LanguageSwitcher.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
52
53
54
55
56
57
58
59
60
61
62
/* eslint-disable no-shadow */
import React from 'react';
import PropTypes from 'prop-types';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import { connect } from 'react-redux';
import { setLocale } from '../../actions/intl';
import s from './LanguageSwitcher.css';
const localeDict = {
/* @intl-code-template '${lang}-${COUNTRY}': '${Name}', */
'en-US': 'English',
'cs-CZ': 'Česky',
/* @intl-code-template-end */
};
class LanguageSwitcher extends React.Component {
static propTypes = {
currentLocale: PropTypes.string.isRequired,
availableLocales: PropTypes.arrayOf(PropTypes.string).isRequired,
setLocale: PropTypes.func.isRequired,
};
render() {
const { currentLocale, availableLocales, setLocale } = this.props;
const isSelected = locale => locale === currentLocale;
const localeName = locale => localeDict[locale] || locale;
return (
<div className={s.root}>
{availableLocales.map(locale => (
<span key={locale}>
{isSelected(locale) ? (
<span>{localeName(locale)}</span>
) : (
<a
href={`?lang=${locale}`}
onClick={e => {
setLocale({ locale });
e.preventDefault();
}}
>
{localeName(locale)}
</a>
)}{' '}
</span>
))}
</div>
);
}
}
const mapState = state => ({
availableLocales: state.runtime.availableLocales,
currentLocale: state.intl.locale,
});
const mapDispatch = {
setLocale,
};
export default connect(mapState, mapDispatch)(withStyles(s)(LanguageSwitcher));