React Native locale strings

Tl;dr: Use NativeModules in React Native or react-native-device-info to get the device language locale strings “en”, “fr”, “es”, etc.

Work on my first React Native mobile app is nearing completion, just a few more bugs and edge cases to handle. The app is translated in 4 languages with the excellent library i18next by Jan Mühlemann.

BD Monitor language settings

BD Monitor language settings

Users of my app can change the language in the Settings view but one user experience improvement I wanted was to have the app switch automatically according to the language specified by the operating system. That way a Spanish speaking user would see the app translated in Spanish on first app load.

So what I needed from React Native was a simple function or attribute to return me the locale string from the phone. I’d then use that string to tell i18next what language to render. The locale strings that I work with:

  • en“: English
  • nb“: Norwegian Bokmål
  • es“: Spanish
  • is“: Icelandic

However at the time of writing this functionality is not really natively supported by React Native. It was suggested about 1.5 years ago but unfortunately got nowhere. In my search I found few potential solutions suitable for different use cases. Personally I just wanted something quick and easy without having to add any extra third party libraries with native code.

No libraries

import { NativeModules } from "react-native";

// iOS, a locale_region string. Example: "en_US"
let i = NativeModules.SettingsManager.settings.AppleLocale;

// Android, note: haven't tested yet
let a = NativeModules.I18nManager.localeIdentifier;

This is the approach that I took. It requires a bit of extra work so I created a custom function that essentially extracts the first two letters (“en”, “es”, “is”, “nb”) if they exists and do some domain specific things for my app. This was inspired from this StackOverflow answer.

Using a native code library

The library react-native-device-info might be suitable if there is some other device information you need. Requires third party native code to be linked.