mac
2024-05-08 157065e7a5fc1946fa1827c89499562ec2d32196
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
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 android.text.TextUtils;
import android.util.DisplayMetrics;
 
 
import com.alibaba.fastjson.JSON;
import com.hdl.linkpm.sdk.utils.HDLFileUtils;
import com.hdl.photovoltaic.HDLApp;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.other.HdlFileLogic;
import com.hdl.photovoltaic.ui.adapter.LanguageAdapter;
import com.hdl.photovoltaic.ui.bean.LongLatListInfo;
import com.hdl.photovoltaic.ui.bean.NationBean;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
 
/**
 * 多语言适配
 */
public class LocalManageUtil {
 
    public static final String zh = "zh";//中文你
    public static final String en = "en";//英文
 
//    private static Locale mLocale;
 
    /**
     * 获取系统的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;
    }
 
    /**
     * 应用内切换语言
     *
     * @param language language (zh:汉语;en:英语)
     * @param context  上下文
     */
    public static void changeAppLanguage(String language, Context context) {
        Locale locale = getLocale(language);
        updateResources(locale, context);
    }
 
 
    /**
     * 获取系统当前语言
     *
     * @return _
     */
    public static Locale getDefaultLocale() {
        return Resources.getSystem().getConfiguration().locale;
    }
 
    /**
     * 获取Locale
     *
     * @param language (zh:汉语;en:英语)
     * @return Locale
     */
    public static Locale getLocale(String language) {
        try {
            if (TextUtils.isEmpty(language)) {
                return getDefaultLocale();
            }
            return new Locale(language);
        } catch (Exception e) {
            return getDefaultLocale();
        }
 
 
    }
 
    /**
     * 更新资源数据
     *
     * @param locale  语言实体类
     * @param context 上下文
     * @return -
     */
    private static void updateResources(Locale locale, Context context) {
 
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            //7.0以下系统
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            Configuration configuration = context.getResources().getConfiguration();
            configuration.setLocale(locale);
            context.getResources().updateConfiguration(configuration, metrics);
//            Locale.setDefault(locale);
        } else {
            //7.0及以上系统
            Configuration configuration = context.getResources().getConfiguration();
            configuration.setLocale(locale);
            configuration.setLocales(new LocaleList(locale));
            context.createConfigurationContext(configuration);
        }
 
    }
 
    /**
     * 设置当前APP的语言模式
     *
     * @param context 上下文
     */
    private static void setCurrLanguageMode(Context context) {
        String language = SharedPreUtils.getSharedPreferencesKey("languege");
        Locale local = getLocale(language);
        Resources res = context.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = local;
        res.updateConfiguration(conf, dm);
    }
 
 
    /**
     * 获取语言列表
     *
     * @return 返回语言列表
     */
    public static List<LanguageAdapter.ItemData> getLanguageList() {
        List<LanguageAdapter.ItemData> list = new ArrayList<>();
        LanguageAdapter.ItemData zh = new LanguageAdapter.ItemData();
        zh.setState(false);
        zh.setTitle("简体中文");
        zh.setLanguage(LocalManageUtil.zh);
        list.add(zh);
        LanguageAdapter.ItemData en = new LanguageAdapter.ItemData();
        en.setState(false);
        en.setTitle("English");
        en.setLanguage(LocalManageUtil.en);
        list.add(en);
        return list;
    }
 
 
    public static LanguageAdapter.ItemData getLanguage(String languageStr) {
        List<LanguageAdapter.ItemData> list = getLanguageList();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getLanguage().equals(languageStr)) {
                return list.get(i);
 
            }
        }
        return new LanguageAdapter.ItemData();
    }
 
 
    //国家,省份,城市
    private static List<NationBean> nationBeans = new ArrayList<>();
    //国家,省份,经纬度
    private static List<LongLatListInfo> longLatBeans = new ArrayList<>();
 
    public static void getLocationInfo(Context context) {
        if (isZh()) {
            nationBeans = JSON.parseArray(HdlFileLogic.getInstance().openAssetsFileJson("locationCN.json", context), NationBean.class);
        } else {
            nationBeans = JSON.parseArray(HdlFileLogic.getInstance().openAssetsFileJson("locationEN.json", context), NationBean.class);
        }
    }
 
    public static void getLongLatInfo(Context context) {
        if (isZh()) {
            longLatBeans = JSON.parseArray(HdlFileLogic.getInstance().openAssetsFileJson("longLatCN.json", context), LongLatListInfo.class);
        } else {
            longLatBeans = JSON.parseArray(HdlFileLogic.getInstance().openAssetsFileJson("longLatEN.json", context), LongLatListInfo.class);
        }
    }
 
 
    public static boolean isZh() {
//        Locale locale = context.getResources().getConfiguration().locale;
//        String language = locale.getLanguage();
        String language = UserConfigManage.getInstance().getCurrentAppLanguage();
        return language.equals(zh);
    }
 
    public static List<NationBean> getNationBeans() {
        if (nationBeans == null) {
            return new ArrayList<>();
        }
        return nationBeans;
    }
 
    public static List<LongLatListInfo> getLongLatBeans() {
        if (longLatBeans == null) {
            return new ArrayList<>();
        }
        return longLatBeans;
    }
 
 
}