The library allows you to listen to locale changes on your device. for example reloading the app when the locale changed.
$ npm install react-native-locale-listener --save
$ react-native link react-native-locale-listener
- Open up
android/app/src/main/java/[...]/MainApplication.java
- Add
import com.toyberman.localeReload.RNReactNativeLocalePackage;
to the imports at the top of the file - Add
new RNReactNativeLocalePackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:include ':react-native-locale-listener' project(':react-native-locale-listener').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-locale-listener/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-locale-listener')
add to AndroidManifest.xml this :
android:configChanges="layoutDirection|locale"
and to MainActivity.java
import android.content.res.Configuration;
import android.content.Intent;
...
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Intent intent = new Intent("onConfigurationChanged");
intent.putExtra("newConfig", newConfig);
this.sendBroadcast(intent);
}
you can use it anywhere
import RNReactNativeLocale from 'react-native-locale-listener';
changeLayout(language) {
// Do what you need here
RNRestart.Restart();
}
componentDidMount () {
RNReactNativeLocale.addLocaleListener(this.changeLayout)
}
componentWillUnmount() {
// prevent leaking
RNReactNativeLocale.removeLocaleListener(this.changeLayout)
}