Posted by Neelansh Sahai Android Developer Relations Engineer (on Twitter and LinkedIn)What you probably have a set of customers who’re fairly fluent in English, Hindi, and Spanish, and so they have a information app on their telephones and like to learn the information in Hindi? For his or her texting app, they like Spanish as they’ve some family and friends who they textual content with in Spanish. However for ease of entry, they nonetheless favor their gadget to be in English. Now there are lots of such use-cases the place the customers would possibly need their app languages to be completely different from their system language. Attention-grabbing!
Construct to your multilingual customers
This weblog focuses on the way to combine the Per-App Language Preferences API in your app to supply your customers the flexibleness to decide on completely different languages for various apps.
1. Customers can change the language settings from system settings by deciding on:
Settings → System → Languages & Enter → App Languages → [Select the desired App] → [Select the desired Language]
2. In case your app already has an in-app language picker, you possibly can combine the Per-App Language Preferences API to leverage the total platform help. For pre-Android 13 customers, the system settings received’t be seen, however builders can nonetheless present an in-app language picker.
Find out how to combine this characteristic in your app?
There are 5 steps that must be adopted whereas engaged on the Per-App Language Preferences characteristic, listed right here →
1. Create locale_config.xml file
Create a brand new file in values/xml/ listing and identify it as locale_config.xml. This file ought to comprise a listing of all of the locales which are supported by the app. The checklist factor must be a string containing a locale tag.
locale_config.xml
2. Add the locale_config within the AndroidManifest.xml
Specify this locale_config.xml file within the app’s AndroidManifest.xml
AndroidManifest.xml
After steps 1 & 2, your customers will have the ability to uncover and set their language choice to your app from system settings on gadgets working Android 13 or greater. In case your customers are on gadgets working on variations decrease than Android 13, you possibly can present an in-app language picker. Optionally, you can too embrace the identical language picker in your app for gadgets working Android 13 or greater. When your app consists of an in-app language picker, it is vital for the person’s preferences to be in sync between the system and the app. That is the place the AndroidX APIs come into the image. Learn on to discover ways to create an in-app language picker.
Use the newest model of AppCompat Library
4. Use AndroidX APIs
Use the APIs in your code to set and get the app locales.
MainActivity.kt
5. Delegate storage to AndroidX
Let AndroidX deal with the locale storage in order that the person’s choice persists.
AndroidManifest.xml
Steps 3, 4, & 5 above exhibit the minimal parts wanted to create an in-app language picker.
And with this, your app can now help locale switching.
Further issues to care for whereas migrating to the API
Earlier, builders needed to deal with the person’s preferences on their very own, both through the use of SharedPreferences, storing the info on a server, or different app logic. With the new APIs, there isn’t a must deal with this individually. So if you find yourself utilizing these APIs, AndroidX is already caring for the storage half, however what occurs when the app is opened for the primary time after a person updates their gadget to Android 13 or greater?
On this case, the system received’t concentrate on the person’s preferences for the app language and thus it’s going to map the app to the default system language. To keep away from this, builders want so as to add some one-time migration logic in order that their customers don’t should set the language once more once they replace the app.
Right here is an instance of the migration that may be performed in case you had been utilizing SharedPreferences earlier to retailer the person’s language choice.
What flexibility does the characteristic present to the customers and builders?
Right here are some things that may show to be helpful for you customers.
- All gadgets working Android 13 or greater could have a standard place for customers to find and alter the language of their apps.
- Though the system settings are restricted to the gadgets working Android 13 or greater, the AndroidX APIs are backwards suitable. Thus, there isn’t a requirement so as to add OS Model checks in your code whereas constructing to your multilingual customers.
- Builders don’t must deal with configuration modifications individually or fear about storing the person’s chosen language each time. The API handles configuration modifications and shops the language preferences for you.
- Works with different Android options like Backup and restore. If a person switches to a brand new gadget and restores the beforehand backed up knowledge, your app will retain the person’s final most well-liked language, thus offering your customers with a greater and extra seamless expertise.
Recap
With that, most components of the characteristic are coated. So let’s have a fast recap on what we mentioned in at this time’s learn.
- A fast learn on what Per-App Language Preferences provide to multilingual customers and app builders.
- What finish customers will see on their gadgets.
- Find out how to migrate your app to the Per-App Language Preferences APIs.
- A number of issues that must be taken care of whereas migrating to the APIs to make sure a greater person expertise.
- Lastly, the advantages that finish customers and builders can leverage from this characteristic.
References