Change languages of App [JETPACK COMPOSE]

 




Hey!! Hello, lets learn about the language changing feature in android app. With Android 13 (TIRAMASU) , There are three places where we can change the language of our app. 

They are : 

1. From Setting > App Language 

2. From AppSetting > Language 

3. From In APP Language 

Android 13 Provides API to get aware of these all and on top we can have a sync experience in our app. No matter where we changed app language , everywhere it will be reflected. 

Let's Start 


Step 1 : We need to generate the locales config file for our app to make it listed for supporting multi language. To do this lets have a ready made gradle thing : 


androidResources{
generateLocaleConfig = true
}

Next add a 'resources.properties'  file in res folder. add this line 

unqualifiedResLocale=en-US

Basically we are adding the defult language of our app here. 


We can accomplish this same thing by manually creating locale-config.xml file inside xml and then connecting to android manifest. 



Step 2 : - Use these Helper functions to Set the App Language and get the Locales 


fun changeAppLanguage(context: Context, localeName: String) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val localeManager = context.getSystemService(LocaleManager::class.java)
localeManager.applicationLocales = LocaleList.forLanguageTags(localeName)

} else {
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(localeName))
}
}


fun getCurrentLanguageLocale(context: Context): String {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.getSystemService(LocaleManager::class.java).applicationLocales[0].toLanguageTag()
} else {
AppCompatDelegate.getApplicationLocales()[0]?.toLanguageTag() ?: "en-US"
}
}


With these functions we can accomplish the desired result of making our app multilingual. 






Post a Comment

0 Comments