YarikSOffice / lingver

Manage your application locale and language.
https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758
MIT License
512 stars 61 forks source link

Not working after publication? #27

Closed khamidjon closed 4 years ago

khamidjon commented 4 years ago

Firstly, thank you for the library.

While I debug on my computer everything is working fine, when I publish to Play Store, and I download from there, then, if the default language of the device is English, the library is not working. If the default language is Russian, everything is working fine.

Could you help to solve the issue?

YarikSOffice commented 4 years ago

Hi @khamidjon

do you either use app bundles or strip resources via resConfigs in build.gradle?

Could you provide more details on how you use the library and what is the issue exactly? A sample project representing the issue is highly appreciated as well.

khamidjon commented 4 years ago

I have created string.xml, string-en.xml, string-ru.xml files I think this is what you mean by app bundles. I could not find resConfigs in build.gradle, I don't know what it is.

In my App.kt file, I am initializing based on user device's language, it is working fine.

`class App : Application() {

@Suppress("UNUSED_VARIABLE")
override fun onCreate() {
    super.onCreate()

    val language = Locale.getDefault().getLanguage()

    if(language.equals("ru")){
        val store = PreferenceLocaleStore(this, Locale(LANGUAGE_RUSSIAN))
        val lingver = Lingver.init(this, store)
    }else{
        val store = PreferenceLocaleStore(this, Locale(LANGUAGE_ENGLISH))
        val lingver = Lingver.init(this, store)
    }

    // you can use this instance for DI or get it via Lingver.getInstance() later on

}
companion object {
    const val LANGUAGE_ENGLISH = "en"
    const val LANGUAGE_ENGLISH_COUNTRY = "US"
    const val LANGUAGE_UKRAINIAN = "uk"
    const val LANGUAGE_UKRAINIAN_COUNTRY = "UA"
    const val LANGUAGE_RUSSIAN = "ru"
    const val LANGUAGE_RUSSIAN_COUNTRY = "RU"
} 

}`

and through dialog, I am changing application language

`MaterialDialog(requireContext()).show {

            listItemsSingleChoice(R.array.languagechoose, initialSelection = initalIndex)
             { 
               dialog, index, text ->
                if(index != initalIndex){
                    if(index == 0){
                        setNewLocale(LANGUAGE_ENGLISH, LANGUAGE_ENGLISH_COUNTRY)
                    }else{
                        setNewLocale(LANGUAGE_RUSSIAN, LANGUAGE_RUSSIAN_COUNTRY)
                    }
                }
            }
        }

`

as I said, while I run on Android Studio, absolutely everything is working great. But after, I upload this version to Play Store and if the default language of the device is in Russian, changing language is working fine. But if default language is in English, if I want to change language to Russian, it keeps default English language.

YarikSOffice commented 4 years ago

App bundles is a publishing format that optimises your apk for each device by removing unnecessary resources like strings, drawables, etc. According to your description, your app might be affected by that. You can find more info on app bundles in the README.

Apart from that, I don't see any issues in your code. Please consider submitting a sample project in case the issue won't be resolved by the suggestions above.

khamidjon commented 4 years ago

Yes, it worked after I added following lines in build.gradle file.

android { bundle { language { enableSplit = false } } }

as you said, the problem was in the app bundle. Thank you very much.