React-Native Android Per-app language settings

Lee young-jun
3 min readSep 28, 2023

--

In the last article, I mentioned the concept of proper language.

Proper language pertains to iOS, while Android offers the Per-app language feature starting from SDK 33.

However, I couldn’t find this option in Kickgoing app settings.

localConfig

To enable the Per-app language settings in the Android app, you need to make modifications to the project.

Open AndroidManifest.xml and add localeConfig.

<application
...
android:localeConfig="@xml/locales_config">

And create res/xml/locales_config.xml

<?xml version="1.0" encoding="utf-8"?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
<locale android:name="fr"/>
<locale android:name="en"/>
...
</locale-config>

You will be able to see it after rebuilding the project.

Open app language settings

localSettings

Kickgoing includes a menu in its settings to access the language setting.

I’m using the localSettings method from react-native-open-settings to open this menu. I imported the library dynamically because it's specific to Android.

const AndroidOpenSettings = await import('react-native-android-open-settings')

AndroidOpenSettings.localeSettings()

However, it’s important to note that the localSettings cannot be used to directly access the app's language settings.

appLocaleSettings

To access the app’s language settings on Android, we utilize the APP_LOCALE_SETTINGS.

In order to send this intent, we have to make changes to the native code. As a result, I cloned the localeSettings as appLocaleSettings and replaced ACTION_LOCALE_SETTINGS with ACTION_APP_LOCALE_SETTINGS in the code.

However, despite these modifications, it didn’t yield the expected results.

@ReactMethod
public void appLocaleSettings() {
Intent intent = new Intent(Settings.ACTION_APP_LOCALE_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

if (intent.resolveActivity(reactContext.getPackageManager()) != null) {
reactContext.startActivity(intent);
}
}

As per the guidelines for using ACTION_APP_LOCALE_SETTINGS, a package name is required as intent data.

Input: The Intent’s data URI can specify the application package name to directly invoke the app locale details GUI specific to the package name. For example “package:com.my.app”.

Hence, I assigned the package name to the intent data.

intent.setData(Uri.parse("package:" + reactContext.getPackageName()));

Upon rebuilding, I was able to navigate to the app’s language settings using appLocaleSettings.

Additionally, I forked the react-native-android-open-settings repository and released version 1.3.1, which includes the appLocaleSettings method.

References

--

--

No responses yet