Reputation: 433
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()
}
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.
Upvotes: 12
Views: 3218
Reputation: 11
after long searches I add in build.gradle (:app)
bundle {
language {
enableSplit = false
}
}
It's work for me
Upvotes: 1
Reputation: 1
AppCompatDelegate.setApplicationLocales()
work fine with debug version
but in release it does not change interface language (release version setApplicationLocales
works with android emulator)
Upvotes: 0
Reputation: 477
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.
Another thing,In Your case go to Settings Screen DropDown menu, Once select any language you have to store that code not name
.
.
.
More Info : if we use AppCompatDelegate.setApplicationLocales() with MaterialTheme, Context is becomes NULL, so its not able to create Locale Manager.
the Context is null because of sActivityDelegates is empty.
Even I don't know what is sActivityDelegates
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.
Upvotes: 16