您的位置:首页 > 编程语言 > Java开发

java 中资源国际化工具类的编写实例

2011-02-23 15:36 375 查看
public class MessageResourceUtil {

public static class Entry implements CharSequence {
private String key;
private CharSequence[] values;

private String strVal;
private Map<Locale, String> i18nVals = new HashMap<Locale, String>();

private Entry(String key, CharSequence ... values) {
this.key = key;
this.values = values;
strVal = toString(Locale.getDefault());
}

public static Entry wrap(String key, CharSequence ... values) {
return new Entry(key, values);
}

public char charAt(int index) {
return strVal.charAt(index);
}

public int length() {
return strVal.length();
}

public CharSequence subSequence(int start, int end) {
return strVal.subSequence(start, end);
}

public String toString() {
return strVal;
}

public String toString(Locale locale) {
String result = i18nVals.get(locale);
if (result == null) {
String[] strVals = new String[0];
if (values != null && values.length > 0) {
strVals = new String[values.length];
for (int i = 0; i < values.length; i++) {
if (values[i] instanceof Entry) {
strVals[i] = ((Entry)values[i]).toString(locale);
} else {
try {
strVals[i] = values[i].toString();
} catch (Exception e) {
strVals[i] = (String)values[i];
}
}
}
}
result = getMessage(locale, key, strVals);
i18nVals.put(locale, result);
}
return result;
}
}

private static final String MESSAGE_BASE_NAME = "ApplicationResources";
private static Logger logger = Logger.getLogger(MessageResourceUtil.class);
private static Map<String, ResourceBundle> resourceBundleMap = new HashMap<String, ResourceBundle>();

private static synchronized ResourceBundle getResourceBundle(
String language, String country) {
if (StringUtil.isEmpty(language) || StringUtil.isEmpty(country)) {
language = "zh";
country = "CN";
}

ResourceBundle resourceBundle = resourceBundleMap.get(language);
if (resourceBundle == null) {
Locale locale = new Locale(language, country);
resourceBundle = PropertyResourceBundle.getBundle(
MESSAGE_BASE_NAME, locale, Thread.currentThread()
.getContextClassLoader());
resourceBundleMap.put(language, resourceBundle);
}
return resourceBundle;
}

/**
* 获取国际化消息
*
* @param language
* 语言代码
* @param country
* 国家代码
* @param key
* 键
* @return
*/
public static String getMessage(Locale locale, String key) {
ResourceBundle resourceBundle = getResourceBundle(locale.getLanguage(), locale.getCountry());
try {
String message = resourceBundle.getString(key);
message = (message != null) ? message.trim() : "";
return message;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return "";
}
}

/**
* 获取国际化消息
*
* @param language
* 语言代码
* @param country
* 国家代码
* @param key
* 键
* @param values
* 格式化值
* @return
*/
public static String getMessage(Locale locale,
String key, String... values) {
String message = getMessage(locale, key);
if (StringUtil.isEmpty(message)) {
return message;
} else {
return StringUtil.formart(message, values);
}
}

public static String getMessage(String key) {
return getMessage(Locale.getDefault(), key);
}
public static String getMessage(String key, String ... values) {
return getMessage(Locale.getDefault(), key, values);
}
public static String getMessage(Exception e) {
return getMessage(Locale.getDefault(), e);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: