React Native,如何在更改语言后更新视图 [英] React Native, how to update view after language change

查看:90
本文介绍了React Native,如何在更改语言后更新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想支持语言更改(手动由用户).为此,我正在使用

环境

环境:

  • 节点:8.9.4
  • 纱线:1.3.2
  • npm:4.0.5

预期行为

当我使用 I18n.locale ='en'; 不在功能中时..照原样,文本将为英文,而当我使用 I18n.locale ='he'; 文本将使用希伯来语.但是我需要在运行时更改语言.所以我希望当我单击每个按钮时,语言会改变并显示出来.

实际行为

什么都没发生.我想我需要重新加载/重新渲染/更新视图,但是我没有找到方法.

解决方案

您可以尝试这样的事情

 < View style = {styles.container}><按钮onPress = {this.setLocale()} title = {strings('test.b1')}></View>公共setLocale(){this.setState({stateOfLocale:'en'});I18n.locale = stateOfLocale;} 

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

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.

解决方案

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;
}

这篇关于React Native,如何在更改语言后更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆