基于选择的用户配置文件语言的默认语言

时间:2018-09-11 08:23:08

标签: reactjs react-native internationalization react-redux react-i18next

我正在使用i18n进行语言翻译。我有一个ToggleSwitch可以更改内容的语言,效果很好。但是我想根据选择的用户配置文件语言来设置默认语言。

这是我正在使用的代码。

有帮助吗?

constructor(props) {
    super(props)
    this._retrieveData()
    this.state = {
        isOnBlueToggleSwitch: false
    }
}
onToggle = (isOn) => {
    I18n.locale = isOn ? "th" : "en"
}
_retrieveData = async () => {
    try {
        const value = await AsyncStorage.getItem('user');
        if (value !== null) {
            localLanguage = JSON.parse(value);
            I18n.locale = localLanguage.lang;
        }
    } catch (error) {
        console.log(error)
    }
}
render() {
    return (
        <View style={styles.container}>
            <ToggleSwitch
                onColor="#52a557"
                size='small'
                isOn={this.state.isOnBlueToggleSwitch}
                onToggle={isOnBlueToggleSwitch => {
                    this.setState({ isOnBlueToggleSwitch });
                    this.onToggle(isOnBlueToggleSwitch);
                }}
            />
        </View>
    )
}

i18n.js

'use strict';
import I18n from 'react-native-i18n';

I18n.fallbacks = true;

// List of language
I18n.translations = {
    'en': require('./en'), //for English
    'th': require('./th'), 
};

export default I18n;

0 个答案:

没有答案
相关问题