在本地操作系统(iOS)中获得设备语言环境的最佳方法是什么?

与 Objective-C 中的[ NSLocale current Locale ]类似的内容。

74595 次浏览

您可能需要扩展自己的本机反应,以获得这一信息。但是,根据您的用例,这个本地化组件(Stefalda/ReactNativeLocalization)可能适合您。

我正在使用 i18n 包(response-national-i18n)。 然后就是:

I18n = require('react-native-i18n')
locale = I18n.currentLocale()

你可以安装 react-native-i18n并使用以下功能:

import React, { NativeModules } from 'react-native'
...
function getLocale () {
if (React.Platform.OS === 'android') {
return NativeModules.RNI18n.getCurrentLocale(locale => locale.replace(/_/, '-'))
} else {
return NativeModules.RNI18n.locale.replace(/_/, '-')
}
}

在 Android 和 iOS 下都可以工作。

不需要外部库,你可以在 React 的 原生模块中找到你想要的

import { NativeModules } from 'react-native'


// iOS:
const locale = NativeModules.SettingsManager.settings.AppleLocale ||
NativeModules.SettingsManager.settings.AppleLanguages[0] // "fr_FR"


// Android:
const locale = NativeModules.I18nManager.localeIdentifier // "fr_FR"

为了测试这一点,我把设备上的语言改成了法语。下面是一个与 locale 相关的 NativeModules.SettingsManager.settings对象的示例:

{
...
AppleKeyboards: [
"fr_FR@hw=US;sw=QWERTY",
"en_US@sw=QWERTY;hw=Automatic",
"es_419@sw=QWERTY-Spanish;hw=Automatic",
"emoji@sw=Emoji"
]
AppleLanguages: ["fr-US", "en-US", "es-US", "en"]
AppleLanguagesDidMigrate: "9.2"
AppleLocale: "fr_FR"
NSLanguages: ["fr-US", "en-US", "es-US", "en"]
...
}

我为 Android 和 iOS 找到了这个解决方案(RN 0.35)

import React, {NativeModules} from 'react-native';


if (NativeModules.I18nManager) {
const {localeIdentifier, isRTL} = NativeModules.I18nManager;
}

这可能对某些人有帮助,但是 iOS 没有显示 locale 属性。它现在只支持 rtl。

除了 这个组件之外,以上这些都不适合我。

console.log("Device Locale", DeviceInfo.getDeviceLocale()); // e.g en-US
function getLocale() {
if (React.Platform.OS === 'android') {
return I18n.locale;
} else {
return NativeModules.SettingsManager.settings.AppleLocale.replace(/_/, '-');
}
}

如果你在世博会上发展... 你可以使用:

console.log(await NativeModules.ExponentUtil.getCurrentLocaleAsync());
console.log(await NativeModules.ExponentUtil.getCurrentDeviceCountryAsync());
console.log(await NativeModules.ExponentUtil.getCurrentTimeZoneAsync());

随着时间的推移,Facebook 开发人员开发了 NativeModule 解决方案 可以改变
因为这个原因,我准备了 一个组件(目前它只能在 Android 上运行)

使用方法:

import SystemSettings from 'react-native-system-settings'


SystemSettings.get(
settings => console.log('settings: ', settings)
)

也可以承诺-然后可以使用:

SystemSettings.get().then(settings => console.log('settings: ', settings)).done()

还可以使用 ES7异步等待方法!

class App extends React.Component {
componentWillMount() {
this._loadInitialState()
}


_loadInitialState = async () => {
try {
let settings = await SystemSettings.get()
// Now settings variable would be filled and can be used!
} catch (error) {}
};
}

一个样本结果:

{
densityDpi: 320,
fontScale: 1,
hardKeyboardHidden: "no",
keyboard: "qwerty",
keyboardHidden: "no",
localization: {
country: "US",
displayCountry: "United States",
displayLanguage: "English",
displayName: "English (United States)",
is24HourFormat: false,
language: "en",
locale: "en_US",
networkCountry: "US",
simCountry: "US",
timeZone: {
ID: "Europe/Amsterdam",
displayName: {
long: "Amsterdam Standard Time",
short: "GMT+01:00",
},
offset: 3600000
}
},
orientation: "portrait",
screenHeightDp: 615,
screenLayout: "normal",
screenWidthDp: 360,
smallestScreenWidthDp: 360,
uiModeType: "normal"
}
import { Platform, NativeModules } 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

就我个人而言,我更喜欢使用 response-national-i18n。 然后你可以像这样在文档中使用。

import { getLanguages } from 'react-native-i18n'


getLanguages().then(languages => {
console.log(languages) // ['en-US', 'en']
})

连结: https://github.com/AlexanderZaytsev/react-native-i18n

我正在使用 本地反应 -i18n

为了访问我使用的设备语言:

import { getLanguages } from 'react-native-i18n';


getLanguages().then(languages => {
console.log(languages); // ['en-US', 'en']
});

都在文件里。

IOS13可以在这里解决:

locale = NativeModules.SettingsManager.settings.AppleLocale // "fr_FR"
console.log(" ==> Current settings: ", NativeModules.SettingsManager.settings)
if (locale === undefined) {
// iOS 13 workaround, take first of AppleLanguages array
locale = NativeModules.SettingsManager.settings.AppleLanguages[0]
if (locale == undefined) {
return "en" // default language
}
}

这个密码是未来的证据

import {NativeModules} from 'react-native';


function getSystemLocale(): string {
let locale: string;
// iOS
if (
NativeModules.SettingsManager &&
NativeModules.SettingsManager.settings &&
NativeModules.SettingsManager.settings.AppleLanguages
) {
locale = NativeModules.SettingsManager.settings.AppleLanguages[0];
// Android
} else if (NativeModules.I18nManager) {
locale = NativeModules.I18nManager.localeIdentifier;
}


if (typeof locale === 'undefined') {
console.log('Couldnt get locale');
return 'en';
}


return locale;
}


export default {
getSystemLocale,
};
$ npm install --save react-native-localize

或者

$ yarn add react-native-localize

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


console.log(RNLocalize.getLocales());


let defaultLanguage = RNLocalize.getLocales()[0].languageCode;


console.log('defaultLanguage', defaultLanguage);