提交 | 用户 | 时间
|
e7c126
|
1 |
package com.xxl.job.admin.core.util; |
H |
2 |
|
|
3 |
import com.xxl.job.admin.core.conf.XxlJobAdminConfig; |
|
4 |
import org.slf4j.Logger; |
|
5 |
import org.slf4j.LoggerFactory; |
|
6 |
import org.springframework.core.io.ClassPathResource; |
|
7 |
import org.springframework.core.io.Resource; |
|
8 |
import org.springframework.core.io.support.EncodedResource; |
|
9 |
import org.springframework.core.io.support.PropertiesLoaderUtils; |
|
10 |
|
|
11 |
import java.io.IOException; |
|
12 |
import java.text.MessageFormat; |
|
13 |
import java.util.HashMap; |
|
14 |
import java.util.Map; |
|
15 |
import java.util.Properties; |
|
16 |
|
|
17 |
/** |
|
18 |
* i18n util |
|
19 |
* |
|
20 |
* @author xuxueli 2018-01-17 20:39:06 |
|
21 |
*/ |
|
22 |
public class I18nUtil { |
|
23 |
private static Logger logger = LoggerFactory.getLogger(I18nUtil.class); |
|
24 |
|
|
25 |
private static Properties prop = null; |
|
26 |
public static Properties loadI18nProp(){ |
|
27 |
if (prop != null) { |
|
28 |
return prop; |
|
29 |
} |
|
30 |
try { |
|
31 |
// build i18n prop |
|
32 |
String i18n = XxlJobAdminConfig.getAdminConfig().getI18n(); |
|
33 |
String i18nFile = MessageFormat.format("i18n/message_{0}.properties", i18n); |
|
34 |
|
|
35 |
// load prop |
|
36 |
Resource resource = new ClassPathResource(i18nFile); |
|
37 |
EncodedResource encodedResource = new EncodedResource(resource,"UTF-8"); |
|
38 |
prop = PropertiesLoaderUtils.loadProperties(encodedResource); |
|
39 |
} catch (IOException e) { |
|
40 |
logger.error(e.getMessage(), e); |
|
41 |
} |
|
42 |
return prop; |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* get val of i18n key |
|
47 |
* |
|
48 |
* @param key |
|
49 |
* @return |
|
50 |
*/ |
|
51 |
public static String getString(String key) { |
|
52 |
return loadI18nProp().getProperty(key); |
|
53 |
} |
|
54 |
|
|
55 |
/** |
|
56 |
* get mult val of i18n mult key, as json |
|
57 |
* |
|
58 |
* @param keys |
|
59 |
* @return |
|
60 |
*/ |
|
61 |
public static String getMultString(String... keys) { |
|
62 |
Map<String, String> map = new HashMap<String, String>(); |
|
63 |
|
|
64 |
Properties prop = loadI18nProp(); |
|
65 |
if (keys!=null && keys.length>0) { |
|
66 |
for (String key: keys) { |
|
67 |
map.put(key, prop.getProperty(key)); |
|
68 |
} |
|
69 |
} else { |
|
70 |
for (String key: prop.stringPropertyNames()) { |
|
71 |
map.put(key, prop.getProperty(key)); |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
String json = JacksonUtil.writeValueAsString(map); |
|
76 |
return json; |
|
77 |
} |
|
78 |
|
|
79 |
} |