setLanguagePreference

fun setLanguagePreference(languageCode: String)(source)

Sets the application locale based on a language code.

Delegates to AppCompatDelegate.setApplicationLocales. Locale application and persistence depend on the Android version and AppCompat configuration; this function does not update a DataStore preference or make arbitrary date/number formatters use the new locale.

The app currently offers English and Arabic. To extend the picker, update resources, the language model, options and locale mapping together; see localization. The manifest enables AppCompat locale storage, and the app generates its locale configuration from resources. Verify selection, persistence and recreation on pre-33 and 33+ devices.

Existing SettingsViewModel method excerpt; imports and containing class omitted:

fun updateLanguagePreference(language: Language) {
setLanguagePreference(language.code)
}

Parameters

languageCode

IETF BCP 47 language tag (e.g., "en", "es-MX", "zh-Hans")

See also