expo-localization
allows you to Localize your app, customizing the experience for specific regions, languages, or cultures. It also provides access to the locale data on the native device.
Using the popular library i18n-js
with expo-localization
will enable you to create a very accessible experience for users.Android Device | Android Emulator | iOS Device | iOS Simulator | Web |
---|---|---|---|---|
expo install expo-localization
If you're installing this in a bare React Native app, you should also follow these additional installation instructions.
i18n-js
yarn add i18n-js
import * as Localization from 'expo-localization'; import i18n from 'i18n-js'; // Set the key-value pairs for the different languages you want to support. i18n.translations = { en: { welcome: 'Hello' }, ja: { welcome: 'こんにちは' }, }; // Set the locale once at the beginning of your app. i18n.locale = Localization.locale;
i18n.fallbacks = true;
."CFBundleAllowMixedLocalizations": true
to your ios.infoPlist
property in your app.json so that your app supports the retrieval of localized strings from frameworks.import * as React from 'react'; import { View, StyleSheet, Text } from 'react-native'; import * as Localization from 'expo-localization'; import i18n from 'i18n-js'; // Set the key-value pairs for the different languages you want to support. i18n.translations = { en: { welcome: 'Hello', name: 'Charlie' }, ja: { welcome: 'こんにちは' }, }; // Set the locale once at the beginning of your app. i18n.locale = Localization.locale; // When a value is missing from a language it'll fallback to another language with the key present. i18n.fallbacks = true; export default App => { return ( <View style={styles.container}> <Text style={styles.text}> {i18n.t('welcome')} {i18n.t('name')} </Text> </View> ); }; %%placeholder-start%%const styles = StyleSheet.create({ ... }); %%placeholder-end%%const styles = StyleSheet.create({ container: { backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', flex: 1, }, text: { fontSize: 20, }, });
import * as Localization from 'expo-localization';
AppState
and Localization.getLocalizationAsync()
. Initially the constants will be correct on both platforms, but on Android a user can change the language and return, more on this later.en
, en-US
, zh-Hans
, zh-Hans-CN
, en-emodeng
.null
on Android or web. Ex: US
, NZ
, null
.America/Los_Angeles
.timezone
is calculated with Intl.DateTimeFormat().resolvedOptions().timeZone
. For a better estimation you could use the moment-timezone
package but it will add significant bloat to your website's bundle size.USD
, EUR
, CNY
, null
.,
, .
..
,
, ,
.Android only, on iOS changing the locale settings will cause all the apps to reset.
type NativeEvent = { currency?: string; decimalSeparator: string; digitGroupingSeparator: string; isoCurrencyCodes: ?Array<string>, isMetric: boolean; isRTL: boolean, locale: string, locales: Array<string>, timezone: string, region: ?string, };
// When the app returns from the background on Android... const { locale } = await Localization.getLocalizationAsync();