akexorcist / Localization

[Android] In-app language changing library
Apache License 2.0
983 stars 154 forks source link

Wrong language in BroadcastReceiver (local notification) #102

Closed PepaZapletal closed 3 years ago

PepaZapletal commented 3 years ago

Hi,

I have a problem with the correct language in the notification.

I have English as the default language. Now I set a new language german in the app. In the app, it's working okay.

But when the scheduled notification arrived then it's in English (default language).

Should I set something? How can I fix it? I'm using LocalizationApplication.

Thanks for help.

akexorcist commented 3 years ago

@PepaZapletal Because context in Broadcast Receiver isn't localized context. So please add this extension function in your project.

fun Context.toLocalizedContext(): Context = LocalizationUtility.applyLocalizationConfig(this)

Then convert it before using getString(resId: String)

class SimpleBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val localizedContext = context.toLocalizedContext()
        val content = localizedContext.getString(R.string.hello_world)
        // Do something
    }
}
akexorcist commented 3 years ago

Good news. 2.1.9 is live with the Context.toLocalizedContext() extension function.