hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
package chat.util;
 
import io.netty.util.internal.StringUtil;
 
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
public class I18n {
    private static ClassLoader Loader = null;
    static {
            String configPath = System.getProperty("wildfirechat.path", null);
            String classPath = I18n.class.getResource("/").getPath();
            classPath = classPath.replace("WEB-INF/classes", "");
            try {
                 classPath = URLDecoder.decode(classPath, "UTF-8");
            } catch (UnsupportedEncodingException e1) {
                 // TODO Auto-generated catch block
                 e1.printStackTrace();
            }
            String resBundlePaht = "config/i18n";
            
            File file = new File(classPath, resBundlePaht);
            URL[] urls = new URL[0];
            try {
                 urls = new URL[]{file.toURI().toURL()};
                 Loader = new URLClassLoader(urls);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
    }
 
    private static Map<String, ResourceBundle> bundleMap = new ConcurrentHashMap<>();
 
    public static String getString(String language, String key) {
        ResourceBundle bundle = bundleMap.get(language);
 
        if (StringUtil.isNullOrEmpty(language)) language = "zh_CN";
 
        if (bundle == null) {
            bundle = ResourceBundle.getBundle("messages", new Locale(language), Loader);
            bundleMap.put(language, bundle);
        }
 
        try {
            return bundle.getString(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "糟糕,字符串 " + key + " 没有找到";
    }
 
    public static void test() throws MalformedURLException {
 
        // 设置定制的语言国家代码
        Locale locale1 = new Locale("zh");
        Locale locale2 = new Locale("en");
        ResourceBundle rb = ResourceBundle.getBundle("messages", locale1, Loader);
 
        // 获得相应的key值
        String greeting = rb.getString("Above_Greeting_Message");
        String userInfo = rb.getString("Friend_Can_Start_Chat");
 
        System.out.println(greeting);
        System.out.println(userInfo);
    }
 
    public static void main(String[] args) throws Exception {
        //test();
        //String configPath = System.getProperty("medeasy.path", null);
        String aa = System.getProperty("user.dir");
        String bb = I18n.class.getResource("/").getPath();//I18n.class.getResource("/").getPath();
        System.out.println(aa);
        bb = URLDecoder.decode(bb, "UTF-8");
        System.out.println(bb);
    }
}