RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1149418
Accepted
Anton Pankratov
Anton Pankratov
Asked:2020-07-06 13:07:29 +0000 UTC2020-07-06 13:07:29 +0000 UTC 2020-07-06 13:07:29 +0000 UTC

选择不同的应用区域设置后无法翻译后面的片段

  • 772

遇到这样的问题。在我的应用程序中,我实现了本地化,但它应该与系统的语言环境分开为我工作。我在教程的帮助下编写了 LocaleHelper 类

import android.content.Context
import android.os.Build
import com.example.core.utils.preferences.AppSharedPreferences
import timber.log.Timber
import java.util.*

class LocaleHelper(private val appPreferences: AppSharedPreferences) {

   fun onAttach(context: Context): Context? {
       val lang = getPersistedData(Locale.getDefault().language)
       Timber.d("Locale/LocaleHelper.onAttach.lang = $lang")
       return setLocale(context, lang!!)
   }

    fun onAttach(context: Context, locale: String): Context? {
        val lang = getPersistedData(locale)
        return setLocale(context, lang!!)
    }

    fun setLocale(context: Context, language: String): Context? {
        persist(language)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, language)
        }
        return updateResourcesLegacy(context, language)
    }

    private fun getPersistedData(locale: String): String? {
        Timber.d("Locale/LocaleHelper.getPersistData.lang = $locale")
        return appPreferences.loadChangingByUserLocale(locale)
    }

    private fun persist(language: String) {
        appPreferences.saveChangingByUserLocale(language)
    }

    private fun updateResources(context: Context, language: String): Context? {
        val appLocale = Locale(language)
        val configuration = context.resources.configuration

        Locale.setDefault(appLocale)
        configuration.apply {
            setLocale(appLocale)
            setLayoutDirection(appLocale)
        }
        return context.createConfigurationContext(configuration)
    }

    @Suppress("DEPRECATION")
    private fun updateResourcesLegacy(context: Context, language: String): Context? {
        val appLocale = Locale(language)
        val resource = context.resources
        val configuration = resource.configuration

        Locale.setDefault(appLocale)
        configuration.locale = appLocale
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            configuration.setLayoutDirection(appLocale)
        }
        resource.updateConfiguration(configuration, resource.displayMetrics)
        return context
    }
}

在主页上,我制作了一个下拉列表,点击即可打开

私人乐趣 preparePopupWindowLangs(): PopupWindow? { val popup = PopupWindow(view!!.context).apply { width = 250 height = WindowManager.LayoutParams.WRAP_CONTENT isOutsideTouchable = true isFocusable = true contentView = contentList() } logger.preparePopupWindow() 返回弹出窗口 }

    private fun contentList(): ListView {
        val localesAdapter = LanguagesPopupAdapter(context!!, prepareLocalesList(viewModel.localesNames))
        localesAdapter.notifyDataSetChanged()
        val listView = ListView(context).apply {
            adapter = localesAdapter
            onItemClickListener = popupOnItemClick()
        }
        return listView
    }

    private fun prepareLocalesList(@StringRes array: Array<Int>): List<String> {
        val locales: MutableList<String> = mutableListOf()
        array.forEachIndexed { index, locale ->
            locales.add(index, resources.getString(locale))
        }
        return locales
    }

    private fun popupOnItemClick(): OnItemClickListener {
        val itemClick = OnItemClickListener { parent, view, position, id ->
            val fadeInAnimation: Animation =
                AnimationUtils.loadAnimation(context, R.anim.fragment_fade_enter)
            fadeInAnimation.duration = 10
            view!!.startAnimation(fadeInAnimation)
            val selectedItemText = view.findViewById<TextView>(R.id.language_tv).text.toString()
            langs_popup.text = selectedItemText
            popup!!.dismiss()
            changeLocale(viewModel.localesIndexes[position])
        }
        return itemClick
    }

通过从列表中选择,我更改了文本

 private fun changeTextAfterLanguageSelection(lang: String) {
        val context = viewModel.localeHelper.setLocale(context!!, lang)
        val resources = context!!.resources
        login_tv.text = resources.getString(R.string.field_header_login)
        login_et.hint = resources.getString(R.string.hint_login)
        password_tv.text = resources.getString(R.string.field_header_password)
        password_et.hint = resources.getString(R.string.hint_password)
        auth_progress_button.setButtonName(context, R.string.btn_enter)
        prepareLocalesList(viewModel.localesNames)
        setPopupTitleText()
    }

我还添加了一种方法来更改片段中的文本

override fun onAttach(context: Context) {
        super.onAttach(viewModel.localeHelper.onAttach(context)!!)
    }

添加了在活动中为应用程序设置区域设置的方法

 override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(localeHelper.onAttach(newBase!!, appPreferences.loadChangingByUserLocale("ru")!!)))
   }

结果,我得到以下信息:在具有 API 22 的模拟器中,一切正常,但已经在 API 29 上,当切换到另一个片段时,不执行翻译,而只执行应用程序时的语言环境已推出作品。我知道选择语言时可以选择重新启动活动,但这种方法不适合我的任务。我正在实施 SingleActivity 技术。

android
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Спицко Дмитрий
    2020-07-06T17:48:42Z2020-07-06T17:48:42Z

    我试图弄清楚你的代码中发生了什么,但结果并不像我想的那么容易。许多功能完全不清楚它们是从哪里获取的以及为什么给出它们......

    我在 SingleActivity 应用程序中根据用户的选择进行了语言更改。这很简单。语言选择后的代码:

    Configuration config = new Configuration();
    config.locale = newLocale;
    context.getResources ().updateConfiguration(config, context.getResources().getDisplayMetrics ());
    Intent intent = activity.getIntent ();
    activity.finish ();
    context.startActivity (intent);
    

    是的,我们正在重新创建活动,这意味着我们必须正确保存当前状态,并且在一个活动和片段的情况下,在我看来,这非常简单

    • 1

相关问题

  • 来自片段的列表落后于 BottomNavigationView

  • 无法将变量从 Activity 传递到 Fragment

  • 构建与完成的片段略有不同的片段的最佳方法是什么?

  • 如何更改来自服务器的响应中的日期格式?

  • 谷歌地图在应用程序的发布版本中不起作用

  • 材料设计按钮。单击按钮上的可选区域!

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5