fix(notifications): use app language#20747
fix(notifications): use app language#20747david-allison wants to merge 1 commit intoankidroid:mainfrom
Conversation
On API 32 and earlier, AppCompatDelegate.setApplicationLocales only works with AppCompatActivity. Work around this by manually setting the app locale Fixes 19048 Assisted-by: Claude Opus 4.6 - withAppLocale + investigation
| override fun onReceive( | ||
| context: Context, | ||
| rawContext: Context, | ||
| intent: Intent, | ||
| ) { | ||
| // #19048: On API < 33, BroadcastReceiver context uses the system locale. | ||
| val context = rawContext.withAppLocale() |
There was a problem hiding this comment.
I'm tempted to make an AnkiBroadcastReceiver which handles this automatically so people can't forget.
Thoughts?
There was a problem hiding this comment.
I'm not opposed! From a brief skim it seems there are other BroadcastReceivers in the codebase that do things like show themed toasts to the user, which are probably in the incorrect language right now, ex. this line in SharedDecksDownloadFragment can be called by a BroadcastReceiver's onReceive:
context?.let { showThemedToast(it, R.string.something_wrong, false) }So it would be good to enforce this more uniformly.
ericli3690
left a comment
There was a problem hiding this comment.
Thank you David!! This looks great. I'll make a note to test this on the new review reminders system too once our strings are localized.
Only one major concern: this issue was originally opened when I found this to-do comment by @mikehardy in NotificationChannels:
/**
* Create or update all the notification channels for the app
*
* In Oreo and higher, you must create a channel for all notifications.
* This will create the channel if it doesn't exist, or if it exists it will update the name.
*
* Note that once a channel is created, only the name may be changed as long as the application
* is installed on the user device. All other settings are fully under user control.
*
* TODO should be called in response to [Intent.ACTION_LOCALE_CHANGED]
* @param context the context for access to localized strings for channel names
*/
fun setupNotificationChannels(context: Context) {
// ...In particular the TODO portion. Do we actually need to make setupNotificationChannels listen to Intent.ACTION_LOCALE_CHANGED, or will that no longer necessary after merging this PR? If so, please remove the above comment in NotificationChannels.
| override fun onReceive( | ||
| context: Context, | ||
| rawContext: Context, | ||
| intent: Intent, | ||
| ) { | ||
| // #19048: On API < 33, BroadcastReceiver context uses the system locale. | ||
| val context = rawContext.withAppLocale() |
There was a problem hiding this comment.
I'm not opposed! From a brief skim it seems there are other BroadcastReceivers in the codebase that do things like show themed toasts to the user, which are probably in the incorrect language right now, ex. this line in SharedDecksDownloadFragment can be called by a BroadcastReceiver's onReceive:
context?.let { showThemedToast(it, R.string.something_wrong, false) }So it would be good to enforce this more uniformly.
Untested, I suspect it will still be necessary |
|
If the goal is to have "all of the app" change when a language changes - either through our in-app language picker or through a system locale change, then you also must persist new notification channel names into the android system as well, implying that - yes - that notification channel setup needs to be hooked on to whatever infra we have for handling language updates, and we should be listening to system locale change broadcast |
Note
Assisted-by: Claude Opus 4.6 - withAppLocale + investigation
Purpose / Description
On API 32 and earlier,
AppCompatDelegate.setApplicationLocalesonly works with AppCompatActivity.Fixes
Approach
Work around this by manually setting the app locale
How Has This Been Tested?
Android Version = 7.0 (SDK 24)
Learning (optional, can help others)
https://developer.android.com/reference/androidx/appcompat/app/AppCompatDelegate#setApplicationLocales(androidx.core.os.LocaleListCompat)
Checklist