【发布时间】:2021-02-28 14:56:42
【问题描述】:
我希望我的应用支持多种语言,例如英语和中文。使用 react native,如何找到手机上的语言设置?
【问题讨论】:
-
这是一个编码社区,你能提供你面临的编码问题吗?
我希望我的应用支持多种语言,例如英语和中文。使用 react native,如何找到手机上的语言设置?
【问题讨论】:
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
【讨论】:
尝试使用这个库 -> https://github.com/AlexanderZaytsev/react-native-i18n
import I18n from 'react-native-i18n';
deviceLocale = I18n.currentLocale()
【讨论】:
const DeviceInfo = require('react-native-device-info'); const currentLocale = DeviceInfo.getDeviceLocale();
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
}
}
【讨论】: