18034727885
2026-05-08 0cd2089f05a87e9a2155d169c1c3f654e17ef396
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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);
    }
}