我想添加语言到我的应用程序使用字符串,朗英语和阿拉伯语。我的应用程序是通过数据json的特定机场的航班时刻表。和我的应用程序正常工作没有问题。我希望改变语言来自数据json到阿拉伯语
字符串res和
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="schedule">schedule</string>
<string name="arrival">arrival</string>
<string name="departed">departed</string>
<string name="cancelled">cancelled</string>
</resources>
字符串res在阿拉伯语中
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="schedule">مجدولة</string>
<string name="arrival">وصلت</string>
<string name="departed">غادرت</string>
<string name="cancelled">الغيت</string>
</resources>
我希望在我的列表中使用这些资源,因为我在我的应用程序中使用了列表视图
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view : View = LayoutInflater.from(context).inflate(R.layout.row_layout,parent,false)
val code = view.findViewById(R.id.code_id) as AppCompatTextView
val status = view.findViewById(R.id.status_id) as AppCompatTextView
val TimeFlight = view.findViewById(R.id.time_id) as AppCompatTextView
val LogoAriline = view.findViewById(R.id.logo_image) as ImageView
CallsingID.text = list[position].Callsign
AirlineID.text = list[position].Airline
code.text = list[position].code
status.text= list[position].status
TimeFlight.text = getDateTime(list[position].TimeFlight)
Picasso.get().load(Uri.parse("https://www.xxxxxxx.com/static/images/data/operators/"+status.text.toString()+"_logo0.png"))
.into(LogoAriline)
return view as View
}
我想在status.text= list[position].status
中添加语言
在Application Tag下的清单中添加此标志。
android:supportsRtl="true"
区域设置管理实用程序类
object LocaleManagerMew {
val SELECTED_LANGUAGE = "MEW_CURRENT_USER_LANGUAGE"
var mSharedPreference: SharedPreferences? = null
var mEnglishFlag = "en"
var mArabicFlag = "ar"
fun setLocale(context: Context?): Context {
return updateResources(context!!, getCurrentLanguage(context)!!)
}
inline fun setNewLocale(context: Context, language: String) {
persistLanguagePreference(context, language)
updateResources(context, language)
}
inline fun getCurrentLanguage(context: Context?): String? {
var mCurrentLanguage: String?
if (mSharedPreference == null)
mSharedPreference = PreferenceHelper.defaultPrefs(context!!)
mCurrentLanguage = mSharedPreference!![SELECTED_LANGUAGE]
return mCurrentLanguage
}
fun persistLanguagePreference(context: Context, language: String) {
if (mSharedPreference == null)
mSharedPreference = PreferenceHelper.defaultPrefs(context)
mSharedPreference!![SELECTED_LANGUAGE] = language
}
fun updateResources(context: Context, language: String): Context {
var contextFun = context
var locale = Locale(language)
Locale.setDefault(locale)
var resources = context.resources
var configuration = Configuration(resources.configuration)
if (Build.VERSION.SDK_INT >= 17) {
configuration.setLocale(locale)
contextFun = context.createConfigurationContext(configuration)
} else {
configuration.locale = locale
resources.updateConfiguration(configuration, resources.getDisplayMetrics())
}
return contextFun
}
}
应用类代码
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
LocaleManagerMew.setLocale(this)
Log.d(MewConstants.mewLogs, "onConfigurationChanged: " + newConfig.locale.getLanguage())
}
基础活动
abstract class BaseActivity : AppCompatActivity(){
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(LocaleManagerMew.setLocale(base))
}
}
语言更改按钮单击活动中的监听器
override fun onClick(p0: View?) {
when (p0?.id) {
R.id.switchLanguage -> {
//LocaleManagerMew.setLocale([email protected]?.applicationContext)
var mCurrentLanguage = LocaleManagerMew.getCurrentLanguage([email protected]?.applicationContext)
if (mCurrentLanguage == LocaleManagerMew.mArabicFlag) {
LocaleManagerMew.setNewLocale([email protected]!!, LocaleManagerMew.mEnglishFlag)
} else if (mCurrentLanguage == LocaleManagerMew.mEnglishFlag) {
LocaleManagerMew.setNewLocale([email protected]!!, LocaleManagerMew.mArabicFlag)
}
activity?.recreate()
}
}
}
您可以尝试以下方面的内容:
code.text = context.getString(
when (list[position].code) {
"schedule" -> R.string.schedule
"arrival" -> R.string.arrival
"departed" -> R.string.departed
"cancelled" -> R.string.cancelled
else -> TODO("This is an error")
}
)