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

2

7

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

Illconditioned answered 18/8, 2022 at 7:47 Comment(3)
Try this ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration())Crawly
The suggestion above does not work. The first locale in the list is still the app language, not the device language.Imperfect
@VivekGupta also receive the app language and not the device languageIllconditioned
C
5

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.

Conception answered 24/8, 2022 at 0:3 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Farm
Note that AppCompatDelegate.getApplicationLocales() only works after onCreate()Crotchety
E
3

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

Erotic answered 19/8, 2022 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.