package cn.sinata.xldutils.utils;
|
|
import android.content.Context;
|
import android.content.res.Configuration;
|
import android.content.res.Resources;
|
import android.os.Build;
|
import android.os.LocaleList;
|
import android.text.TextUtils;
|
import android.util.DisplayMetrics;
|
|
import java.util.Locale;
|
|
public class LanguageUtil {
|
private static Locale getLocaleByLanguage(String language) {
|
if (TextUtils.isEmpty(language)) {
|
return Locale.FRENCH;
|
}
|
return switch (language) {
|
case "zh", "ch" -> Locale.CHINESE;
|
case "fr" -> Locale.FRENCH;
|
default -> Locale.ENGLISH;
|
};
|
}
|
|
public static Context attachBaseContext(Context context, String language, String country) {
|
Locale locale = getLocaleByLanguage(language);
|
Locale.setDefault(locale);
|
Resources resources = context.getResources();
|
Configuration configuration = new Configuration(resources.getConfiguration());
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
configuration.setLocale(locale);
|
LocaleList localeList = new LocaleList(locale);
|
LocaleList.setDefault(localeList);
|
configuration.setLocales(localeList);
|
return context.createConfigurationContext(configuration);
|
} else {
|
configuration.locale = locale;
|
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
|
}
|
|
return context;
|
}
|
|
/**
|
* @param context
|
* @param newLanguage 想要切换的语言类型 比如 Const.Language.ENGLISH ,"zh"
|
*/
|
public static void changeAppLanguage(Context context, String newLanguage, String country) {
|
if (TextUtils.isEmpty(newLanguage)) {
|
return;
|
}
|
Locale locale = getLocaleByLanguage(newLanguage);
|
Locale.setDefault(locale);
|
Resources resources = context.getResources();
|
Configuration configuration = new Configuration(resources.getConfiguration());
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
configuration.setLocale(locale);
|
LocaleList localeList = new LocaleList(locale);
|
LocaleList.setDefault(localeList);
|
configuration.setLocales(localeList);
|
} else {
|
configuration.locale = locale;
|
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
|
}
|
}
|
|
/**
|
* 如果是7.0以下,我们需要调用changeAppLanguage方法,
|
* 如果是7.0及以上系统,直接把我们想要切换的语言类型保存在SharedPreferences中,然后重新启动MainActivity即可
|
*/
|
public static void setLanguage(String language, String country, Context appContext) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
changeAppLanguage(appContext, language, country);
|
}
|
SPUtils.Companion.instance().put("language", language);
|
SPUtils.Companion.instance().put("country", country);
|
}
|
|
|
public static void set(boolean isEnglish, Context context) {
|
Configuration configuration = context.getResources().getConfiguration();
|
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
|
if (isEnglish) {
|
//设置英文
|
configuration.locale = Locale.ENGLISH;
|
} else {
|
//设置中文
|
configuration.locale = Locale.SIMPLIFIED_CHINESE;
|
}
|
//更新配置
|
context.getResources().updateConfiguration(configuration, displayMetrics);
|
}
|
}
|