Android Developer Weblog: Language preferences for every app


Posted by Neelansh Sahai Android Developer Relationship Engineer (dated Twitter And the linkedin)What you probably have a bunch of customers who’re fluent in English, Hindi and Spanish, have a information app on their telephone and like to learn information in Hindi? For his or her texting app, they like Spanish as a result of they’ve some family and friends that they textual content with in Spanish. However for ease of entry, they nonetheless favor their system to be in English. There at the moment are many such use instances the place customers might want their software languages ​​to be completely different from their system language. fascinating!

ranging from Android 13We’ve included one of the crucial requested options from the customers, Language preferences for every app. Permits customers to change software languages ​​from the system settings, offering customers with higher management over their language selections for various functions, whatever the system language.

A mobile phone screen showing the app's language preference in the YouTube app's system settings

Construct for multilingual customers

This weblog focuses on how one can combine the Per-App Language Choice API into your app to offer your customers with flexibility to decide on completely different languages ​​for various apps.

1. Customers can change the language settings from the system settings by choosing:

Settings → System → Languages ​​and enter → Software languages ​​→ [Select the desired App] → [Select the desired Language]

be aware: Solely these functions which have opted in to the function by choosing locale_config.xml file (extra on this under), it’s going to seem within the system settings.

Mobile screen showing Finding the language settings from the system settings by selecting Settings → System → Languages ​​and input → Application languages ​​→ [Select the desired App] → [Select the desired Language]

2. In case your app already has an in-app language picker, you’ll be able to combine the language desire API for every app to make the most of full platform help. For customers of the model previous to Android 13, the system settings won’t be seen, however builders can nonetheless present a file In-app language picker.

A mobile screen showing the integration of the per-app language preference API for an app that already has an in-app language picker

How do you combine this function into your app?

There are 5 steps to observe whereas engaged on the Per App Language Preferences function, listed right here →

1. Create locale_config. xml a file

Create a brand new file within the /xml/values ​​listing and identify it as locale_config. xml. This file ought to include a listing of all languages ​​supported by the appliance. The menu merchandise have to be a string containing a language tag.

be aware: Locale tags should observe BCP47 syntax, which is often {language subtag} – {script subtag} – {nation subtag}. Something apart from that shall be filtered out by the system and won’t be seen within the system settings.

locale_config. xml

xml model =“1.0” encoding =“utf-8”?>
xmlns: android=“http://schemas.android.com/apk/res/android”>


android: identify=“ar”/>


android: identify=“ja-JP”/>


android: identify=“zh-Hans-MO”/>


android: identify=“zh-Hant-TW”/>

2. Add locale_config in AndroidManifest.xml

Choose this locale_config. xml file within the software AndroidManifest. xml

AndroidManifest. xml





android: localeConfig=“@xml/locale_config”>


After steps 1 and a couple of, customers will have the ability to uncover and set their very own language desire in your app from the system settings on gadgets operating Android 13 or greater. In case your customers are utilizing gadgets with variations sooner than Android 13, you’ll be able to present an in-app language picker. Optionally, you can even embody the identical language picker in your app for gadgets with Android 13 or greater. When your software contains an in-app language picker, it is crucial that the consumer’s preferences are in sync between the system and the appliance. 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 most recent model of the AppCompat library

def latestAppCompatVersion = “1.6.0-rc01”

dependencies {

Software androidx.appcompat:appcompat:latestAppCompatVersion $
Software androidx.appcompat:appcompat-resources:latestAppCompatVersion $

}

4. Use the AndroidX APIs

Use APIs in your code to set and get software languages.

MainActivity.kt

omen appLocale: LocaleListCompat = LocaleListCompat. forLanguageTags (“xx-YY”)

// name this in the primary thread as it might require Exercise.restart()
AppCompatDelegate. setApplicationLocales (appLocale)

// Name this to get the required language and show it in your software
omen selectedLocale = AppCompatDelegate.getApplicationLocales()[0]

be aware: These APIs are too backward suitable, So even when the app is used on Android 12 or decrease, the APIs will nonetheless work the identical means, and no further checks for OS variations are required in your code.

5. Delegate storage to AndroidX

Let AndroidX deal with storing the locale so the consumer desire persists.

AndroidManifest. xml




android: identify=“androidx.appcompat.app.AppLocalesMetadataHolderService”
android: enabled=“False”
android: exported=“False”>

android: identify=“autoStoreLocals”
android: worth=“TRUE” />



Steps 3, 4, and 5 above present the minimal elements wanted to create an in-app language picker.

With this, your software can now help locale switching.

Extra issues to care for whereas migrating to the API

Earlier, builders needed to deal with consumer preferences themselves, both through the use of frequent preferences, Retailer information on the server or different software logic. With the brand new APIs, there is no such thing as a must take care of this individually. So if you use these APIs, AndroidX already takes care of the storage half, however what occurs when the app is opened for the primary time after the consumer updates their system to Android 13 or greater?

On this case, the system won’t pay attention to the consumer’s desire for the language of the appliance, and can subsequently set the appliance to the default system language. To keep away from this, builders want so as to add one-time paging logic in order that customers do not need to set the language once more when updating the app.

// Outline the constants for use within the code snippets under

Companion Purpose {

// Constants for the SharedPreference file
Fastened quantity omen PREFERENCE_NAME = “frequent desire”
Fastened quantity omen PREFERENCE_MODE = context.MODE_PRIVATE

// Constants for SharedPreference keys
Fastened quantity omen FIRST_TIME_MIGRATION = “Migration_Time_First”
Fastened quantity omen SELECTED_LANGUAGE = “selected_language”

// Constants for SharedPreference values
Fastened quantity omen STATUS_DONE = “status_done”
}

// Utility methodology to place a string into SharedPreference
Particular cheerful String mode(a key: collectionthe worth: collection) {
omen Editor = getSharedPreferences (PREFERENCE_NAMEAnd the PREFERENCE_MODE).Modify()
editor.putString(key, worth)
editor. apply ()
}

// Utility methodology to get a string from SharedPreference
Particular cheerful getString(a key: collection): collection? {
omen desire = getSharedPreferences (PREFERENCE_NAMEAnd the PREFERENCE_MODE)
return desire.getString(key, no factor)
}

// Verify if the migration has already been accomplished
if (getString (FIRST_TIME_MIGRATION)! = STATUS_DONE) {

// Get the required language from wherever it’s saved. On this case SharedPref

// On this case, for example it’s saved in a key named SELECTED_LANGUAGE
getString (SELECTED_LANGUAGE?.Leaves { He. She

// Set this location utilizing the AndroidX library that can deal with the storage itself
omen localeList = LocaleListCompat. forLanguageTags (He. She)
AppCompatDelegate. setApplicationLocales (localeList)

// Set the paging flag to verify this solely will get executed as soon as
putString (FIRST_TIME_MIGRATIONAnd the STATUS_DONE)
}
}

What flexibility does the function present to customers and builders?

Listed here are some issues that could be helpful to customers.

  1. All gadgets operating Android 13 or greater may have a standard place for customers to find and alter the language of their apps.
  2. Though the system settings are restricted to gadgets with Android 13 or greater, AndroidX APIs are backward suitable. Thus, there is no such thing as a requirement so as to add OS model checks in your code whereas constructing for multilingual customers.
  3. Builders need not deal with configuration modifications individually or fear about storing the consumer’s particular language every time. The API handles configuration modifications and shops language preferences for you.
  4. Works with different Android options like backup and restore. If the consumer switches to a brand new system and restores beforehand backed up information, your app will maintain the consumer’s final most well-liked language, offering customers with a greater and smoother expertise.

a abstract

With that stated, most components of the function are coated. So let’s take a fast have a look at what we mentioned in in the present day’s studying.

  1. A fast learn on what every app’s language preferences supply multilingual customers and app builders.
  2. What finish customers will see on their gadgets.
  3. Easy methods to migrate your app to every app’s language desire APIs.
  4. There are some things that should be taken care of whereas migrating to APIs to make sure a greater consumer expertise.
  5. Lastly, the advantages that customers and builders can get from this function.

References

  1. Language preferences for every app
  2. Software kind (create)
  3. Software kind (views)
  4. Language preferences for every app (YouTube video)

The Information Weblog The place You Get The Information First
Android Builders Weblog
#Android #Developer #Weblog #Language #preferences #app

brain2gain