如何在Kotlin(语言环境)中更改语言 [英] How to change language in kotlin (locale)

查看:289
本文介绍了如何在Kotlin(语言环境)中更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个字符串文件"en"和"tr".更改电话的语言时,字符串文件会自动更改(我没有为此结果编写额外的代码,也不知道如何发生).我想以编程方式更改字符串文件. 我用了这段代码.我收到Toast消息,但语言没有改变.为什么?之前,我将这些代码用于另一个我用Java而不是Kotlin编写的应用程序,并且这些代码可以正常工作.请不要重复,因为我读了很多问题.到目前为止,我已经尝试了4个小时.

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())
    }

推荐答案

即使在调用onCreate之前,也需要更新配置.要做到这一点 创建像这样的BaseActivity类

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.

像这样在您的App类中设置dLocale:

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
    }
}

您还需要像这样在清单文件中设置App类:

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>    

注意:我们应该仅在App onCreate中设置dLocale,以确保所有活动都使用相同的语言.

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

这篇关于如何在Kotlin(语言环境)中更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆