4

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.

Mohd
  • 71
  • 7

1 Answers1

5

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
aormsby
  • 126
  • 5
  • 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. – Mohd Aug 24 '22 at 01:04
  • 1
    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().` – Starwave Mar 22 '23 at 10:20