Search code examples
androidkotlinmultilingualdeprecated

Changing language with updateConfiguration Kotlin Android


I've written a simple app that let's you change the language and see the text in that language, however I've used this code:

    private fun changeLanguage(language: String) {
        Log.i("SettingsFragment", "2. " + binding.languageButton.text.toString())
        if(language != binding.languageButton.text.toString()){
            val local = Locale(language)
            val dm =  resources.displayMetrics
            val con = resources.configuration
            con.locale = local
            resources.updateConfiguration(con, dm)
            val refresh = Intent(
                    requireContext(),
                    MainActivity::class.java
            )
            refresh.putExtra(binding.languageButton.text.toString(), language)
            startActivity(refresh)
        }
    }

and it does work but while building it displays: enter image description here

and I've been wondering, is it worth updating that code to match the current standards (what should I use to do that)? will it break after sometime after because it is no longer supported ?


Solution

  • Generally you are doing right, but it very dependable from android version. I will suggest you to use one of the language change libs. I'm using this library and it's working fine.

    https://github.com/akexorcist/Localization