setApplicationLocales

open fun setApplicationLocales(@NonNull locales: @NonNull LocaleListCompat)(source)

Sets the current locales for the calling app.

If this method is called after any host components with attached AppCompatDelegates have been 'created', a LocaleList configuration change will occur in each. This may result in those components being recreated, depending on their manifest configuration.

This method accepts LocaleListCompat as an input parameter.

Apps should continue to read Locales via their in-process LocaleLists.

Pass a getEmptyLocaleList to reset to the system locale.

Note: This API should always be called after Activity.onCreate(), apart from any exceptions explicitly mentioned in this documentation.

On API level 33 and above, this API will handle storage automatically.

For API levels below that, the developer has two options:

  • They can opt-in to automatic storage handled through the library. They can do this by adding a special metaData entry in their AndroidManifest.xml, similar to :
    
        <service
            android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
            android:enabled="false"
            android:exported="false">
            <meta-data
                android:name="autoStoreLocales"
                android:value="true" />
        </service>
    
    They should be mindful that this will cause a blocking diskRead and diskWrite strictMode violation, and they might need to suppress it at their end.
  • The second option is that they can choose to handle storage themselves. In order to do so they must use this API to initialize locales during app-start up and provide their stored locales. In this case, API should be called before Activity.onCreate() in the activity lifecycle, e.g. in attachBaseContext(). Note: Developers should gate this to API versions <33.

    This API should be called after Activity.onCreate() for all other cases.

When the application using this API with API versions <33 updates to a version >= 33, then there can be two scenarios for this transition:

  • If the developer has opted-in for autoStorage then the locales will be automatically synced to the framework. Developers must specify android:enabled="false" for the AppLocalesMetadataHolderService as shown in the meta-data entry above.
  • If the developer has not opted-in for autoStorage then they will need to handle this transition on their end.

Note: This API work with the AppCompatActivity context, not for others context, for Android 12 (API level 32) and earlier. If there is a requirement to get the localized string which respects the per-app locale in non-AppCompatActivity context, please consider using getString or getContextForLanguage.

Parameters

locales

a list of locales.