DEV Community

Hazrat Ummar Shaikh
Hazrat Ummar Shaikh

Posted on

Need help with App language | Android Compose

This code working fine for android 13 and later but not for below android 13

fun changeLanguage(context: Context, language: Languages) {
    val languageString = when (language) {
        Languages.ENGLISH -> "en"
        Languages.BENGALI -> "bn"
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        // Use LocaleManager for Android 13 and later
        val localManager = context.getSystemService(LocaleManager::class.java)
        localManager?.applicationLocales = LocaleList.forLanguageTags(languageString)
    } else {
        AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(languageString))
    }
}

enum class Languages {
    ENGLISH,
    BENGALI
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)