如何通过本机反应获取手机中的当前语言设置

Fei*_*Lei 6 javascript react-native

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

Fir*_*med 28

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
Run Code Online (Sandbox Code Playgroud)

  • 您可以在不使用任何库的情况下获得本地。 (5认同)

Apu*_*ain 5

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

import I18n from 'react-native-i18n';
deviceLocale = I18n.currentLocale()
Run Code Online (Sandbox Code Playgroud)

  • 该库现已弃用,不再维护。 (3认同)