实现多语言适配
1.在res下创建多语言资源文件:
2.选择需要的语言
然后得到多种语言适配string文件:
3.代码设置多语言
object LanguageHelper {
/**
* 获取适配的 Context
*/
fun getAttachBaseContext(context: Context): Context {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setAppLanguageApi24(context)
} else {
setAppLanguage(context)
context
}
}
/**
* 获取当前系统语言,如未包含则默认中文
*/
private fun getSystemLocal(): Locale {
val systemLocal = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList.getDefault()[0]
} else {
Locale.getDefault()
}
return when (systemLocal.language) {
Locale.CHINA.language -> Locale.CHINA // 简体中文
Locale.TAIWAN.language -> Locale.TAIWAN // 台湾繁体
"zh" -> when (systemLocal.country) { // 修复拼写错误
"HK" -> Locale("zh", "HK") // 香港繁体
else -> Locale.CHINA // 默认简体中文
}
Locale.ENGLISH.language -> Locale.ENGLISH // 英文
else -> Locale.CHINA // 默认简体中文
}
}
/**
* 兼容 7.0 以上
*/
@TargetApi(Build.VERSION_CODES.N)
private fun setAppLanguageApi24(context: Context): Context {
val locale = getSystemLocal()
val configuration = context.resources.configuration
configuration.setLocale(locale)
configuration.setLocales(LocaleList(locale))
return context.createConfigurationContext(configuration)
}
/**
* 动态更新 Context 的资源配置
*/
fun updateResources(context: Context, locale: Locale): Context {
val resources = context.resources
val configuration = resources.configuration
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(locale)
configuration.setLocales(LocaleList(locale))
return context.createConfigurationContext(configuration)
} else {
configuration.locale = locale
resources.updateConfiguration(configuration, resources.displayMetrics)
}
return context
}
/**
* 设置应用语言
*/
private fun setAppLanguage(context: Context) {
val locale = getSystemLocal()
updateResources(context, locale)
}
}
4.使用示例
1.在 Application 中使用
在 Application
类的 attachBaseContext
方法中调用 LanguageHelper.getAttachBaseContext
,以确保应用启动时使用正确的语言环境。
class MyApplication : Application() {
override fun attachBaseContext(base: Context) {
super.attachBaseContext(LanguageHelper.getAttachBaseContext(base))
}
}
2. 在 Activity 中动态切换语言
在设置页面中,当用户选择语言后,调用 LanguageHelper.updateResources
方法更新当前 Activity
的资源配置,并刷新界面。
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
// 假设有一个语言选择的下拉菜单
val languageSpinner = findViewById<Spinner>(R.id.languageSpinner)
val languages = arrayOf("简体中文", "繁體中文(台灣)", "繁體中文(香港)", "English")
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, languages)
languageSpinner.adapter = adapter
// 监听语言选择
languageSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val selectedLanguage = languages[position]
when (selectedLanguage) {
"简体中文" -> setAppLanguage(Locale.CHINA)
"繁體中文(台灣)" -> setAppLanguage(Locale.TAIWAN)
"繁體中文(香港)" -> setAppLanguage(Locale("zh", "HK"))
"English" -> setAppLanguage(Locale.ENGLISH)
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
}
private fun setAppLanguage(locale: Locale) {
// 更新应用语言
LanguageHelper.updateResources(this, locale)
recreate() // 刷新当前 Activity
}
}