7

I would like my app to support multiple languages, for example English and Chinese. Using react native, how do I find the language settings on the phone?

Alex
  • 171
  • 2
  • 10
Feiyang Lei
  • 81
  • 1
  • 1
  • 4
  • This is a coding community, could you provide the coding problem you are facing? – Patrik_P Sep 25 '17 at 06:53
  • 2
    Possible duplicate of [What's the best way to get device locale in react native (iOS)?](https://stackoverflow.com/questions/33468746/whats-the-best-way-to-get-device-locale-in-react-native-ios) – Jigar Shah Sep 25 '17 at 06:56

5 Answers5

38
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
Firoz Ahmed
  • 1,929
  • 16
  • 18
5

Try using this library -> https://github.com/AlexanderZaytsev/react-native-i18n

import I18n from 'react-native-i18n';
deviceLocale = I18n.currentLocale()
hardkoded
  • 18,915
  • 3
  • 52
  • 64
Apurva jain
  • 1,460
  • 13
  • 14
  • thanks a lot, I used the library(react-native-device-info) and solved it. this library has many informations about the device. [react-native-device-info](https://github.com/facebook/react-native) `const DeviceInfo = require('react-native-device-info'); const currentLocale = DeviceInfo.getDeviceLocale();` – Feiyang Lei Oct 17 '17 at 06:45
  • 3
    The library is now deprecated and no longer maintained. – Sampo Feb 12 '20 at 11:51
3
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
    }
}
Alex
  • 171
  • 2
  • 10
Leonid Veremchuk
  • 1,952
  • 15
  • 27
0

You can use react-native-localize and get current user's device language:

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

console.log(RNLocalize.getLocales());
Khurshid Ansari
  • 4,638
  • 2
  • 33
  • 52
0
export const getDeviceLanguage = () => {
  const locale = Platform.select({
    ios: () =>
      NativeModules.SettingsManager.settings.AppleLocale ||
      NativeModules.SettingsManager.settings.AppleLanguages[0],
    default: () => NativeModules.I18nManager.localeIdentifier,
  })();
  return locale.replace('-', '_').split('_')[0];
};
HANNAN Std
  • 369
  • 5
  • 7