Search code examples
androidlocalizationlocaleandroid-13

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


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.


Solution

  • 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