Per-App Language Preferences - Part 1
Posted by Neelansh Sahai Android Developer Relations Engineer (on Twitter and LinkedIn)What if you have a set of users who are quite fluent in English, Hindi, and Spanish, and they have a news app on their phones and prefer to read the news in Hindi? For their texting app, they prefer Spanish as they have some friends and family who they text with in Spanish. But for ease of access, they still prefer their device to be in English. Now there are many such use-cases where the users might want their app languages to be different from their system language. Interesting! Starting with Android 13, we have included one of the most-requested features from users, Per-App Language Preferences. It lets users switch app languages from System settings, providing users with better control over their language choices for different apps, irrespective of the system language. Build for your multilingual users This blog focuses on how to integrate the Per-App Language Preferences API in your app to p
Posted by Neelansh Sahai Android Developer Relations Engineer (on Twitter and LinkedIn)What if you have a set of users who are quite fluent in English, Hindi, and Spanish, and they have a news app on their phones and prefer to read the news in Hindi? For their texting app, they prefer Spanish as they have some friends and family who they text with in Spanish. But for ease of access, they still prefer their device to be in English. Now there are many such use-cases where the users might want their app languages to be different from their system language. Interesting!
Build for your multilingual users
This blog focuses on how to integrate the Per-App Language Preferences API in your app to provide your users the flexibility to choose different languages for different apps.
1. Users can change the language settings from system settings by selecting:
Settings → System → Languages & Input → App Languages → [Select the desired App] → [Select the desired Language]
NOTE: Only those apps that have opted in for the feature by specifying the locale_config.xml file (more on this below), will appear in system settings. |
How to integrate this feature in your app?
There are 5 steps that need to be followed while working on the Per-App Language Preferences feature, listed here →
1. Create locale_config.xml file
Create a new file in values/xml/ directory and name it as locale_config.xml. This file should contain a list of all the locales that are supported by the app. The list element should be a string containing a locale tag.
NOTE: The locale tags must follow the BCP47 syntax, which is usually {language subtag}–{script subtag}–{country subtag}. Anything other than that will be filtered out by the system and won't be visible in the system settings. |
locale_config.xml
xml version="1.0" encoding="utf-8"?> |
2. Add the locale_config in the AndroidManifest.xml
Specify this locale_config.xml file in the app’s AndroidManifest.xml
AndroidManifest.xml
|
After steps 1 & 2, your users will be able to discover and set their language preference for your app from system settings on devices running Android 13 or higher. In case your users are on devices running on versions lower than Android 13, you can provide an in-app language picker. Optionally, you can also include the same language picker in your app for devices running Android 13 or higher. When your app includes an in-app language picker, it's important for the user's preferences to be in sync between the system and the app. This is where the AndroidX APIs come into the picture. Read on to learn how to create an in-app language picker.
3. Add the libraries
def latestAppCompatVersion = “1.6.0-rc01” dependencies { |
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags("xx-YY")
// Call this on the main thread as it may require Activity.restart()
AppCompatDelegate.setApplicationLocales(appLocale)
// Call this to get the selected locale and display it in your App
val selectedLocale = AppCompatDelegate.getApplicationLocales()[0]
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags("xx-YY") |
// Call this to get the selected locale and display it in your App |
NOTE: These APIs are also backward compatible, so even if the app is being used on Android 12 or lower, the APIs would still behave the same, and no additional checks for OS versions are required in your code.
NOTE: These APIs are also backward compatible, so even if the app is being used on Android 12 or lower, the APIs would still behave the same, and no additional checks for OS versions are required in your code. |
|
Steps 3, 4, & 5 above demonstrate the minimum components needed to create an in-app language picker.
And with this, your app can now support locale switching.
Additional things to take care of while migrating to the API
In this case, the system won’t be aware of the user’s preferences for the app language and thus it will map the app to the default system language. To avoid this, developers need to add some one-time migration logic so that their users don’t have to set the language again when they update the app.
// Specify the constants to be used in the below code snippets companion object { // Constants for SharedPreference File |
// Utility method to put a string in a SharedPreference
private fun putString(key: String, value: String) {
val editor = getSharedPreferences(PREFERENCE_NAME, PREFERENCE_MODE).edit()
editor.putString(key, value)
editor.apply()
}
// Utility method to get a string from a SharedPreference
private fun getString(key: String): String? {
val preference = getSharedPreferences(PREFERENCE_NAME, PREFERENCE_MODE)
return preference.getString(key, null)
}
// Utility method to put a string in a SharedPreference |
// Check if the migration has already been done or not
if (getString(FIRST_TIME_MIGRATION) != STATUS_DONE) {
// Fetch the selected language from wherever it was stored. In this case it’s SharedPref
// In this case let’s assume that it was stored in a key named SELECTED_LANGUAGE
getString(SELECTED_LANGUAGE)?.let { it →
// Set this locale using the AndroidX library that will handle the storage itself
val localeList = LocaleListCompat.forLanguageTags(it)
AppCompatDelegate.setApplicationLocales(localeList)
// Set the migration flag to ensure that this is executed only once
putString(FIRST_TIME_MIGRATION, STATUS_DONE)
}
}
// Check if the migration has already been done or not // Fetch the selected language from wherever it was stored. In this case it’s SharedPref // In this case let’s assume that it was stored in a key named SELECTED_LANGUAGE // Set this locale using the AndroidX library that will handle the storage itself // Set the migration flag to ensure that this is executed only once |
What flexibility does the feature provide to the users and developers?
Here are a few things that might prove to be beneficial for you users.
- All devices running Android 13 or higher will have a common place for users to discover and change the language of their apps.
- Although the system settings are limited to the devices running Android 13 or higher, the AndroidX APIs are backwards compatible. Thus, there is no requirement to add OS Version checks in your code while building for your multilingual users.
- Developers do not need to handle configuration changes separately or worry about storing the user's selected language every time. The API handles configuration changes and stores the language preferences for you.
- Works with other Android features like Backup and restore. If a user switches to a new device and restores the previously backed up data, your app will retain the user’s last preferred language, thus providing your users with a better and more seamless experience.
Recap
With that, most parts of the feature are covered. So let’s have a quick recap on what we discussed in today’s read.
- A quick read on what Per-App Language Preferences offer to multilingual users and app developers.
- What end users will see on their devices.
- How to migrate your app to the Per-App Language Preferences APIs.
- A few things that need to be taken care of while migrating to the APIs to ensure a better user experience.
- Lastly, the benefits that end users and developers can leverage from this feature.
References
What's Your Reaction?