我一直在努力的想法可能會幫助你。
我用react-native-i18n
(https://github.com/AlexanderZaytsev/react-native-i18n)創建了一個包含我所有翻譯的文件。事情是這樣的:
I18n.translations = {
en: {
helloWorld: 'Hello World!',
loginButton: 'Login',
}
}
於是我進口它在我的組件,它包含應該被翻譯和使用這樣的文字:
<View>
<Text>{I18n.t('helloWorld'}</Text>
</View>
我已經保存在我的數據庫,我的用戶語言當用戶在我的應用程序中登錄時獲取它並設置在我的reducer中。起初我從I18n.currentLocale()
方法得到languge。
在我的部分,我從我的減速器得到了用戶的語言,並將其設置的I18n上配置: I18n.locale = this.props.language;
在這部分我有一個選擇器,以便用戶可以選擇自己的語言。我觸發一個動作來更新我的數據庫,以及減速器和使用componentWillReceiveProps()
生命週期來更新I18n.locale
值
componentWillReceiveProps(nextProps) {
I18n.locale = nextProps.language;
}
希望它可以幫助