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
package com.vci.common.exception;
 
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.ResourceBundle;
 
 
public class LocaleCommonDisplay {
 
private static HashMap<String, ResourceBundle> map = new HashMap<String, ResourceBundle>();
    
    public LocaleCommonDisplay() {
        
    }
    
    /**
     * 从指定文件中获取指定key的国际化信息
     * @param key,主键
     * @param fileName,国际化文件名称
     * @param locale,当前区域
     * @return
     */
    public static String getI18nString(VciException exp, String fileName, Locale locale) {
        ResourceBundle resStrings = map.get(fileName);
        if (resStrings == null) {
            try {
                resStrings = ResourceBundle.getBundle("properties." + fileName, locale);
                map.put(fileName, resStrings);
            } catch (Exception e) {
                e.printStackTrace();
                resStrings = null;
            }
        }
        return getLocaleString(resStrings, exp);
    }
 
    /**
     * 获取指定key对应的值,如果key对应的值为空或者出现获取异常,则返回key。
     * @param key
     * @return
     */
    private static String getLocaleString(ResourceBundle resStrings, String key) {
        try {
            String value = resStrings.getString(key);
            if (value == null) {
                value = key;
            }
            return value;
        } catch (Exception e) {
            return key;
        }
    }
    
    public static String getI18nString(String key, String fileName, Locale locale) {
        ResourceBundle resStrings = map.get(fileName);
        if (resStrings == null) {
            try {
                resStrings = ResourceBundle.getBundle("properties." + fileName, locale);
                map.put(fileName, resStrings);
            } catch (Exception e) {
                e.printStackTrace();
                resStrings = null;
            }
        }
        return getLocaleString(resStrings, key);
    }
    
    /**
     * 根据异常结果,获取国际化异常提示信息。
     *  文件中有如下三种类型值:
     *   1、一个参数类型,如10000=客户端文件夹{0}下版本配置文件读取错误
     *   2、无参数类型:如10001=客户端配置文件读取错误
     *   3、多各参数类型:如10002=客户端配置文件{0}读取错误,具体错误信息为{1}
     *   
     * @param exp
     * @return
     */
    private static String getLocaleString(ResourceBundle resStrings, VciException exp) {
        if (exp == null) {
            return "";
        }
        
        String message = getLocaleString(resStrings, String.valueOf(exp.getException_code()));
        //无参数时,返回获取结果
        if (exp.getException_objArray().length == 0) {
            return message;
        }
        int len = exp.getException_objArray().length;
        for (int i = 0; i < len - 1; i++) {
            String uniqueIndex = ORAUniqueProperties.getUniqueIndexName(exp.getException_objArray()[i]);
            if (uniqueIndex == null || uniqueIndex.trim().equals("")) {
                continue;
            }
            exp.getException_objArray()[i] = getI18nString(uniqueIndex, "ORA00001", resStrings.getLocale());
        }
        //有参数时,根据参数及properties文件获取结果组合成显示信息返回
        String msg = MessageFormat.format(message, exp.getException_objArray());
        return msg;
    }
}