3

We are currently facing some issues using AppCompatDelegate.setApplicationLocales() to set our app language. For example we call:

AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags("fr"))

when changing the language in our app. The activity is recreated as expected and our app shows the correct language.

But then we trigger a push notifications. It's always shown in our default app language (e.g. "de") and not in "fr". I was able to solve it for the notifications by using the following code:

val storedLanguageTag = AppCompatDelegate.getApplicationLocales()[0]
// In our case we set storedLanguageTag to "fr" or "it"
val newLocale = Locale(storedLanguageTag)
Locale.setDefault(newLocale)
val config = context.resources.configuration
val newConfig = Configuration(config).apply { setLocale(newLocale) }
return context.createConfigurationContext(newConfig)

Using the newly created context to build the notification solves the language issue for the notification. But it feels like a workaround and not the right solution. Also I don't understand why this happen We are following the Per-app language preferences guide to support Android 12 and lower.

The pending intent of the notification and the containing intent are created using this newly created context. Clicking on the notification opens an activity but this activity shows some content in our default language and some in the stored language. It's mixed.

How could this happen?

I've tried the same approach like with the notification context by overriding the Activity.attachBaseContext() method but the activity is still shown with mixed language (default "de" and "fr").

Does anyone has faced something similar?

This language issue only happens on Android 12 or lower ... works fine on Android 13. So I guess there has to be somewhere an implementation issue.

Our service inside AndroidManifest looks like this:

<service
    android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
    android:enabled="false"
    android:exported="false">
    <meta-data
        android:name="autoStoreLocales"
        android:value="true" />
</service>

We have omitted the android:localeConfig manifest entry to disable language setting via system settings (or did I get that wrong from the guide?).

Aaron
  • 413
  • 5
  • 13
  • Found https://stackoverflow.com/questions/4985805/set-locale-programmatically. We are using the new AndroidX API already – Aaron Apr 14 '23 at 10:29

1 Answers1

0

Figured out the root cause. Inside our ViewModel we've been using the application context. We solved it by using the activity context inside the view model. My guess is that the AppCompatDelegate only updates the context of AppCompatActivity but not the application context.

For the notification we have to stick to the mentioned workaround in my question.

Aaron
  • 413
  • 5
  • 13