What's the best way to get device locale in re

2020-02-08 05:42发布

Something similar to [NSLocale currentLocale] in Objective-C.

14条回答
Summer. ? 凉城
2楼-- · 2020-02-08 05:48
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
查看更多
爷、活的狠高调
3楼-- · 2020-02-08 05:50

if you develop over Expo... you can use:

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

查看更多
Emotional °昔
4楼-- · 2020-02-08 05:54

I am using the i18n package (react-native-i18n). And then it's just:

I18n = require('react-native-i18n')
locale = I18n.currentLocale()
查看更多
时光不老,我们不散
5楼-- · 2020-02-08 05:54

I am using react-native-i18n

in order to access the device's language I used:

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

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

It's all in the documentation.

查看更多
Fickle 薄情
6楼-- · 2020-02-08 05:59

iOS 13 workaround here:

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

查看更多
Anthone
7楼-- · 2020-02-08 06:00

I found this solution for Android and iOS (RN 0.35)

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

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

May be this will help to someone, but iOS not shows locale property. It shows only rtl support now.

查看更多
登录 后发表回答