Get language from Android or iOS device or web browser using React Native

Tiempo de lectura: < 1 minuto

Reading time: < 1 minute

To obtain the device language using React Native, we need to use the following method:

First, we import the Platform and NativeModules methods from react-native.

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

And with the following method, we obtain the language:

function getLanguage() {
  try {
    const deviceLanguage =
      Platform.OS === 'ios'
        ? NativeModules.SettingsManager.settings.AppleLocale 
        NativeModules.SettingsManager.settings.AppleLanguages[0] //iOS 13
        : NativeModules.I18nManager.localeIdentifier;
    var deviceLanguageSplit = deviceLanguage.split("_")[0];

    return deviceLanguageSplit;
  } catch (error) {
    var defaultLanguage = "es"; //Default language
    //Check browser language:
    if (navigator.language != null) {
      defaultLanguage = navigator.language.split("-")[0];
    }
    return defaultLanguage; //Default for browser
  }
}

Leave a Comment