【问题标题】:Dynamically set RTL / LTR in React-Native在 React-Native 中动态设置 RTL/LTR
【发布时间】:2017-11-19 21:17:06
【问题描述】:

我想通过属性动态设置我的应用程序方向。 React Native 允许根据设备的默认语言设置方向,但我想通过我在用户数据上的语言环境属性选择方向。

到目前为止,我在 MainApplication.java 中使用此代码来强制所有用户只使用 LTR:

I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
sharedI18nUtilInstance.allowRTL(getApplicationContext(), false);

我现在正在寻找一种将代码中的“假”切换为根据用户的语言环境更改的布尔值的方法。 不知道如何实现...

额外信息:

  • Android 设备
  • RN 版本:0.40.0

【问题讨论】:

标签: react-native right-to-left react-native-android


【解决方案1】:

你可以使用I18nManager来强制语言方向

constructor() {
    super();
    //set user language b default english
    this.state = {
        lang: 'en'
    }
}


componentWillMount() {
          //get user lang form AsyncStorage 
    AsyncStorage.getItem('lang').then((value) => {
         //now you should forceRTL by Language and set Language in your states
        if ((value === 'ar' || value === 'fa' )) {
            I18nManager.forceRTL(true);
        } else {
            I18nManager.forceRTL(false);
        }
        return this.setState({
            lang: value 
        });
    }).done();
}

【讨论】:

    猜你喜欢
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 2016-03-20
    相关资源
    最近更新 更多