getPreferredLocale

Returns the user's currently preferred application locale.

This function retrieves the application-specific locale set via setLanguagePreference. If no application locale is set, it falls back to the system default locale.

Usage in ViewModels

@HiltViewModel
class SettingsViewModel @Inject constructor() : ViewModel() {
private val _uiState = MutableStateFlow(
UiState(
SettingsScreenData(
currentLanguage = getPreferredLocale().displayLanguage
)
)
)
}

Usage for Formatting

val locale = getPreferredLocale()
val formattedDate = SimpleDateFormat("dd MMMM yyyy", locale).format(Date())
val formattedNumber = NumberFormat.getCurrencyInstance(locale).format(price)

Locale Information

val locale = getPreferredLocale()
val languageCode = locale.language // "en", "es", "fr", etc.
val countryCode = locale.country // "US", "ES", "FR", etc.
val displayName = locale.displayLanguage // "English", "Español", "Français"

Return

The user's preferred Locale, or system default if none is set

See also