在Kotlin中使用其他语言环境

时间:2018-11-10 13:25:20

标签: android string kotlin

我想使用字符串,lang英语和阿拉伯语向我的应用添加语言。我的应用程序是通过数据json特定机场的航班时刻表。和我的应用程序正常工作而没有问题。我想将语言从数据json转换为阿拉伯语

en中的字符串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内添加语言

my app

2 个答案:

答案 0 :(得分:0)

将此标记添加到清单中的“应用程序标签”下。

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(this@LoginCustomerFragment.activity?.applicationContext)
            var mCurrentLanguage = LocaleManagerMew.getCurrentLanguage(this@LoginCustomerFragment.activity?.applicationContext)
            if (mCurrentLanguage == LocaleManagerMew.mArabicFlag) {
                LocaleManagerMew.setNewLocale(this@LoginCustomerFragment.context!!, LocaleManagerMew.mEnglishFlag)
            } else if (mCurrentLanguage == LocaleManagerMew.mEnglishFlag) {
                LocaleManagerMew.setNewLocale(this@LoginCustomerFragment.context!!, LocaleManagerMew.mArabicFlag)
            }
            activity?.recreate()
        }
    }
}

答案 1 :(得分:0)

您可以尝试以下方法:

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