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.configurationconfiguration.setLocale(locale)configuration.setLocales(LocaleList(locale))return context.createConfigurationContext(configuration)}/*** 動態更新 Context 的資源配置*/fun updateResources(context: Context, locale: Locale): Context {val resources = context.resourcesval configuration = resources.configurationif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {configuration.setLocale(locale)configuration.setLocales(LocaleList(locale))return context.createConfigurationContext(configuration)} else {configuration.locale = localeresources.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}
}