英文:
Notification uses default language and screen shows multiple languages while setting app language via AppCompatDelegate
问题
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 happens 😅
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").
Has anyone 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?).
英文:
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?).
答案1
得分: 0
已找到根本原因。
在我们的“ViewModel”内部,我们一直在使用应用程序上下文。
我们通过在视图模型内部使用活动上下文来解决了这个问题。
我猜想“AppCompatDelegate”只更新“AppCompatActivity”的上下文,而不是应用程序上下文。
对于通知,我们必须坚持我问题中提到的解决方法。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论