doum
6 天以前 2b287056e2f59518888d05a1bbc7e5a55fbd84d5
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
package com.doumee.keyCabinet.utils;
 
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
 
import com.yuyh.library.imgsel.utils.LogUtils;
 
import org.apache.commons.lang3.StringUtils;
 
import java.util.HashMap;
import java.util.Locale;
 
/**
 * 功能描述:修改app内部的语言工具类
 */
public class LanguageUtil {
 
    /*语言类型:
     * 此处支持3种语言类型,更多可以自行添加。
     * */
 
    private static HashMap<String, Locale> languagesList = new HashMap<String, Locale>(2) {{
        put("zh", Locale.CHINESE);
        put("ru", new Locale("ru"));
    }};
 
    /**
     * 修改语言
     *
     * @param context 上下文
     * @param language 例如修改为 英文传“en”,参考上文字符串常量
     * @param cls      要跳转的类(一般为入口类), Class<?> cls
     */
    public static void changeAppLanguage(Context context, String language) {
        if(StringUtils.isEmpty(language)){
            language = "zh";
        }
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        // app locale 默认简体中文
        Locale locale = getLocaleByLanguage(language);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);
 
        LogUtils.e("设置的语言:" + language);
        //finish();
        // 重启app
        /*Intent intent = new Intent(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);*/
        //加载动画
        //activity.overridePendingTransition(R.anim.anim_right_in, R.anim.anim_left_out);
        //activity.overridePendingTransition(0, 0);
    }
 
    /**
     * 获取指定语言的locale信息,如果指定语言不存在
     * 返回本机语言,如果本机语言不是语言集合中的一种,返回英语
     */
    private static Locale getLocaleByLanguage(String language) {
        if (isContainsKeyLanguage(language)) {
            return languagesList.get(language);
        } else {
            Locale locale = Locale.getDefault();
            for (String key : languagesList.keySet()) {
                if (TextUtils.equals(languagesList.get(key).getLanguage(), locale.getLanguage())) {
                    return locale;
                }
            }
        }
        return Locale.CHINESE;
    }
 
    /**
     * 如果此映射包含指定键的映射关系,则返回 true
     */
    private static boolean isContainsKeyLanguage(String language) {
        return languagesList.containsKey(language);
    }
 
}