javascript 如何使用react native获取手机中的当前语言设置

dtcbnfnu  于 2022-12-10  发布在  Java
关注(0)|答案(4)|浏览(347)

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

kqlmhetl

kqlmhetl1#

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
pxyaymoc

pxyaymoc2#

尝试使用此库-〉https://github.com/AlexanderZaytsev/react-native-i18n

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

ssm49v7z3#

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
    }
}
5anewei6

5anewei64#

您可以使用react-native-localize并获取当前用户的设备语言:

import * as RNLocalize from "react-native-localize";

console.log(RNLocalize.getLocales());

相关问题