package com.util;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import java.text.MessageFormat;
/**
* 获得配置资源(默认的为src/properties/message.properties文件)中的key的value信息
* 也可用Utility.setResourcePath(resourcePathStr)来改变资源的位置
* @author Administrator
*
*/
public class Utility {
public static final String module = Utility.class.getName();
// store resource bundle(默认放在properties文件夹下的message.properties文件中)
private static String csResourcePath = "properties.message";
public static String getResourcePath() {
return csResourcePath;
}
public static void setResourcePath(String asResourcePath) {
csResourcePath = asResourcePath;
}
/**
* Gary: get user locale from langcd
* @param asLangCd:国家语言参数
* @return
*/
public static Locale getUserLocale(String asLangCd) {
Locale lsUserLocale = Locale.ENGLISH;
if (asLangCd.equals("zht"))
lsUserLocale = Locale.TRADITIONAL_CHINESE;
else if (asLangCd.equals("zhs"))
lsUserLocale = Locale.SIMPLIFIED_CHINESE;
else if (asLangCd.equals("en"))
lsUserLocale = Locale.ENGLISH;
return lsUserLocale;
}
public static Locale getUserLocale() {
//the default locale is SIMPLIFIED_CHINESE
return Locale.SIMPLIFIED_CHINESE;
}
/**
* Gary: Functions to get message from resource
* @param asKey
* @param asLangCd
* @return
*/
public static String getMessage(String asKey) {
return getMessage(asKey, getUserLocale());
}
public static String getMessage(String asKey, String asLangCd) {
return getMessage(asKey, getUserLocale(asLangCd));
}
public static String getMessage(String asKey, Locale asLocale) {
ResourceBundle bundle = ResourceBundle.getBundle(Utility
.getResourcePath(), asLocale);
return bundle.getString(asKey);
}
public static String getMessage(String asKey, String asLangCd, String arg0,
String arg1, String arg2) {
return getMessage(asKey, getUserLocale(asLangCd), arg0, arg1, arg2);
}
public static String getMessage(String asKey, Locale asLocale, String arg0,
String arg1, String arg2) {
String msg = getMessage(asKey, asLocale);
String[] messageArgs = new String[3];
messageArgs[0] = arg0;
messageArgs[1] = arg1;
messageArgs[2] = arg2;
return MessageFormat.format(msg, messageArgs);
}
public static boolean isEmpty(String string) {
boolean isEmpty = true;
if (string != null && !"".equals(string) && string.trim().length() > 0)
isEmpty = false;
return isEmpty;
}
/**
* 自己的
* 加载配置文件中的所有键值对,并保存在Properties中
* @param propertyFileName:属性文件名
* @return
* 如getBundlePros("properties/email"); //在src/properties包下的email.properties文件
public Properties getBundleProps(String propertyFileName){
Properties props = new Properties();
ResourceBundle bundle = ResourceBundle.getBundle(propertyFileName.trim());
Enumeration enume = bundle.getKeys();
String key = "";
String value = "";
while(enume.hasMoreElements()){
key = (String)enume.nextElement();
value = bundle.getString(key.toString());
props.put(key, value);
}
return props;
}
*/
}