8

Per-app language preferences  |  Android 13 Developer Preview  |  Android Develo...

 2 years ago
source link: https://developer.android.com/about/versions/13/features/app-languages
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Per-app language preferences

In many cases, multilingual users set their system language to one language—such as English—but they want to select other languages for specific apps, such as Dutch, Chinese, or Hindi. To help apps provide a better experience for these users, Android 13 introduces APIs that let apps set a different language at runtime to display in their user interface.

Apps that use custom in-app language pickers can use the new APIs to reduce the amount of boilerplate code that's required to display an in-app language picker. To support previous versions of the Android platform, we'll be adding similar APIs in upcoming versions of our Jetpack libraries.

Apps that do not support multiple languages are not impacted by these changes.

API Implementation

For apps that do not currently use a custom in-app language picker, no additional work is required.

For apps that do have an in-app language picker or that want to use one, use the new setApplicationLocales() and getApplicationLocales() methods instead of your apps custom logic to handle setting and getting a user's preferred language for your app.

For example, to set a user's preferred language, you would ask the user to select a locale in the language picker, then set that value in the system:

// 1. Inside an activity, in-app language picker gets an input locale "xx-YY"
// 2. App calls the API to set its locale
mContext.getSystemService(LocaleManager.class
    ).setApplicationLocales(newLocaleList(Locale.forLanguageTag("xx-YY")));
// 3. The system updates the locale and restarts the app, including any configuration updates
// 4. The app is now displayed in "xx-YY" language

To get a user's current preferred language to display in the language picker, your app can get the value back from the system:

// 1. App calls the API to get the preferred locale
LocaleList currentAppLocales =
    mContext.getSystemService(LocaleManager.class).getApplicationLocales();
// 2. App uses the returned LocaleList to display languages to the user

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK