Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change language in kotlin (locale)

I have 2 string files "en" and "tr". When I change my telephone's language string files change automatically(I didn't write extra code for this result and I don't know how this happen). I want change string files with programmatically. I used this code. I get Toast message but language doesn't change.WHY? I used these code before for another application which I write with java not Kotlin and these code work fine. Please don't say duplicate because I read a lot of questions. I try a lot of things until now 4 hours.

override fun onResume() {

        buttonDate()
        changeLanguage()
    super.onResume()
    }
fun changeLanguage(){
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
        val language = sharedPreferences.getString("language","bak")
        Toast.makeText(applicationContext,language,Toast.LENGTH_SHORT).show()
        if(language=="English"){
            Toast.makeText(applicationContext,"English",Toast.LENGTH_SHORT).show()
            language("")
        }else if(language=="Turkish"){
            Toast.makeText(applicationContext,"Turkish",Toast.LENGTH_SHORT).show()
            language("tr")
        }
    }


    fun language(language: String){
        val locale = Locale(language)
        Locale.setDefault(locale)
        val resources = getResources()
        val configuration = resources.getConfiguration()
        configuration.locale = locale
        resources.updateConfiguration(configuration, resources.getDisplayMetrics())
    }
like image 430
Hacettepe Hesabı Avatar asked May 22 '19 05:05

Hacettepe Hesabı


1 Answers

You need to update configuration even before onCreate is called. To do that create a BaseActivity class like this

open class BaseActivity : AppCompatActivity() {

    companion object {
        public var dLocale: Locale? = null
    }

    init {
        updateConfig(this)
    }

    fun updateConfig(wrapper: ContextThemeWrapper) {
        if(dLocale==Locale("") ) // Do nothing if dLocale is null
            return

        Locale.setDefault(dLocale)
        val configuration = Configuration()
        configuration.setLocale(dLocale)
        wrapper.applyOverrideConfiguration(configuration)
    }
}

Extend you activities from this class.

Set dLocale in you App class like this:

class App : Application() {

    override fun onCreate() {
        super.onCreate()

        var change = ""
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        val language = sharedPreferences.getString("language", "bak")
        if (language == "Turkish") {
            change="tr" 
        } else if (language=="English" ) {
            change = "en"
        }else {
            change ="" 
        } 

        BaseActivity.dLocale = Locale(change) //set any locale you want here
    }
}

You will also need to set App class in your manifest file like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    //..

    <application
        android:name=".App"
        //..>

    </application>
</manifest>    

Note: We should set dLocale only in App onCreate to ensure that all activities have same language.

like image 163
ahsanali274 Avatar answered Oct 11 '22 14:10

ahsanali274