【问题标题】:How to get current language setting in the phone with react native如何使用本机反应在手机中获取当前语言设置
【发布时间】:2021-02-28 14:56:42
【问题描述】:

我希望我的应用支持多种语言,例如英语和中文。使用 react native,如何找到手机上的语言设置?

【问题讨论】:

标签: javascript react-native


【解决方案1】:
import { NativeModules, Platform } from 'react-native';

const deviceLanguage =
          Platform.OS === 'ios'
            ? NativeModules.SettingsManager.settings.AppleLocale ||
              NativeModules.SettingsManager.settings.AppleLanguages[0] // iOS 13
            : NativeModules.I18nManager.localeIdentifier;

console.log(deviceLanguage); //en_US

【讨论】:

  • 请在您的答案中添加解释。
  • 不用任何库就可以获取到本地。
  • 更多信息在这里:stackoverflow.com/a/35493069/2206455
  • 听起来是最好的选择,不使用任何第三方组件
【解决方案2】:

尝试使用这个库 -> https://github.com/AlexanderZaytsev/react-native-i18n

import I18n from 'react-native-i18n';
deviceLocale = I18n.currentLocale()

【讨论】:

  • 非常感谢,我使用了库(react-native-device-info)并解决了它。这个库有很多关于设备的信息。 react-native-device-infoconst DeviceInfo = require('react-native-device-info'); const currentLocale = DeviceInfo.getDeviceLocale();
  • 该库现已弃用,不再维护。
【解决方案3】:
import { NativeModules, Platform } from 'react-native';

let deviceLanguage = Platform.OS === 'ios' ? 
    NativeModules.SettingsManager.settings.AppleLocale:
    NativeModules.I18nManager.localeIdentifier;

if (deviceLanguage === undefined) {
    // iOS 13 workaround, take first of AppleLanguages array 
    deviceLanguage = NativeModules.SettingsManager.settings.AppleLanguages[0]
    if (deviceLanguage == undefined) {
        return defaultLocalization // default language
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 2018-06-05
    • 1970-01-01
    • 2021-11-04
    相关资源
    最近更新 更多