在本机(iOS)中获取设备区域设置的最佳方法是什么?

时间:2015-11-02 00:05:37

标签: react-native

类似于Objective-C中的[NSLocale currentLocale]。

15 个答案:

答案 0 :(得分:26)

不需要外部库。您可以在React的Native Modules

中找到您要查找的内容
import { NativeModules } from 'react-native'

// iOS:
const locale = NativeModules.SettingsManager.settings.AppleLocale // "fr_FR"

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

为了测试这一点,我将设备上的语言更改为法语。以下是您在与区域设置相关的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"]
    ...
}

答案 1 :(得分:13)

下面的函数将返回一个2个字母的语言代码。例如:en

function getLanguageCode() {
  let systemLanguage = 'en';
  if (Platform.OS === 'android') {
    systemLanguage = NativeModules.I18nManager.localeIdentifier;
  } else {
    systemLanguage = NativeModules.SettingsManager.settings.AppleLocale;
  }
  const languageCode = systemLanguage.substring(0, 2);
  return languageCode;
}

答案 2 :(得分:12)

我正在使用i18n软件包(react-native-i18n)。 然后就是:

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

答案 3 :(得分:6)

以上任何内容都不适合我,但this组件。

console.log("Device Locale", DeviceInfo.getDeviceLocale()); // e.g en-US

答案 4 :(得分:5)

function getLocale() {
  if (React.Platform.OS === 'android') {
    return I18n.locale;
  } else {
    return NativeModules.SettingsManager.settings.AppleLocale.replace(/_/, '-');
  }
}

答案 5 :(得分:2)

此处的iOS 13解决方法:

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

答案 6 :(得分:0)

您可能需要自己扩展react native以获取此信息。但是,根据您的使用案例,此本地化组件(stefalda/ReactNativeLocalization)可能适合您。

答案 7 :(得分:0)

您可以安装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。

答案 8 :(得分:0)

我找到了适用于Android和iOS(RN 0.35)的解决方案

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

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

可能这对某人有帮助,但iOS不显示语言环境属性。它现在只显示rtl支持。

答案 9 :(得分:0)

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

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

答案 10 :(得分:0)

Facebook开发人员随着时间推移使用NativeModules解决方案can be changed 由于这个原因,我准备了a component。 (目前仅适用于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 async-await方法!

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"
}

答案 11 :(得分:0)

我个人更喜欢使用react-native-i18n。 然后你可以在文档中使用这样的..

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

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

link:https://github.com/AlexanderZaytsev/react-native-i18n

答案 12 :(得分:0)

我正在使用react-native-i18n

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

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

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

这完全在文档中。

答案 13 :(得分:0)

此代码可供将来验证

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,
};

答案 14 :(得分:0)

$ 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);