语言选择
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
package com.ckkj.water.utils.manager;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
import androidx.core.os.LocaleListCompat;
|
||||
|
||||
import com.ckkj.water.AppConfig;
|
||||
import com.ckkj.water.ConstantUtil;
|
||||
import com.ckkj.water.MyApplication;
|
||||
import com.ckkj.water.R;
|
||||
import com.ckkj.water.login.LoginActivity;
|
||||
import com.ckkj.water.main.activity.MainActivity;
|
||||
import com.ckkj.water.utils.dataUtil.SharePreUtil;
|
||||
import com.ckkj.water.utils.show.ToastUtil;
|
||||
|
||||
/**
|
||||
* 语言切换管理工具类
|
||||
* 使用 AppCompatDelegate.setApplicationLocales() API 实现中英切换
|
||||
*/
|
||||
public class LanguageManager {
|
||||
|
||||
private static final String TAG = "LanguageManager";
|
||||
|
||||
/**
|
||||
* 语言类型枚举
|
||||
*/
|
||||
public enum LanguageType {
|
||||
CHINESE(0, "zh", "中文"),
|
||||
ENGLISH(1, "en", "English");
|
||||
|
||||
public final int index;
|
||||
public final String languageTag;
|
||||
public final String displayName;
|
||||
|
||||
LanguageType(int index, String languageTag, String displayName) {
|
||||
this.index = index;
|
||||
this.languageTag = languageTag;
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public static LanguageType fromIndex(int index) {
|
||||
for (LanguageType type : values()) {
|
||||
if (type.index == index) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return CHINESE;
|
||||
}
|
||||
|
||||
public static LanguageType fromLanguageTag(String languageTag) {
|
||||
if ("en".equals(languageTag)) {
|
||||
return ENGLISH;
|
||||
}
|
||||
return CHINESE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前语言类型
|
||||
*/
|
||||
public static LanguageType getCurrentLanguageType() {
|
||||
LocaleListCompat locales = AppCompatDelegate.getApplicationLocales();
|
||||
if (locales.isEmpty()) {
|
||||
return LanguageType.CHINESE;
|
||||
}
|
||||
String language = locales.get(0).getLanguage();
|
||||
return LanguageType.fromLanguageTag(language);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前语言索引 (0: 中文, 1: 英文)
|
||||
*/
|
||||
public static int getCurrentLanguageIndex() {
|
||||
return getCurrentLanguageType().index;
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换语言
|
||||
* @param languageType 要切换到的语言类型
|
||||
*/
|
||||
public static void switchLanguage(LanguageType languageType) {
|
||||
switchLanguage(languageType, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换语言并重启应用
|
||||
* @param languageType 要切换到的语言类型
|
||||
* @param activity 触发切换的Activity(用于重启)
|
||||
*/
|
||||
public static void switchLanguage(LanguageType languageType, Activity activity) {
|
||||
// 先保存语言设置
|
||||
saveLanguage(languageType.languageTag);
|
||||
|
||||
// 设置应用语言
|
||||
LocaleListCompat localeList = LocaleListCompat.forLanguageTags(languageType.languageTag);
|
||||
AppCompatDelegate.setApplicationLocales(localeList);
|
||||
|
||||
// 如果传入了Activity,则重启应用
|
||||
if (activity != null) {
|
||||
ToastUtil.showShort(activity, activity.getString(R.string.language_switch_success));
|
||||
|
||||
// 延迟重启应用,确保语言设置生效
|
||||
new android.os.Handler(android.os.Looper.getMainLooper()).postDelayed(() -> {
|
||||
try {
|
||||
boolean isLogin = AppConfig.USER_LOGIN || SharePreUtil.getBoolean(
|
||||
ConstantUtil.SHARE_PRE_NAME,
|
||||
activity,
|
||||
ConstantUtil.USER_LOGIN
|
||||
);
|
||||
ActivityManager.removeAllActivity();
|
||||
Intent intent = new Intent(activity, isLogin ? MainActivity.class : LoginActivity.class);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
activity.startActivity(intent);
|
||||
System.exit(0);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换语言(通过索引)
|
||||
* @param position 0: 中文, 1: 英文
|
||||
*/
|
||||
public static void switchLanguageByIndex(int position) {
|
||||
switchLanguage(LanguageType.fromIndex(position), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换语言并重启应用(通过索引)
|
||||
* @param position 0: 中文, 1: 英文
|
||||
* @param activity 触发切换的Activity
|
||||
*/
|
||||
public static void switchLanguageByIndex(int position, Activity activity) {
|
||||
switchLanguage(LanguageType.fromIndex(position), activity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存语言设置到本地
|
||||
*/
|
||||
public static void saveLanguage(String languageTag) {
|
||||
SharePreUtil.putString(ConstantUtil.SHARE_PRE_NAME,
|
||||
MyApplication.getContext(),
|
||||
ConstantUtil.KEY_APP_LANGUAGE,
|
||||
languageTag);
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用启动时恢复保存的语言设置
|
||||
* 应该在 Application.onCreate() 中调用
|
||||
*/
|
||||
public static void applyStoredLocale() {
|
||||
String savedLocale = SharePreUtil.getString(
|
||||
ConstantUtil.SHARE_PRE_NAME,
|
||||
MyApplication.getContext(),
|
||||
ConstantUtil.KEY_APP_LANGUAGE);
|
||||
|
||||
if (savedLocale != null && !savedLocale.isEmpty()) {
|
||||
LocaleListCompat localeList = LocaleListCompat.forLanguageTags(savedLocale);
|
||||
AppCompatDelegate.setApplicationLocales(localeList);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取语言选择列表的显示文本
|
||||
*/
|
||||
public static String[] getLanguageDisplayNames(Context context) {
|
||||
return new String[]{
|
||||
context.getString(R.string.language_chinese),
|
||||
context.getString(R.string.language_english)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取语言选择列表的图标资源ID
|
||||
*/
|
||||
public static int[] getLanguageIcons() {
|
||||
return new int[]{
|
||||
R.mipmap.icon_langage_chinese,
|
||||
R.mipmap.icon_langage_eng
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user