Sergey
Sergey

Reputation: 71

How to identify device language from the App language in Android 13?

I added support per app language to my app, the android 13 feature, now it is both English and Russian. I can change the app language from settings and no need to change the device language. Now for analytics purposes, I need to know the app language and the device language. But if my device is in English and the app is in Russian, I always receive Russian -

   Resources.getSystem().configuration.locales.get(0).language  - ru

   context.resources.configuration.locales[0].language - ru

   Locale.getDefault().language - ru

I expect to receive en from

Resources.getSystem().configuration.locales.get(0).language

and ru from

context.resources.configuration.locales[0].language

I will be glad for any help

Upvotes: 7

Views: 2015

Answers (2)

aormsby
aormsby

Reputation: 176

I've been working with these:

AppCompatDelegate.getApplicationLocales()  // app locales
LocaleManagerCompat.getSystemLocales(Context) // system locales

getApplicationLocales() may return an empty array [] if there is no app-level setting. In that case, the system locale is being used.

I also use AppCompatDelegate.setApplicationLocales(LocaleListCompat) for consistency.

I'm not sure if there are non-Compat classes available for targeting only API 33, but this is currently working for me down to API 27.

As mentioned in another answer, you'll need to import androidx.appcompat:appcompat:1.6.0-beta01 - see first line here.

Upvotes: 7

Ember Rose
Ember Rose

Reputation: 41

The system locale list is available from LocaleManagerCompat.getSystemLocales(Context) in androidx.core:core:1.9.0-beta01 or later.

Upvotes: 4

Related Questions