2017-12-31 03:08:54 +05:30
|
|
|
// @flow
|
2017-10-16 00:30:21 +05:30
|
|
|
import React, { Component } from 'react';
|
2017-12-16 22:25:57 +05:30
|
|
|
import { FormattedMessage as Message, intlShape } from 'react-intl';
|
2017-10-16 00:30:21 +05:30
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
import LANGS from 'i18n/index.json';
|
|
|
|
import formStyles from 'components/ui/form/form.scss';
|
|
|
|
import popupStyles from 'components/ui/popup/popup.scss';
|
|
|
|
import icons from 'components/ui/icons.scss';
|
|
|
|
import styles from './languageSwitcher.scss';
|
|
|
|
import messages from './languageSwitcher.intl.json';
|
2017-12-31 03:08:54 +05:30
|
|
|
import LanguageList from './LanguageList';
|
2017-12-17 05:56:09 +05:30
|
|
|
|
2017-12-16 06:06:39 +05:30
|
|
|
const improveTranslationUrl = 'http://ely.by/erickskrauch/posts/174943';
|
2017-10-16 00:30:21 +05:30
|
|
|
|
2017-12-31 03:08:54 +05:30
|
|
|
export type LocaleData = {
|
|
|
|
code: string,
|
|
|
|
name: string,
|
|
|
|
englishName: string,
|
|
|
|
progress: number,
|
|
|
|
isReleased: bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
export type LocalesMap = {[code: string]: LocaleData};
|
|
|
|
|
|
|
|
class LanguageSwitcher extends Component<{
|
|
|
|
onClose: Function,
|
|
|
|
userLang: string,
|
|
|
|
changeLang: (lang: string) => void,
|
|
|
|
langs: LocalesMap,
|
|
|
|
emptyCaptions: Array<{
|
|
|
|
src: string,
|
|
|
|
caption: string,
|
|
|
|
}>,
|
|
|
|
}, {
|
|
|
|
filter: string,
|
|
|
|
filteredLangs: LocalesMap,
|
|
|
|
}> {
|
2017-12-16 06:06:39 +05:30
|
|
|
static contextTypes = {
|
|
|
|
intl: intlShape.isRequired,
|
|
|
|
};
|
|
|
|
|
2017-10-16 00:30:21 +05:30
|
|
|
state = {
|
|
|
|
filter: '',
|
2017-12-16 22:25:57 +05:30
|
|
|
filteredLangs: this.props.langs,
|
2017-10-16 00:30:21 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
2017-12-16 22:25:57 +05:30
|
|
|
langs: LANGS,
|
|
|
|
onClose() {},
|
2017-10-16 00:30:21 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2017-12-31 03:08:54 +05:30
|
|
|
const { userLang, onClose } = this.props;
|
|
|
|
const { filteredLangs } = this.state;
|
2017-10-16 00:30:21 +05:30
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.languageSwitcher}>
|
|
|
|
<div className={popupStyles.popup}>
|
|
|
|
<div className={popupStyles.header}>
|
|
|
|
<h2 className={popupStyles.headerTitle}>
|
|
|
|
<Message {...messages.siteLanguage} />
|
|
|
|
</h2>
|
|
|
|
<span className={classNames(icons.close, popupStyles.close)} onClick={onClose} />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className={styles.languageSwitcherBody}>
|
|
|
|
<div className={styles.searchBox}>
|
|
|
|
<input
|
|
|
|
className={classNames(
|
|
|
|
formStyles.lightTextField,
|
2017-12-16 22:25:57 +05:30
|
|
|
formStyles.greenTextField,
|
2017-10-16 00:30:21 +05:30
|
|
|
)}
|
2017-12-16 06:06:39 +05:30
|
|
|
placeholder={this.context.intl.formatMessage(messages.startTyping)}
|
2017-12-16 22:25:57 +05:30
|
|
|
onChange={this.onFilterUpdate}
|
2017-10-16 00:30:21 +05:30
|
|
|
onKeyPress={this.onFilterKeyPress()}
|
2017-12-16 06:06:39 +05:30
|
|
|
autoFocus
|
2017-10-16 00:30:21 +05:30
|
|
|
/>
|
|
|
|
<span className={styles.searchIcon} />
|
|
|
|
</div>
|
|
|
|
|
2017-12-31 03:08:54 +05:30
|
|
|
<LanguageList
|
|
|
|
userLang={userLang}
|
|
|
|
langs={filteredLangs}
|
|
|
|
onChangeLang={this.onChangeLang}
|
|
|
|
/>
|
2017-10-16 00:30:21 +05:30
|
|
|
|
|
|
|
<div className={styles.improveTranslates}>
|
|
|
|
<div className={styles.improveTranslatesIcon} />
|
|
|
|
<div className={styles.improveTranslatesContent}>
|
|
|
|
<div className={styles.improveTranslatesTitle}>
|
|
|
|
<Message {...messages.improveTranslates} />
|
|
|
|
</div>
|
|
|
|
<div className={styles.improveTranslatesText}>
|
|
|
|
<Message {...messages.improveTranslatesDescription} values={{
|
|
|
|
articleLink: (
|
2017-12-16 06:06:39 +05:30
|
|
|
<a href={improveTranslationUrl} target="_blank">
|
2017-10-16 00:30:21 +05:30
|
|
|
<Message {...messages.improveTranslatesArticleLink} />
|
|
|
|
</a>
|
2017-12-16 22:25:57 +05:30
|
|
|
),
|
2017-10-16 00:30:21 +05:30
|
|
|
}} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-12-31 03:08:54 +05:30
|
|
|
onChangeLang = this.changeLang.bind(this);
|
2017-10-16 00:30:21 +05:30
|
|
|
|
|
|
|
changeLang(lang) {
|
|
|
|
this.props.changeLang(lang);
|
2017-12-31 03:08:54 +05:30
|
|
|
|
2017-10-16 00:30:21 +05:30
|
|
|
setTimeout(this.props.onClose, 300);
|
|
|
|
}
|
|
|
|
|
2017-12-31 03:08:54 +05:30
|
|
|
onFilterUpdate = (event: SyntheticInputEvent<HTMLInputElement>) => {
|
|
|
|
const filter = event.currentTarget.value.trim().toLowerCase();
|
2017-12-16 22:25:57 +05:30
|
|
|
const { langs } = this.props;
|
2017-12-31 03:08:54 +05:30
|
|
|
|
2017-12-16 22:25:57 +05:30
|
|
|
const result = Object.keys(langs).reduce((previous, key) => {
|
2018-01-04 04:34:36 +05:30
|
|
|
if (langs[key].englishName.toLowerCase().indexOf(filter) === -1
|
|
|
|
&& langs[key].name.toLowerCase().indexOf(filter) === -1
|
2017-12-16 22:25:57 +05:30
|
|
|
) {
|
|
|
|
return previous;
|
2017-10-16 00:30:21 +05:30
|
|
|
}
|
|
|
|
|
2017-12-16 22:25:57 +05:30
|
|
|
previous[key] = langs[key];
|
|
|
|
|
|
|
|
return previous;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
filter,
|
|
|
|
filteredLangs: result,
|
|
|
|
});
|
|
|
|
};
|
2017-10-16 00:30:21 +05:30
|
|
|
|
|
|
|
onFilterKeyPress() {
|
2018-05-03 10:45:09 +05:30
|
|
|
return (event: SyntheticKeyboardEvent<HTMLInputElement>) => {
|
2017-10-16 00:30:21 +05:30
|
|
|
if (event.key !== 'Enter' || this.state.filter === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-16 22:39:58 +05:30
|
|
|
const locales = Object.keys(this.state.filteredLangs);
|
2017-12-31 03:08:54 +05:30
|
|
|
|
2017-10-16 00:30:21 +05:30
|
|
|
if (locales.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.changeLang(locales[0]);
|
|
|
|
};
|
2017-12-16 06:06:39 +05:30
|
|
|
}
|
2017-10-16 00:30:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { changeLang } from 'components/user/actions';
|
|
|
|
|
|
|
|
export default connect((state) => ({
|
2017-12-16 22:25:57 +05:30
|
|
|
userLang: state.user.lang,
|
2017-10-16 00:30:21 +05:30
|
|
|
}), {
|
2017-12-16 22:25:57 +05:30
|
|
|
changeLang,
|
2017-10-16 00:30:21 +05:30
|
|
|
})(LanguageSwitcher);
|