wjc
2023-07-07 22494af577e21a930abef309f2f60c03c9615bd1
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package com.hdl.photovoltaic.utils;
 
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import java.util.Locale;
 
/**
 * Created by hxb on 2022/6/7.
 * desc   : 多语言适配方案,适配各种版本,核心未替换上下文Context中的Local
 */
public class LocalManageUtil {
 
//        private static final String TAG = "LanguageUtil";
//
//        /**
//         * 默认支持的语言,英语、法语、阿拉伯语
//         */
//        private static HashMap<String, Locale> supportLanguage = new HashMap<String, Locale>(4) {{
//            put(Language.ENGLISH, Locale.ENGLISH);
//        }};
//
//        /**
//         * 应用多语言切换,重写BaseActivity中的attachBaseContext即可
//         * 采用本地SP存储的语言
//         *
//         * @param context 上下文
//         * @return context
//         */
//        public static Context attachBaseContext(Context context) {
//            String language = LanguageSp.getLanguage(context);
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
//                return createConfigurationContext(context, language);
//            } else {
//                return updateConfiguration(context, language);
//            }
//        }
//
//    /**
//         * 应用多语言切换,重写BaseActivity中的attachBaseContext即可
//         *
//         * @param context  上下文
//         * @param language 语言
//         * @return context
//         */
//        public static Context attachBaseContext(Context context, String language) {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
//                return createConfigurationContext(context, language);
//            } else {
//                return updateConfiguration(context, language);
//            }
//        }
//
//        /**
//         * 获取Local,根据language
//         *
//         * @param language 语言
//         * @return Locale
//         */
//        private static Locale getLanguageLocale(String language) {
//            if (supportLanguage.containsKey(language)) {
//                return supportLanguage.get(language);
//            } else {
//                Locale systemLocal = getSystemLocal();
//                for (String languageKey : supportLanguage.keySet()) {
//                    if (TextUtils.equals(supportLanguage.get(languageKey).getLanguage(), systemLocal.getLanguage())) {
//                        return systemLocal;
//                    }
//                }
//            }
//            return Locale.ENGLISH;
//        }
//
//        /**
//         * 获取当前的Local,默认英语
//         *
//         * @param context context
//         * @return Locale
//         */
//        public static Locale getCurrentLocale(Context context) {
//            String language = LanguageSp.getLanguage(context);
//            if (supportLanguage.containsKey(language)) {
//                return supportLanguage.get(language);
//            } else {
//                Locale systemLocal = getSystemLocal();
//                for (String languageKey : supportLanguage.keySet()) {
//                    if (TextUtils.equals(supportLanguage.get(languageKey).getLanguage(), systemLocal.getLanguage())) {
//                        return systemLocal;
//                    }
//                }
//            }
//            return Locale.ENGLISH;
//        }
//
//        /**
//         * 获取系统的Local
//         *
//         * @return Locale
//         */
//        private static Locale getSystemLocal() {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                return Resources.getSystem().getConfiguration().getLocales().get(0);
//            } else {
//                return Locale.getDefault();
//            }
//        }
//
//        /**
//         * Android 7.1 以下通过 updateConfiguration
//         *
//         * @param context  context
//         * @param language 语言
//         * @return Context
//         */
//        private static Context updateConfiguration(Context context, String language) {
//            Resources resources = context.getResources();
//            Configuration configuration = resources.getConfiguration();
//            Locale locale = getLanguageLocale(language);
//            Log.e(TAG, "updateLocalApiLow==== " + locale.getLanguage());
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                // apply locale
//                configuration.setLocales(new LocaleList(locale));
//            } else {
//                // updateConfiguration
//                configuration.locale = locale;
//                DisplayMetrics dm = resources.getDisplayMetrics();
//                resources.updateConfiguration(configuration, dm);
//            }
//            return context;
//        }
//
//        /**
//         * Android 7.1以上通过createConfigurationContext
//         * N增加了通过config.setLocales去修改多语言
//         *
//         * @param context  上下文
//         * @param language 语言
//         * @return context
//         */
//        @RequiresApi(api = Build.VERSION_CODES.N_MR1)
//        private static Context createConfigurationContext(Context context, String language) {
//            Resources resources = context.getResources();
//            Configuration configuration = resources.getConfiguration();
//            Locale locale = getLanguageLocale(language);
//            Log.d(TAG, "current Language locale = " + locale);
//            LocaleList localeList = new LocaleList(locale);
//            configuration.setLocales(localeList);
//            return context.createConfigurationContext(configuration);
//        }
//
//        /**
//         * 切换语言
//         *
//         * @param language 语言
//         * @param activity 当前界面
//         * @param cls      跳转的界面
//         */
//        public static void switchLanguage(String language, Activity activity, Class<?> cls) {
//            LanguageSp.setLanguage(activity, language);
//            Intent intent = new Intent(activity, cls);
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//            activity.startActivity(intent);
//            activity.finish();
//        }
//
//        /**
//         * 切换语言,携带传递数据
//         *
//         * @param language 语言
//         * @param activity 当前界面
//         * @param cls      跳转的界面
//         */
//        public static void switchLanguage(String language, Activity activity, Class<?> cls, Bundle bundle) {
//            LanguageSp.setLanguage(activity, language);
//            Intent intent = new Intent(activity, cls);
//            if (bundle != null) {
//                intent.putExtras(bundle);
//            }
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//            activity.startActivity(intent);
//            activity.finish();
//        }
//
//        /**
//         * 获取新语言的 Context,修复了androidx.appCompact 1.2.0的问题
//         *
//         * @param newBase newBase
//         * @return Context
//         */
//        public static Context getNewLocalContext(Context newBase) {
//            try {
//                // 多语言适配
//                Context context = LanguageUtil.attachBaseContext(newBase);
//                // 兼容appcompat 1.2.0后切换语言失效问题
//                final Configuration configuration = context.getResources().getConfiguration();
//                return new ContextThemeWrapper(context, R.style.Theme_AppCompat_Empty) {
//                    @Override
//                    public void applyOverrideConfiguration(Configuration overrideConfiguration) {
//                        if (overrideConfiguration != null) {
//                            overrideConfiguration.setTo(configuration);
//                        }
//                        super.applyOverrideConfiguration(overrideConfiguration);
//                    }
//                };
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            return newBase;
//        }
//
//        /**
//         * 更新Application的Resource local,应用不重启的情况才调用,因为部分会用到application中的context
//         * 切记不能走新api createConfigurationContext,亲测
//         *
//         * @param context     context
//         * @param newLanguage newLanguage
//         */
//        public static void updateApplicationLocale(Context context, String newLanguage) {
//            Resources resources = context.getResources();
//            Configuration configuration = resources.getConfiguration();
//            Locale locale = getLanguageLocale(newLanguage);
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                // apply locale
//                configuration.setLocales(new LocaleList(locale));
//            } else {
//                configuration.setLocale(locale);
//            }
//            DisplayMetrics dm = resources.getDisplayMetrics();
//            resources.updateConfiguration(configuration, dm);
//        }
 
    private static Locale locale;
 
 
    /**
     * 获取系统的locale
     *
     * @return Locale对象
     */
    public static Locale getSystemLocale(Context context) {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = LocaleList.getDefault().get(0);
        } else {
            locale = Locale.getDefault();
        }
        return locale;
    }
 
    public static Context setLocal(Context context) {
        return updateResources(context, locale);
    }
 
    public static Context updateResources(Context context, Locale locale) {
        if (locale == null) {
            return context;
        }
        LocalManageUtil.locale = locale;
        Locale.setDefault(locale);
 
        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
        if (Build.VERSION.SDK_INT >= 17) {
            config.setLocale(locale);
            context = context.createConfigurationContext(config);
        } else {
            config.locale = locale;
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
        return context;
    }
}