Home > database >  Android best way to switch language on run-time
Android best way to switch language on run-time

Time:03-22

I'm looking for a nice way in Android to change the language of an app on run-time (meaning, I wish to press a button to change strings from language A to language B without losing the current navigation, state, user input, etc). So far I have only found a way to do it which requires a restart of the activity, but since I'm running a single activity architecture, this will restart the entire app, which is not ideal.

So, can anyone propose some ways to handle this in Android?

CodePudding user response:

    val languageToLoad = "en" // your language fr etc

    val locale = Locale(languageToLoad)
    Locale.setDefault(locale)
    val config = Configuration()
    config.locale = locale
    baseContext.resources.updateConfiguration(
        config,
        baseContext.resources.displayMetrics
    )

CodePudding user response:

Me, in the apps I use an external library called 'dev.b3nedikt.reword: reword'

in build.gradle file:

 implementation 'dev.b3nedikt.viewpump:viewpump:4.0.7'
    implementation 'dev.b3nedikt.reword:reword:4.0.0'
    implementation 'dev.b3nedikt.applocale:applocale:2.0.3'

github repo: https://github.com/B3nedikt/reword

  • Related