登录后更改主页的文字语言

时间:2018-09-12 02:40:39

标签: react-native react-i18next

我有两个页面的login.js和main.js文件。当用户登录时,我必须根据用户已注册的个人资料中存储的语言来设置主页的语言。我在_storeData处检查了我的I18n.locale是否设置为所选的语言用户。但是main.js中的内容并未更改。

login.js

class Login extends React.Component {

    constructor(props) {
        super(props);
    }
    async login() {
        this._storeData(this.props.login.getLogin, this.props.login.lPassword)
    }
    _storeData = async (user, password) => {
        try {
            await AsyncStorage.setItem('user', JSON.stringify(user))
            await AsyncStorage.setItem('password', password)
            I18n.locale = user.language;

        } catch (error) {
            console.log(error)
        }
    }
}

main.js

import I18n from '../../translations/i18n';

class Main extends Component {
    constructor(props) {
        super(props)
        this._retrieveData()
    }
    _retrieveData = async () => {
        try {
            const value = await AsyncStorage.getItem('user');
                this.props.editUser(JSON.parse(value))
        } catch (error) {
            console.log(error)
        }
    }

    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.menutext}>{I18n.t('company')}</Text>
            </View>
        )
    }
}

1 个答案:

答案 0 :(得分:0)

如果可能在整个时间范围内更改语言环境,请尝试从 $(document).ready(function() { $("#summernote").summernote(); //Comment var commentForm = document.getElementById("commentForm"); var domID = "#taskPro" + jobID; $(domID).html('<div class="progress-bar progress-bar-striped progress-bar-animated bg-danger" style="width:100%;">Sending Report</div>'); $(domID).load("../../../config/actions.php?cN=" + customerNumber + "&date=" + date + "&fullName=" + newString7 + "&address=" + newString8 + "&landMark=" + newString9 + "&commet=" + comment + "&complextion=" + complextion + "&type1=" + type1 + "&type2=" + type2 + "&color=" + color + "&xOwner=" + newString0 + "&areaProfile=" + newString1 + "&ifn=" + newString2 + "&ixOwner=" + newString3 + "&iAddress=" + newString4 + "&telephone=" + newString5 + "&requestType=" + newString6 + "&jobID=" + jobID + "&gpsCoords=" + gpsCoords + "&submitReport=1"); //Post Comment $.ajax({ url: "../testAction.php", type: "POST", data: new FormData(commentForm), contentType: "text/plain" }); });甚至从 <form method="post" action="" id="commentForm"> <textarea class="form-control" name="comment" id="summernote"><p>Comment: Should contain Comment, Verification Status Description, Building deatails. Also upload images where neccessary</p></textarea> </form> <script> </script>生命周期方法中而不是从构造函数中调用_retrieveData方法。现在,由于您是从构造函数调用它的,因此它发生在挂载之前,因此它使用的数据可能已过时。

相关问题