package com.czp.util;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
public class Resource {
public static final int INVAILDATE_VALUE = -1;
private Map<String, String> resc = new HashMap<String, String>();
/**
* @param basePathName
* like com/resc/filename
* 请不要加:_语言(_zh/_en)
*/
public Resource(String basePathName) {
ResourceBundle bundle = ResourceBundle.getBundle(basePathName);
if (resc.isEmpty()) {
Enumeration<String> enu = bundle.getKeys();
while (enu.hasMoreElements()) {
String key = enu.nextElement();
resc.put(key, bundle.getString(key));
}
}
}
public String getString(String key) {
return resc.get(key);
}
public int getInt(String key) {
String value = resc.get(key);
try {
return Integer.parseInt(value);
} catch (Exception e) {
return INVAILDATE_VALUE;
}
}
}
///测试
package com.czp.util;
public class ResourceUtil {
private static ResourceUtil resu;
private ResourceUtil() {
}
public static ResourceUtil instance() {
if (resu == null)
resu = new ResourceUtil();
return resu;
}
public Resource getReource(String baseName) {
return new Resource(baseName);
}
public static void main(String[] args) {
//资源文件在src目录下:resc/DisplayList_zh.properties/DisplayList_en.properties
Resource resource = ResourceUtil.instance().getReource("resc/DisplayList");
System.out.println(resource.getInt("age"));
}
}
java国际化
最新推荐文章于 2023-04-07 09:00:11 发布