In Per-app language, how to get App locale in API < 33 if System locale is different?
Asked Answered
M

1

8

Android just introduced Per-app language preferences in Android API level 33 which let you change the app language regardless of the system language.

To support devices that running API <= 32, Android in Androidx Appcompat: androidx.appcompat:appcompat:1.6.0-beta01 has added: AppCompatDelegate.setApplicationLocales(appLocale);

Now app language can be differ from system language.

Now there are 2 scenarios:

  1. In API <= 32:
    Locale.getDefault() will return the system Locale which is equal to the app.
  2. In In API 33:
    Locale.getDefault() will return the app locale not the system. And to get system locale, LocaleManagerCompat.getSystemLocales(this).get(0) can be used.

My question: How to get App locale in Android API <= 32 When using backward compatibility with out the need to use Context?

I have found this which is the opposite to what I need.

What I have tried to store Locale then fetch it whenever needed but this method needs Context which is incompetent.

Midshipmite answered 23/8, 2022 at 18:11 Comment(0)
D
6

Can you use AppCompatDelegate.getApplicationLocales()?

I'm working through the same challenges as you right now, and this seems to work for me.

AppCompatDelegate -> [es_US] // after setting with AppCompatDelegate
Locale -> en_US // never changed, my system default

// Worked on emulators for both API 27 and 33
Daffy answered 23/8, 2022 at 23:40 Comment(2)
Thank you. AppCompatDelegate.getApplicationLocales() is returning a list, maybe you are using only 1 locale. So AppCompatDelegate.getApplicationLocales().get(0) is the way to get the the App locale.Midshipmite
It was returning empty to me because I was using it from BaseApplication. The docs state Note: This API should always be called after Activity.onCreate().Marcille

© 2022 - 2024 — McMap. All rights reserved.