Open In App

How to Implement Per-App Language Preference in Android 13?

Multilingual users frequently set their system language to one language, like English, but they want to choose different languages for particular programs, like Malayalam, Chinese, or Hindi. Android 13 offers the following improvements for apps that support several languages in order to better serve these users:

Additional APIs: These open APIs allow apps to override the system language at runtime. Examples are the setApplicationLocales() and getApplicationLocales() methods in LocaleManager. We will now look at the detailed sheet of all the features this all-new API brings to us, and to what extent we can leverage it for our use.



Features of Per-App Language Preferences

Particulars

Description

There is already a language selector built into your program.
  • To add your app’s languages to phone settings, use the android:localeConfig element in the manifest of your app.
  • To guarantee that consumers have a consistent experience, convert the bespoke logic in your app to use the public APIs.
You don’t have an in-app language selector in your app.
  • Use the AndroidX library and our API implementation to offer backward compatibility with autoStoreLocales if you wish to add an in-app language selector.
  • To add your app’s languages to phone settings, use the android:localeConfig element in the manifest of your app.

The user-defined settings for the app

Android has a central location in the phone settings for configuring per-app language options as of version 13. Create a locales config XML file and include it in your app’s manifest using the android:localeConfig attribute to guarantee that the languages for your app are selectable in the system settings on devices running Android 13 or higher. Absence of the android:localeConfig manifest element indicates that users shouldn’t be able to change the language of your app independently of their phone’s system language.

How to add this new functionality?

Adding this new functionality to your app is simple, makes you target your app to the latest Android 13 SDK and you’re good to go! Without further ado, let’s start with the XML first:



Step #1: Adding the ‘localeConfig’ to our app

The languages for your app, as well as its ultimate fallback locale, which is the locale defined in res/values/strings.xml, should be specified in a file called res/xml/locales config.xml. Combine the language code with the optional script and region codes, separating each with a dash, to create the locale names. A sample XML is shown below, you may take inspiration from that, and it to your localeConfig yourself!

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

Step #2: Adding the changes to our manifest file

Almost every new change which we do in our app needs to somewhere reflect in our app’s manifest so that the system can know what we are trying to convey to it, we will not add these changes in our app’s manifest to tell the operating system that we want to use per-app flag:

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

Step #3: Adding and specifying the language locales in Gradle

The most crucial step in making the new API work is to update the Gradle file in our Android Application:

android {
      defaultConfig {
          resConfigs "en", "en-us", "hi"
      }
}

That’s it, we have done it, we have implemented the per-app language settings, and now all we need to do is to take these on a quick run.

How to Choose a Language?

  1. Open System Settings
  2. Languages & Input > Settings > System > App Languages (Choose an app)
  3. Language

You can choose from the languages which you have provided in the app, and that will only apply to that particular app, how convenient it is!

Article Tags :