语言切换
This commit is contained in:
@@ -162,11 +162,10 @@ object ApiConfig {
|
||||
fun createHeadersInterceptor() = Interceptor { chain ->
|
||||
val requestBuilder = chain.request().newBuilder()
|
||||
val token = authToken
|
||||
val langHeader = LanguageHeaderManager.getLanguageHeaderValue()
|
||||
|
||||
// 添加必要的请求头
|
||||
requestBuilder.header("clientid", "428a8310cd442757ae699df5d894f051")
|
||||
requestBuilder.header("content-language", langHeader)
|
||||
requestBuilder.header("content-language", LanguageUtil.getLanguageHeaderValue())
|
||||
|
||||
// 如果有 token,添加 Authorization 头
|
||||
if (token.isNotBlank()) {
|
||||
|
||||
@@ -1,37 +1,13 @@
|
||||
package com.example.defenseapplication.utils
|
||||
|
||||
import android.content.Context
|
||||
import androidx.core.content.edit
|
||||
|
||||
enum class AppLanguage(val code: String) {
|
||||
ENGLISH("en_US"),
|
||||
SIMPLIFIED_CHINESE("zh_CN"),
|
||||
TRADITIONAL_CHINESE("zh_TW")
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP 请求头的语言值管理,统一由 LanguageUtil 提供
|
||||
* @deprecated Use LanguageUtil.getLanguageHeaderValue() directly
|
||||
*/
|
||||
object LanguageHeaderManager {
|
||||
private const val PREF_NAME = "app_lang_pref"
|
||||
private const val KEY_LANGUAGE = "selected_language"
|
||||
|
||||
private var currentLanguage: AppLanguage = AppLanguage.ENGLISH
|
||||
|
||||
fun init(context: Context) {
|
||||
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
val langCode = prefs.getString(KEY_LANGUAGE, AppLanguage.ENGLISH.code)
|
||||
currentLanguage = AppLanguage.values().find { it.code == langCode } ?: AppLanguage.ENGLISH
|
||||
}
|
||||
|
||||
fun getCurrentLanguage(): AppLanguage = currentLanguage
|
||||
|
||||
fun switchLanguage(context: Context, language: AppLanguage) {
|
||||
currentLanguage = language
|
||||
// 持久化
|
||||
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit {
|
||||
putString(KEY_LANGUAGE, language.code)
|
||||
apply()
|
||||
}
|
||||
// 可选:更新应用 Locale、重启 Activity 等
|
||||
}
|
||||
|
||||
fun getLanguageHeaderValue(): String = currentLanguage.code
|
||||
@Deprecated("Use LanguageUtil.getLanguageHeaderValue() instead", ReplaceWith("LanguageUtil.getLanguageHeaderValue()"))
|
||||
fun getLanguageHeaderValue(): String = LanguageUtil.getLanguageHeaderValue()
|
||||
}
|
||||
@@ -3,77 +3,128 @@ package com.example.defenseapplication.utils
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.content.res.Configuration
|
||||
import android.content.res.Resources
|
||||
import android.os.Build
|
||||
import android.os.LocaleList
|
||||
import java.util.Locale
|
||||
|
||||
enum class AppLanguage(val code: String, val locale: Locale) {
|
||||
ENGLISH("en_US", Locale.US),
|
||||
SIMPLIFIED_CHINESE("zh_CN", Locale.SIMPLIFIED_CHINESE);
|
||||
|
||||
companion object {
|
||||
fun fromCode(code: String): AppLanguage {
|
||||
return values().find { it.code == code } ?: SIMPLIFIED_CHINESE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object LanguageUtil {
|
||||
|
||||
private const val PREF_NAME = "language_pref"
|
||||
private const val KEY_LANGUAGE = "selected_language"
|
||||
|
||||
const val LANGUAGE_CHINESE = "zh"
|
||||
const val LANGUAGE_ENGLISH = "en"
|
||||
@JvmStatic
|
||||
var currentLanguage: AppLanguage = AppLanguage.SIMPLIFIED_CHINESE
|
||||
private set
|
||||
|
||||
/**
|
||||
* 切换语言
|
||||
* @param context 上下文
|
||||
* @param language 语言代码: "zh" 中文, "en" 英文
|
||||
* 初始化语言,从持久化中恢复
|
||||
*/
|
||||
fun init(context: Context) {
|
||||
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
var code = prefs.getString(KEY_LANGUAGE, null)
|
||||
if (code == null) {
|
||||
// 兼容旧数据:尝试从 app_lang_pref 读取(LanguageHeaderManager 旧存储路径)
|
||||
val oldPrefs = context.getSharedPreferences("app_lang_pref", Context.MODE_PRIVATE)
|
||||
code = oldPrefs.getString("selected_language", AppLanguage.SIMPLIFIED_CHINESE.code)
|
||||
?: AppLanguage.SIMPLIFIED_CHINESE.code
|
||||
// 迁移到新存储路径
|
||||
prefs.edit().putString(KEY_LANGUAGE, code).apply()
|
||||
}
|
||||
currentLanguage = AppLanguage.fromCode(code)
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换语言,同时更新 app locale 和 HTTP header
|
||||
* @param language AppLanguage.ENGLISH 或 AppLanguage.SIMPLIFIED_CHINESE
|
||||
*/
|
||||
fun switchLanguage(context: Context, language: AppLanguage) {
|
||||
currentLanguage = language
|
||||
saveLanguage(context, language.code)
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容旧代码:使用字符串 "zh" / "en" 切换
|
||||
*/
|
||||
fun switchLanguage(context: Context, language: String) {
|
||||
saveLanguage(context, language)
|
||||
val appLang = when (language) {
|
||||
"zh" -> AppLanguage.SIMPLIFIED_CHINESE
|
||||
else -> AppLanguage.ENGLISH
|
||||
}
|
||||
switchLanguage(context, appLang)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前设置的语言
|
||||
* 获取当前设置的语言代码 "zh" / "en"
|
||||
*/
|
||||
fun getCurrentLanguage(context: Context): String {
|
||||
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
return prefs.getString(KEY_LANGUAGE, LANGUAGE_CHINESE) ?: LANGUAGE_CHINESE
|
||||
return currentLanguage.name.lowercase().let {
|
||||
if (it.contains("chinese")) "zh" else "en"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 HTTP header 的语言值 "en_US" / "zh_CN"
|
||||
*/
|
||||
fun getLanguageHeaderValue(): String = currentLanguage.code
|
||||
|
||||
/**
|
||||
* 保存语言设置
|
||||
*/
|
||||
private fun saveLanguage(context: Context, language: String) {
|
||||
private fun saveLanguage(context: Context, languageCode: String) {
|
||||
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
prefs.edit().putString(KEY_LANGUAGE, language).apply()
|
||||
prefs.edit().putString(KEY_LANGUAGE, languageCode).apply()
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前是否是中文
|
||||
*/
|
||||
fun isChinese(context: Context): Boolean {
|
||||
return getCurrentLanguage(context) == LANGUAGE_CHINESE
|
||||
return currentLanguage == AppLanguage.SIMPLIFIED_CHINESE
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本地化的 Context(用于在应用内切换语言)
|
||||
* 在 attachBaseContext 中调用
|
||||
* 获取本地化的 Context(在 attachBaseContext 中调用)
|
||||
*/
|
||||
fun attachBaseContext(context: Context): Context {
|
||||
val language = getCurrentLanguage(context)
|
||||
return createLocalizedContext(context, language)
|
||||
val locale = getSavedLocale(context)
|
||||
return createLocalizedContext(context, locale)
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取已保存的语言 locale(attachBaseContext 中使用,此时 currentLanguage 可能尚未初始化)
|
||||
*/
|
||||
private fun getSavedLocale(context: Context): Locale {
|
||||
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
val code = prefs.getString(KEY_LANGUAGE, AppLanguage.SIMPLIFIED_CHINESE.code)
|
||||
?: AppLanguage.SIMPLIFIED_CHINESE.code
|
||||
return AppLanguage.fromCode(code).locale
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建本地化的 Context
|
||||
*/
|
||||
private fun createLocalizedContext(context: Context, language: String): Context {
|
||||
val locale = Locale(language)
|
||||
private fun createLocalizedContext(context: Context, locale: Locale): Context {
|
||||
Locale.setDefault(locale)
|
||||
|
||||
val resources = context.resources
|
||||
val configuration = resources.configuration
|
||||
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
// Android 7.0 及以上
|
||||
val localeList = LocaleList(locale)
|
||||
configuration.setLocales(localeList)
|
||||
context.createConfigurationContext(configuration)
|
||||
} else {
|
||||
// Android 7.0 以下
|
||||
configuration.locale = locale
|
||||
resources.updateConfiguration(configuration, resources.displayMetrics)
|
||||
context
|
||||
@@ -84,8 +135,7 @@ object LanguageUtil {
|
||||
* 更新应用语言(用于在 Activity 中动态切换)
|
||||
*/
|
||||
fun updateResources(context: Context): Context {
|
||||
val language = getCurrentLanguage(context)
|
||||
val locale = Locale(language)
|
||||
val locale = currentLanguage.locale
|
||||
Locale.setDefault(locale)
|
||||
|
||||
val resources = context.resources
|
||||
@@ -107,8 +157,8 @@ object LanguageUtil {
|
||||
*/
|
||||
fun getLanguageDisplayName(language: String): String {
|
||||
return when (language) {
|
||||
LANGUAGE_CHINESE -> "中文"
|
||||
LANGUAGE_ENGLISH -> "English"
|
||||
"zh" -> "中文"
|
||||
"en" -> "English"
|
||||
else -> "中文"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user