18034727885
2026-07-02 eb6124e310ae8875a0150ee5d2edd52d5a579d87
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.driver.igotrip.utils;
 
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.os.PowerManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
 
import com.driver.igotrip.base.Const;
import com.driver.igotrip.base.MyApplication;
import com.driver.igotrip.ui.pub.StartActivity;
 
import java.util.Locale;
 
import cn.sinata.xldutils.utils.SPUtils;
 
import static android.content.ContentValues.TAG;
 
public class LanguageUtil {
    public static Context attachBaseContext(Context context, String language, String country) {
        Log.d(TAG, "attachBaseContext: " + Build.VERSION.SDK_INT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, language, country);
        } else {
            return context;
        }
    }
 
    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context, String language, String country) {
        Resources resources = context.getResources();
        Locale locale = new Locale(language);
        if (language.equals(Const.Language.CHINESE) || language.equals("")) {
            locale = Locale.CHINA;
        }
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        configuration.setLocales(new LocaleList(locale));
        return context.createConfigurationContext(configuration);
    }
 
    public static void reApp(Context context) {
//        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//        manager.restartPackage("com.driver.igotrip");
        MyApplication.Companion.getInstance().finishAll();
        context.startActivity(new Intent(context, StartActivity.class));
 
    }
 
    /**
     * @param context
     * @param newLanguage 想要切换的语言类型 比如 Const.Language.ENGLISH ,"zh"
     */
    @SuppressWarnings("deprecation")
    public static Context changeAppLanguage(Context context, String newLanguage, String country) {
        if (TextUtils.isEmpty(newLanguage)) {
            return context;
        }
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        //获取想要切换的语言类型
        Locale locale = new Locale(newLanguage, country);
        configuration.setLocale(locale);
        if (newLanguage.equals(Const.Language.CHINESE)) {
            configuration.setLocale(Locale.CHINA);
        }
        Locale.setDefault(locale);
        Context newContext = context.createConfigurationContext(configuration);
 
        // updateConfiguration
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            DisplayMetrics dm = resources.getDisplayMetrics();
            resources.updateConfiguration(configuration, dm);
        }
        return newContext;
    }
 
    /**
     * 如果是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) {
            LanguageUtil.changeAppLanguage(appContext, language, country);
        }
        SPUtils.save(Const.language, language);
        SPUtils.save("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);
    }
}