【问题标题】:How to apply changes after language change?语言更改后如何应用更改?
【发布时间】:2020-04-12 19:38:56
【问题描述】:

我使用自定义设置活动来满足我的偏好。其中之一是ListPreference,用于选择应用程序语言。到目前为止,我可以实现区域设置首选项,但要应用更改,我需要重新启动 app。问题是,它不适用于所有 Android 版本。这是我所做的:

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.FragmentActivity
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat

class SettingsActivity : FragmentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)
        val toolbar: Toolbar = findViewById(R.id.toolbar_settings)
        toolbar.apply {
            setNavigationIcon(R.drawable.ic_arrow)
            setNavigationOnClickListener { finish() }
            title = getString(R.string.settings)
            setTitleTextColor(Color.WHITE)
        }
        supportFragmentManager.beginTransaction().replace(R.id.content, SettingsFragment())
            .commit()
    }

    class SettingsFragment : PreferenceFragmentCompat() {

        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            addPreferencesFromResource(R.xml.preferences)

            val lang: ListPreference = findPreference("Language")!!
            val language = when (LanguageSettings().getLang()) {
                "en" -> getString(R.string.en_lang)
                "de" -> getString(R.string.de_lang)
                else -> getString(R.string.ru_lang)
            }

            lang.summary = "${getString(R.string.current_language)} $language"
            val temp = lang.value
            lang.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
                val dialogBuilder =
                    activity?.let { AlertDialog.Builder(it, R.style.AlertDialogLight) }
                dialogBuilder!!.setMessage(getString(R.string.restart_txt))
                    .setPositiveButton(getString(R.string.restart)) { _, _ ->
                        activity?.let { LanguageSettings().setLocale(it, newValue.toString()) }
                        restartApp()
                    }
                    .setNegativeButton(getString(R.string.cancel)) { dialog, _ ->
                        lang.value = temp
                        dialog.cancel()
                    }
                dialogBuilder.create().apply {
                    show()
                }
                true
            }
        }

        private fun restartApp() {
            val intent = activity!!.packageManager
                .getLaunchIntentForPackage(activity!!.packageName)
            intent!!.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            activity!!.finish()
            startActivity(intent)
        }
    }

    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(LanguageSettings().onAttach(newBase))
    }
}

在 Lollipop 下,重启后会应用更改,这意味着重启后会更改应用语言。它也适用于 android 10。但这不适用于 Lollipop 和 Marshmallow(到目前为止已测试),但仅适用于 SettingsActivity 的语言更改,其他活动保持不变。可能在 Marshmallow 和 Android X 之间还有其他版本,它也适用。

不过,我想让它适用于所有版本,我的应用支持从 16 到最新版本的 SDK。

提前非常感谢。如果我不是那么精确并且您需要更多信息,请告诉我,我会尽力提供更多输入。同时,请原谅我不完美的英语。

我的LanguageSettings.kt 班级:

import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.util.Log
import androidx.preference.PreferenceManager
import java.util.*

@Suppress("DEPRECATION")
class LanguageSettings {

    fun onAttach(context: Context): Context {
        return setLocale(context, getPersistedData(context, getLang()!!)!!)
    }

    fun onAttach(context: Context, defaultLanguage: String): Context {
        return setLocale(context, getPersistedData(context, defaultLanguage)!!)
    }

    fun getLang(): String? {
        return when (Locale.getDefault().displayLanguage) {
            "English" -> "en"
            "Deutsch" -> "de"
            "русский" -> "ru"
            else -> "en"
        }
    }

    fun setLocale(context: Context, language: String): Context {
        persist(context, language)
        val configuration: Configuration
        val resources = context.resources
        val locale =
            Locale(language)
        Locale.setDefault(locale)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration = context.resources.configuration
            configuration.setLocale(locale)
            configuration.setLayoutDirection(locale)
            return context.createConfigurationContext(configuration)
        }
        configuration = resources.configuration
        configuration.locale = locale
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
            configuration.setLayoutDirection(locale)
        resources.updateConfiguration(configuration, resources.displayMetrics)
        return context
    }

    private fun getPersistedData(context: Context, defaultLanguage: String): String? {
        return PreferenceManager
            .getDefaultSharedPreferences(context)
            .getString("Language", defaultLanguage)
    }

    private fun persist(context: Context, language: String) {
        PreferenceManager
            .getDefaultSharedPreferences(context)
            .edit()
            .putString("Language", language)
            .apply()
    }
}

问题:这适用于 Android 4.2-4.4 及以上 7,但不适用于 Android 5.0/5.1/6(并且可能对于某些版本也不起作用,到目前为止没有测试所有版本,仅适用于以上提到的版本)。

Ans this my MyApp.kt class that extends Application is assigned in Manifest file (android:name=".MyApp"):

class MyApp : Application() {

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(LanguageSettings().onAttach(base, "en"))
    }
}

【问题讨论】:

  • 您好,感谢您的参考。但这对我没有帮助。我更新了我的问题并添加了更多信息和所涉及的课程。你能看看吗?谢谢。

标签: android activity-finish application-restart


【解决方案1】:

在测试了很多不同的案例后,我找到了解决方案:

private fun setLocale(context: Context, language: String): Context {
        val configuration: Configuration
        val resources = context.resources
        val locale = when (language) {
            "de" -> Locale("de", "DE")
            "ru" -> Locale("ru", "RU")
            "en" -> Locale("en", "US")
            else -> Locale("en", "US")
        }

        Locale.setDefault(locale)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration = context.resources.configuration
            configuration.setLocale(locale)
            configuration.setLayoutDirection(locale)
            return context.createConfigurationContext(configuration)
        }
        configuration = resources.configuration
        configuration.locale = locale
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
            configuration.setLayoutDirection(locale)
        resources.updateConfiguration(configuration, resources.displayMetrics)
        return context
    } 

这在 Kotlin 中非常适合我。

【讨论】:

  • 还有一点,resources.xxXXXX 必须用于启用在语言之间更改字符串值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-09
  • 2023-03-31
  • 1970-01-01
  • 2013-09-28
  • 2015-01-19
相关资源
最近更新 更多