expo-localization
enables you to interface with the native device locale.
Now, you need to install the package from npm
registry.
npm install expo-localization
or yarn add expo-localization
If you're using Cocoapods, add the dependency to your Podfile
:
pod 'EXLocalization', path: '../node_modules/expo-localization/ios'
and if not already included
pod 'EXCore', path: '../node_modules/expo-core/ios'
and run pod install
.
-
Append the following lines to
android/settings.gradle
:include ':expo-localization' project(':expo-localization').projectDir = new File(rootProject.projectDir, '../node_modules/expo-localization/android')
and if not already included
include ':expo-core' project(':expo-core').projectDir = new File(rootProject.projectDir, '../node_modules/expo-core/android')
-
Insert the following lines inside the dependencies block in
android/app/build.gradle
:api project(':expo-localization')
and if not already included
api project(':expo-core')
Some Unimodules are not included in the default ExpoKit
suite, these modules will needed to be added manually.
If your Android build cannot find the Native Modules, you can add them like this:
./android/app/src/main/java/host/exp/exponent/MainActivity.java
@Override
public List<Package> expoPackages() {
// Here you can add your own packages.
return Arrays.<Package>asList(
new LocalizationPackage() // Include this.
);
}
import React from 'react';
import { Text } from 'react-native';
import { Localization } from 'expo-localization';
import i18n from 'i18n-js';
const en = {
foo: 'Foo',
bar: 'Bar {{someValue}}',
};
const fr = {
foo: 'como telle fous',
bar: 'chatouiller {{someValue}}',
};
i18n.fallbacks = true;
i18n.translations = { fr, en };
i18n.locale = Localization.locale;
export default class LitView extends React.Component {
componentWillMount() {
this._subscription = Localization.addListener(({ locale }) => {
i18n.locale = locale;
});
}
componentWillUnmount() {
if (!!this._subscription) {
this._subscription.remove();
}
}
render() {
return (
<Text>
{i18n.t('foo')} {i18n.t('bar', { someValue: Date.now() })}
</Text>
);
}
}
This API is mostly synchronous and driven by constants.
Native device language, returned in standard format. ex: en-US
, es-US
.
List of all the native languages provided by the user settings. These are returned in the order the user defines in their native settings.
Country code for your device.
A list of all the supported ISO codes.
The current time zone in display format. ex: America/Los_Angeles
This will return true
if the current language is Right-to-Left.
Callbacks are Android only, changing the native locale on iOS will cause all the apps to reset.
Observe when a language is added or moved in the Android settings.
Clear all language observers.
Stop observing when the native languages are edited.