Android应用中设置非系统默认语言(使用Kotlin)

[复制链接]
发表于 2025-7-27 05:41:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
以下是使用Kotlin在Android应用中设置非系统默认语言的几种方法:
方法1:通过扩展函数动态更改语言

  1. fun Context.setAppLocale(languageCode: String): Context {
  2.     val locale = Locale(languageCode)
  3.     Locale.setDefault(locale)
  4.    
  5.     return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  6.         updateResourcesLocale(locale)
  7.     } else {
  8.         updateResourcesLocaleLegacy(locale)
  9.     }
  10. }
  11. @RequiresApi(Build.VERSION_CODES.N)
  12. private fun Context.updateResourcesLocale(locale: Locale): Context {
  13.     val configuration = resources.configuration
  14.     configuration.setLocale(locale)
  15.     return createConfigurationContext(configuration)
  16. }
  17. @Suppress("DEPRECATION")
  18. private fun Context.updateResourcesLocaleLegacy(locale: Locale): Context {
  19.     val resources = resources
  20.     val configuration = resources.configuration
  21.     configuration.locale = locale
  22.     resources.updateConfiguration(configuration, resources.displayMetrics)
  23.     return this
  24. }
复制代码
在Activity中使用:
  1. // 设置为法语
  2. val newContext = context.setAppLocale("fr")
  3. // 需要重启Activity使更改生效
  4. recreate()
复制代码
方法2:在Application类中初始化语言


  1. class MyApp : Application() {
  2.     override fun onCreate() {
  3.         super.onCreate()
  4.         // 从SharedPreferences读取用户选择的语言
  5.         val lang = getPreferredLanguage()
  6.         setAppLocale(lang)
  7.     }
  8.    
  9.     private fun getPreferredLanguage(): String {
  10.         // 从SharedPreferences获取语言设置
  11.         val prefs = PreferenceManager.getDefaultSharedPreferences(this)
  12.         return prefs.getString("app_language", "en") ?: "en"
  13.     }
  14.    
  15.     private fun setAppLocale(languageCode: String) {
  16.         val resources = resources
  17.         val config = resources.configuration
  18.         val locale = Locale(languageCode)
  19.         Locale.setDefault(locale)
  20.         
  21.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  22.             config.setLocale(locale)
  23.         } else {
  24.             @Suppress("DEPRECATION")
  25.             config.locale = locale
  26.         }
  27.         
  28.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  29.             createConfigurationContext(config)
  30.         }
  31.         
  32.         @Suppress("DEPRECATION")
  33.         resources.updateConfiguration(config, resources.displayMetrics)
  34.     }
  35. }
复制代码

方法3:使用BaseActivity管理语言

  1. open class BaseActivity : AppCompatActivity() {
  2.     override fun attachBaseContext(newBase: Context) {
  3.         // 从SharedPreferences获取语言设置
  4.         val language = newBase.getPreferredLanguage()
  5.         super.attachBaseContext(newBase.setAppLocale(language))
  6.     }
  7. }
  8. // 然后让所有Activity继承自BaseActivity
  9. class MainActivity : BaseActivity() {
  10.     // ...
  11. }
  12. // SharedPreferences扩展函数
  13. fun Context.getPreferredLanguage(): String {
  14.     val prefs = PreferenceManager.getDefaultSharedPreferences(this)
  15.     return prefs.getString("app_language", "en") ?: "en"
  16. }
复制代码
切换语言的实用函数

  1. fun Activity.changeAppLanguage(languageCode: String) {
  2.     // 保存语言设置
  3.     val prefs = PreferenceManager.getDefaultSharedPreferences(this)
  4.     prefs.edit().putString("app_language", languageCode).apply()
  5.    
  6.     // 更新应用上下文
  7.     val newContext = setAppLocale(languageCode)
  8.    
  9.     // 重启当前Activity
  10.     recreate()
  11.    
  12.     // 如果需要更新所有Activity,可以发送广播通知其他Activity重启
  13.     sendBroadcast(Intent("LANGUAGE_CHANGED"))
  14. }
复制代码
留意事项


资源文件结构:
  1. res/
  2.   values/           # 默认资源 (英语)
  3.   values-fr/        # 法语资源
  4.   values-es/        # 西班牙语资源
  5.   values-zh/        # 中文资源
复制代码

这些方法可以让你在Android应用中独立于系统设置使用特定的语言。

  • 语言代码格式:

    • 使用ISO 639-1语言代码(如"en", "fr", "es")
    • 对于特定地区的变体,可以使用格式如"zh-rCN"(简体中文)、"zh-rTW"(繁体中文)

  • 兼容性处置惩罚:

    • 对于Android 7.0及以上版本使用新的API
    • 对于旧版本使用兼容方法

  • Activity生命周期:

    • 更改语言后通常需要重启Activity才能看到效果
    • 可以使用recreate()方法重启当前Activity

  • 长期化存储:

    • 使用SharedPreferences存储用户的语言选择
    • 每次应用启动时读取设置并应用

  • 这些方法可以让你在Android应用中独立于系统设置使用特定的语言。
Android应用中设置非系统默认语言(java)-CSDN博客
Jetpack Compose 中更新应用语言-CSDN博客
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表