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条回答
forever°为你锁心
2楼-- · 2020-02-08 06:00

This code is future proof

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,
};
查看更多
三岁会撩人
3楼-- · 2020-02-08 06:03
function getLocale() {
  if (React.Platform.OS === 'android') {
    return I18n.locale;
  } else {
    return NativeModules.SettingsManager.settings.AppleLocale.replace(/_/, '-');
  }
}
查看更多
Explosion°爆炸
4楼-- · 2020-02-08 06:04

Nothing of the above worked for me, but this component.

console.log("Device Locale", DeviceInfo.getDeviceLocale()); // e.g en-US
查看更多
成全新的幸福
5楼-- · 2020-02-08 06:04

You can install react-native-i18n and use this function:

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(/_/, '-')
  }
}

Works both under Android and iOS.

查看更多
家丑人穷心不美
6楼-- · 2020-02-08 06:04

You may need to extend react native yourself to get this info. But, depending on your use case, this localization component (stefalda/ReactNativeLocalization) may work for you.

查看更多
beautiful°
7楼-- · 2020-02-08 06:06

NativeModules solution can be changed over time by Facebook developers.
Because of this reason I had prepared a component. (It works only on Android for now)

Sample usage:

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

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

Also promise-then can be used:

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

Also ES7 async-await method can be used!

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) {}
    };
}

A sample result:

{
    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"
}
查看更多
登录 后发表回答