akexorcist / Localization

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

Add support for changing language without activity recreate #70

Closed Shahjahan786 closed 4 years ago

Shahjahan786 commented 4 years ago

Please add support for changing language without activity recreation. like ActivityRecreationHelper

sgallego commented 4 years ago

You can do this with an extension function:

fun LocalizationActivityDelegate.setLanguageWithoutNotification(context: Context, newLocale: Locale) {

    val oldLocale = LanguageSetting.getLanguageWithDefault(
            context,
            LanguageSetting.getDefaultLanguage(context)
    )
    if (!isCurrentLanguageSetting(newLocale, oldLocale)) {
        LanguageSetting.setLanguage(activity, newLocale)
        // Dont notifiy current activity notifyLanguageChanged()
    }

}

private fun isCurrentLanguageSetting(newLocale: Locale, currentLocale: Locale): Boolean {
    return newLocale.toString() == currentLocale.toString()
}
Shahjahan786 commented 4 years ago

You can do this with an extension function:

fun LocalizationActivityDelegate.setLanguageWithoutNotification(context: Context, newLocale: Locale) {

    val oldLocale = LanguageSetting.getLanguageWithDefault(
            context,
            LanguageSetting.getDefaultLanguage(context)
    )
    if (!isCurrentLanguageSetting(newLocale, oldLocale)) {
        LanguageSetting.setLanguage(activity, newLocale)
        // Dont notifiy current activity notifyLanguageChanged()
    }

}

private fun isCurrentLanguageSetting(newLocale: Locale, currentLocale: Locale): Boolean {
    return newLocale.toString() == currentLocale.toString()
}

What if app source code is in Java?

sgallego commented 4 years ago

I think you have to create a new class that extends LocalizationActivityDelegate. Add setLanguageWithoutNotification in the new class and use your custom class instead LocalizationActivityDelegate.

Shahjahan786 commented 4 years ago

setLanguageWithoutNotification

Ok Got it, Thanks @sgallego