Warm tip: This article is reproduced from serverfault.com, please click

React Native, how to update view after language change

发布于 2018-03-01 17:29:12

I want to support language change (manually by user). I'm using react-native-i18n for that. I found how to change the displayed language at run time but I didn't find how to update the current view.

My Code

enter image description here

Environment

Environment:

  • Node: 8.9.4
  • Yarn: 1.3.2
  • npm: 4.0.5

Expected Behavior

When I use I18n.locale ='en'; not in function.. just as it is, the text will be in English and when I use I18n.locale ='he'; the text will be in Hebrew. However I need to change the language at run time. So I want that when I click each button the language will change and will be displayed.

Actual Behavior

Nothing happened.. I assume I need to reload / re-render / update the view but I didn't find how to.

Questioner
Omri Attiya
Viewed
0
Mertcan Diken 2018-03-02 18:04:09

You can try something like this

<View style={styles.container}>
    <Button onPress={this.setLocale()} title={strings('test.b1')}> 
</View>

public setLocale() {
    this.setState({stateOfLocale: 'en'});
    I18n.locale = stateOfLocale;
}