Make language preferences persisted

parent 3191f8fb
...@@ -18,25 +18,25 @@ class LangSwitcher extends Component { ...@@ -18,25 +18,25 @@ class LangSwitcher extends Component {
this.onLangChanged = props.onLangChanged; this.onLangChanged = props.onLangChanged;
this.state = { this.state = {
activeLanguage: 0 activeLanguage: this.props.activeLanguage || this.languages[0]
}; };
} }
changeLanguage = index => { changeLanguage = element => {
this.setState({ activeLanguage: index }); this.setState({ activeLanguage: element });
this.onLangChanged(this.languages[index]); this.onLangChanged(element);
} }
render() { render() {
const thumbs = this.languages.map((element, index) => { const thumbs = this.languages.map((element, index) => {
if (index === this.state.activeLanguage) { if (element === this.state.activeLanguage) {
return <Thumbnail key={index} className="lang-thumb active" return <Thumbnail key={index} className="lang-thumb active"
src={countryImages[element]} src={countryImages[element]}
onClick={e => this.changeLanguage(index)}></Thumbnail> onClick={e => this.changeLanguage(element)}></Thumbnail>
} else { } else {
return <Thumbnail key={index} className="lang-thumb" return <Thumbnail key={index} className="lang-thumb"
src={countryImages[element]} src={countryImages[element]}
onClick={e => this.changeLanguage(index)}></Thumbnail> onClick={e => this.changeLanguage(element)}></Thumbnail>
} }
}); });
......
...@@ -21,14 +21,13 @@ class TypoModal extends Component { ...@@ -21,14 +21,13 @@ class TypoModal extends Component {
text: this.props.text, text: this.props.text,
correct: this.props.text, correct: this.props.text,
context: this.props.context, context: this.props.context,
language: config.defaultLanguage, language: this.getLanguage(),
comment: "", comment: "",
error: "" error: ""
} }
this.languages = [ "ru", "en" ]; this.languages = [ "ru", "en" ];
// TODO: English support
i18n.setLanguage(this.state.language); i18n.setLanguage(this.state.language);
this.closeCallback = props.closeCallback; this.closeCallback = props.closeCallback;
...@@ -149,10 +148,22 @@ class TypoModal extends Component { ...@@ -149,10 +148,22 @@ class TypoModal extends Component {
}); });
} }
// Returns language from local storage preferences or from default config value
getLanguage() {
const persisted = window.localStorage.getItem(`${config.appStorageKey}.lang`);
return persisted || config.defaultLanguage;
}
// Save language user preference to the local storage
persistLanguage(lang) {
window.localStorage.setItem(`${config.appStorageKey}.lang`, lang);
}
onLangChanged = language => { onLangChanged = language => {
i18n.setLanguage(language); i18n.setLanguage(language);
this.setState({language: language}); this.setState({language: language});
this.forceUpdate();
this.persistLanguage(language);
} }
render() { render() {
...@@ -193,7 +204,7 @@ class TypoModal extends Component { ...@@ -193,7 +204,7 @@ class TypoModal extends Component {
</Modal.Body> </Modal.Body>
<Modal.Footer> <Modal.Footer>
<LangSwitcher onLangChanged={this.onLangChanged} languages={this.languages}></LangSwitcher> <LangSwitcher activeLanguage={this.state.language} onLangChanged={this.onLangChanged} languages={this.languages}></LangSwitcher>
<Button onClick={this.handleClose}>{i18n.close}</Button> <Button onClick={this.handleClose}>{i18n.close}</Button>
<Button onClick={this.submitTypo} bsStyle="primary">{i18n.saveChanges}</Button> <Button onClick={this.submitTypo} bsStyle="primary">{i18n.saveChanges}</Button>
......
...@@ -10,4 +10,6 @@ export const config = { ...@@ -10,4 +10,6 @@ export const config = {
requestTimeout: 10000, requestTimeout: 10000,
defaultLanguage: "ru", defaultLanguage: "ru",
appStorageKey: "typos.etersoft",
}; };
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment