问题描述
我正在尝试将“fa”设置为我的项目中的主要语言环境,将“en”设置为第二个语言环境。所以基本上我的主要 string.xml 文件是 "fa" 。支持 RTL 值为 "true" 和 Layout direction="locale"
应用程序不断向我显示从左到右的布局方向。
我的设备语言是美国英语,但我希望我的应用程序默认显示在“fa”中。
这是我的设置活动代码:
import android.app.Activity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.View
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.tron.khorshid.AppBaseActivity
import com.tron.khorshid.R
import com.tron.khorshid.khorshidApp
import com.tron.khorshid.adapter.RecyclerViewAdapter
import com.tron.khorshid.fragments.DashboardListFragment
import com.tron.khorshid.utils.Constants
import com.tron.khorshid.utils.Constants.SharedPref.LANGUAGE
import com.tron.khorshid.utils.SLocaleHelper
import com.tron.khorshid.utils.extensions.*
import kotlinx.android.synthetic.main.activity_settings.*
import kotlinx.android.synthetic.main.dialog_launguage_selection.*
import kotlinx.android.synthetic.main.spinner_language.view.*
import kotlinx.android.synthetic.main.toolbar.*
var mIsLanguageUpdated = false
class SettingActivity : AppBaseActivity() {
private lateinit var lan: String
private var codes = arrayOf(
"fa","en"
)
private var mCountryImg = intArrayOf(
R.drawable.fa,R.drawable.us
)
private var mIsSelectedByUser = false
private var mDashboardListFragment: DashboardListFragment? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
title = getString(R.string.title_setting)
setToolbar(toolbar)
toolbar.setNavigationOnClickListener { onBackpressed() }
lan = getAppLanguage()
val languages = resources.getStringArray(R.array.language)
switchNightMode.isChecked = khorshisApp.appTheme == Constants.THEME.DARK
mIsLanguageUpdated = false
val dialog = BottomSheetDialog(this)
dialog.setContentView(R.layout.dialog_launguage_selection)
val languageAdapter = RecyclerViewAdapter<String>(R.layout.spinner_language,onBind = { view: View,s: String,i: Int ->
view.ivlogo.setimageResource(mCountryImg[i])
view.tvName.text = languages[i]
})
languageAdapter.onItemClick = { i: Int,view: View,s: String ->
ivLanguage.loadImageFromDrawable(mCountryImg[i])
tvLanguage.text = languages[i]
dialog.dismiss()
setNewLocale(codes[i])
}
dialog.listLanguage.apply {
setVerticalLayout()
adapter = languageAdapter
}
languageAdapter.addItems(languages.toCollection(ArrayList()))
llLanguage.onClick {
dialog.show()
}
llDashboard.onClick {
if (mDashboardListFragment == null) {
mDashboardListFragment = DashboardListFragment.newInstance()
}
if (mDashboardListFragment?.isAdded!!) {
return@onClick
}
mDashboardListFragment?.show(supportFragmentManager,DashboardListFragment.tag)
}
codes.forEachIndexed { i: Int,s: String ->
if (lan == s) {
ivLanguage.loadImageFromDrawable(mCountryImg[i])
tvLanguage.text = languages[i]
}
}
switchNotification.setonCheckedchangelistener { _,isChecked ->
khorshidApp.getAppInstance().enableNotification(isChecked)
}
switchNightMode.setonCheckedchangelistener { _,isChecked ->
khorshidApp.changeAppTheme(isChecked)
switchToDarkMode(isChecked)
}
Handler().postDelayed({ mIsSelectedByUser = true },1000)
}
override fun onBackpressed() {
if (mIsLanguageUpdated) {
setResult(Activity.RESULT_OK)
finish()
} else {
super.onBackpressed()
}
/*if (lan != khorshidApp.language) {
launchActivityWithNewTask<DashBoardActivity>()
exitProcess(0)
} else {
super.onBackpressed()
}*/
}
private fun setNewLocale(language: String) {
Log.e("lan",language)
mIsLanguageUpdated = true
getSharedPrefInstance().setValue(LANGUAGE,language)
SLocaleHelper.setLocale(this,language);
recreate()
}
}
和区域设置代码:
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import android.os.LocaleList
import android.preference.PreferenceManager
import java.util.LinkedHashSet
import java.util.Locale
import androidx.annotation.RequiresApi
import android.os.Build.VERSION_CODES.JELLY_BEAN_MR1
import android.os.Build.VERSION_CODES.N
import com.tron.khorshid.utils.extensions.getSharedPrefInstance
class LocaleManager(context: Context) {
val language: String?
get() = getSharedPrefInstance().getStringValue(LANGUAGE_KEY,LANGUAGE_PERSIAN)
/*fun setLocale(c: Context): Context {
return updateResources(c,language)
}
fun setNewLocale(c: Context,language: String): Context {
persistLanguage(language)
return updateResources(c,language)
}
*/
@SuppressLint("ApplySharedPref")
private fun persistLanguage(language: String) {
// use commit() instead of apply(),because sometimes we kill the application process
// immediately that prevents apply() from finishing
getSharedPrefInstance().setValue(LANGUAGE_KEY,language)
}
/*
private fun updateResources(context: Context,language: String?): Context {
var context = context
val locale = Locale(language)
Locale.setDefault(locale)
val res = context.resources
val config = Configuration(res.configuration)
context = if (Build.VERSION.SDK_INT >= N) {
setLocaleForApi24(config,locale)
context.createConfigurationContext(config)
} else {
config.setLocale(locale)
context.createConfigurationContext(config)
}
return context
}
*/
/*
@RequiresApi(api = N)
private fun setLocaleForApi24(config: Configuration,target: Locale) {
val set = LinkedHashSet<Locale>()
// bring the target locale to the front of the list
set.add(target)
val all = LocaleList.getDefault()
for (i in 0 until all.size()) {
// append other locales supported by the user
set.add(all.get(i))
}
val locales = set.toTypedArray()
config.locales = LocaleList(*locales)
}
*/
companion object {
val LANGUAGE_PERSIAN = "fa"
val LANGUAGE_ENGLISH = "en"
val LANGUAGE_UKRAINIAN = "uk"
val LANGUAGE_RUSSIAN = "ru"
private val LANGUAGE_KEY = "language_key"
fun getLocale(res: Resources): Locale {
val config = res.configuration
return if (Build.VERSION.SDK_INT >= N) config.locales.get(0) else config.locale
}
}
}
非常感谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)