Android Jetpack Compose Language Switching via AppCompatDelegate is not working
Asked Answered
A

3

10

I am trying to build an in-app language switcher for the app I am working on, but I have tried almost everything method available on Stackoverflow. I also tried Official guide from here https://developer.android.com/guide/topics/resources/app-languages#api-implementation

Nothing seems to work. Here is the code on GitHub https://github.com/bauripalash/Pankti-Android

Here's the settings screen where I am changing the language


fun Context.findActivity() : Activity? = when(this){
    is Activity -> this
    is ContextWrapper -> baseContext.findActivity()
    else -> null
}

fun changeLanguage( context: Context , language: String ){
    context.findActivity()?.runOnUiThread {
        val appLocale = LocaleListCompat.forLanguageTags(language)
        AppCompatDelegate.setApplicationLocales(appLocale)
        //context.findActivity()?.recreate()
    }

    //context.findActivity()?.recreate()
}

https://github.com/bauripalash/Pankti-Android/blob/ed8cc2a34a3c615b360860a3c6a3977b98742862/app/src/main/java/in/palashbauri/panktimob/views/SettingsScreen.kt#L103

And here's main activity

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags("bn"))
        AppCompatDelegate.getApplicationLocales()[0]?.let { Log.i("MainView" , it.displayLanguage) }
        setContent {
            PanktiMobTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    MainView()


                }
            }
        }
    }
}

Recreating the activity, doesn't work either.

I have tried almost every method i could find on Stackoverflow and elsewhere.

Android context.getResources.updateConfiguration() deprecated

How to trigger recomposition after updateConfiguration?

How to force jetpack compose to recompose?

Android Language change using JetPack Compose

I know this type of question is being asked a lot here. But can someone help me here.

Anarthrous answered 12/11, 2022 at 9:31 Comment(0)
N
15

Inorder to Work, Change the MaterialTheme to AppCompat Theme in Themes.xml file in values folder.

Check this blog for adding android localization or multilanguage from scratch - click-here

example : themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<!--    <style name="Theme.SatCat" parent="android:Theme.Material.Light.NoActionBar" />-->
    <style name="Theme.SatCat" parent="Theme.AppCompat.Light" />
</resources>

UPDATE : For Jetpack Compose,

Even you add android:theme="@style/Theme.SatCat" of MainActivity in AndroidManifest.xml. it will only work above setContent{} of your ComponentActivity. because,we added our MaterialTheme inside setContent{}.

Solution :

  • We have to change our MainActivity extends ComponentActivity into AppCompatActivity,and use theme as AppCompat Theme in Manifest.

  • After we change its working in Jetpack Compose. enter image description here

  • Another thing,In Your case go to Settings Screen DropDown menu, Once select any language you have to store that code not name enter image description here . . .

More Info : if we use AppCompatDelegate.setApplicationLocales() with MaterialTheme, Context is becomes NULL, so its not able to create Locale Manager.

enter image description here

the Context is null because of sActivityDelegates is empty.

Even I don't know what is sActivityDelegates enter image description here

But, as soon as i change the Theme to AppCompat. its becomes working.

Tell us, if someone know about sActivityDelegates, and why its not adding when lifecycle in postCreate as they mention in Comments. it would be appreciated.

Navarro answered 12/11, 2022 at 12:40 Comment(5)
I tried as you mentioned, but it is not working for me. If I set my device default language to Bengali, it works, but otherwise not workingAnarthrous
updated my answer.check it now.Navarro
I created an issue on google's issue tracker for this issue. It looks like a bug to me. issuetracker.google.com/issues/258907847Anarthrous
dirty but worksLan
it works, not understanding why.Timekeeper
L
0

AppCompatDelegate.setApplicationLocales() work fine with debug version but in release it does not change interface language (release version setApplicationLocales works with android emulator)

Livialivid answered 13/10, 2023 at 6:54 Comment(1)
after long searches I add in build.gradle (:app) bundle { language { enableSplit = false } }Livialivid
N
0

after long searches I add in build.gradle (:app)

bundle {         
    language {             
        enableSplit = false         
    } 
}

It's work for me

Neilneila answered 6/6 at 6:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.