2021-11-30

Android language change and settings are completely broken inside app downloaded from Play Store

I have a function to rewrite and pick app language based on system language while you install an app. This function is working as expected if you build your app from Android Studio (even release build). Also, I can confirm it is working from Play Store on all Androids except Android 10, 11 and 12.

It looks like I will correctly pick locale according to my logs and I will rewrite resource config, but after activity restart, it will jump to English as default no matter what system language is currently set (even if default locale in code is correct - in my case Czech (lang code "cs").

As I said, it is caused by the Google Play version of APK, not from Android Studio one.

Is there any undocumented change by Google Play Terms of Service, that they are blocking resource config to be read-only if uploaded on Play Store since Android 10?

Here is function:

fun applyLanguage() {
        val defaultLocale = startupLocale
        val langs = App.languages
        val langCode = app.languageIndex.let {
            if (it == 0) {
                if(langs.any{ l -> l.first==defaultLocale.language }) {
                    defaultLocale.language
                }else {
                    langs[App.LANGUAGE_INIT_DEFAULT].first
                }
            } else {
                langs[it - 1].first
            }
        }
        App.log("LangChange: MainActivity -> applyLanguage (langCodeSet) $langCode")
        app.sysLog("LangChange: MainActivity -> applyLanguage (langCodeSet) $langCode")
        if (resources.configuration.locale.language != langCode) {
            val l = if (langCode == defaultLocale.language) {
                defaultLocale
            } else
                Locale(langCode, "", "")
            arrayOf(resources, app.resources).forEach { res ->
                val cfg = res.configuration
                cfg.locale = l
                res.updateConfiguration(cfg, res.displayMetrics)
            }
            Locale.setDefault(l)
        }
        app.langCode = if (langs.any { it.first == langCode }) langCode else "cs"
        App.log("LangChange: MainActivity -> applyLanguage (langCode) ${app.langCode}")
        app.sysLog("LangChange: MainActivity -> applyLanguage (langCode) ${app.langCode}")
    }

Its simple function, I have an array of available languages (in my case it's 3 of them based on available resources - translates) and if the default system language is one of them I will set the app in that language, if it's not I will set Czech as default. So if I pick English, I should have the app in English, if I pick German, I should have the app in German, if I pick Czech, I should have the app in Czech and if I pick any other language (for example French) it should be set to Czech as a fallback is there.

Also, the same function is used for language picker in App settings and it's the same issue. Default locale has langCode "cs" but if I pick any of those languages from the picker, it will always set resources to default state (string.xml file) which is of course English.

Another example, I setup default language as French in device settings. I downloaded an app from store and it correctly rewrites resources locale to Czech (language code "cs"). But app was still in English.

So, resources.configuration.locale.language was "cs" after activity restart, but this resource config was completely ignored by the system and system picked default resource xml - string.xml which is English.

So it looks like you cant rewrite the resources config anymore, or technically you can, but this altered resource config is completely ignored by the system.

UPDATE

Additional debugging.

Android 10: Default language (French): App was installed and default language was set to English(suppose to be Czech). If you change language in settings, no matter what language you pick, it will always be set to English.

Android 11: Default language (French): App was installed and default language was set to Czech(correct). If you change your language in settings it gets interesting: If you change to English, app switches to English. If you change back to Czech, app switches to Czech. If you change to German, app switches to English (I dont know whats going on).

Android 12: Default language (French): App was installed and default language was set to English(suppose to be Czech). If you change to English, app switches to English. If you change back to Czech, app switches to English. If you change to German, app switches to German.

Android 9, 8, 7, 6 (and probably lower) - working as intended.

I'm not sure whats going on but its kinda funny.



from Recent Questions - Stack Overflow https://ift.tt/3I42asQ
https://ift.tt/eA8V8J

No comments:

Post a Comment