B3nedikt/AppLocale

[SOLVED] Locale not changing for .aab file

Closed this issue · 5 comments

I tried this with .apk file and it worked as expected. But after generating .aab file and published the app in play store locale not changing anymore.

You probably enabled splitting for your aab. Just add this to your build.gradle:

android {
    bundle {
        language {
            // We want to be able to switch the locale at runtime using AppLocale!
            enableSplit = false
        }
    }
}

I may add this to the Readme ;)
Does this fix it for you?

Hi, Extremely sorry for the late response. I was out of town. Yes that solves the issue but this solution is discouraged by google as it skip the benefit of .aab file. Using this solution increases the app size. I was wondering if you could use PlayCore library which is recommended by google for App locale changes.

Documentation URL: https://developer.android.com/guide/playcore/dynamic-delivery#lang_resources

I think adding this feature will make this library A complete & perfect library for In-App-Lang-Picker. If i wasn't a beginner in Android App Development i would send a pull request and add this feature :( I was wondering you could do it easily.

Please let me know if you can add this feature so that i can use this library for each of projects.
Please add that feature.
Anyway, Big thanks for what you did to help developers like me. Thank you.

No problem you´re welcome :) Yes, this disables splits for languages and will lead to a bigger app size. But if you don´t support that many languages, the size difference is marginal, as we are only talking about a few XML files here. Most of the savings from using the aab file format comes from large binary files like images, native libs etc. When you have a large app with many languages supported this might me a different issue though.

I may integrate the lib with PlayCore in some way, however I am not sure how as I don´t want the lib to depend on the Play Core library directly. Maybe an extra artifact which automatically installs/uninstalls languages.

For now I would suggest to just use the PlayCore library manually. With AppLocale this would look like this:

Adjust your Application:

class MyApplication : SplitCompatApplication() {
    
        override fun attachBaseContext(base: Context) {
            super.attachBaseContext(base)

            SplitCompat.install(this)
        }
}

Your base activity:

abstract class BaseActivity : AppCompatActivity() {

    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(ViewPumpContextWrapper.wrap(AppLocale.wrap(newBase)))

        SplitCompat.installActivity(this)
    }

    override fun getResources(): Resources {
        return AppLocale.wrap(baseContext).resources
    }
}

And when you want to change the locale:

        val newLocale = Locale.FRENCH

        val request = SplitInstallRequest.newBuilder()
                .addLanguage(newLocale)
                .build()

        val splitInstallManager = SplitInstallManagerFactory.create(this)
        splitInstallManager.startInstall(request)
                .addOnFailureListener { error ->
                    // Handle the error
                }
                .addOnCompleteListener {
                    AppLocale.desiredLocale = newLocale
                }

Do not forget to handle possible network errors, you´re users might try to change the app language when they are offline ;)

Thanks mate :)

Finally got the time to document this in the Readme ;)