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);
}
}
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);
}
}
相关文章推荐
- Java工具类的编写方法实例
- JNI中如何在本地代码中生成和操作Java的对象和字符串,并编写应用实例
- JAVA--第12周实验--任务1--编写一个Panel的子类MyPanel,要求MyPanel的实例作为其按钮的ActionEvent事件的监视器
- 【java工具类】导入freemarker并且编写一个freemarker的辅助类
- java公众平台通用接口工具类HttpConnectUtil实例代码
- 自己动手用JAVA编写JSON工具类
- Java实例 定义任务的一个发射小程序 实现Runnable接口并编写run()方法
- Java编写多功能万年历程序的实例分享
- java 高新技术【10】 编写对class文件进行加密的工具类
- JNI中在被调用的C/C++函数中如何访问Java程序中的类,并编写应用实例
- java 从属性文件加载数据的方法工具类 实例 可直接使用
- java 从数据源获取连接工具类DBUtil 实例
- 编写Java的(单例模式)单实例Singleton 的示例
- 07_数据库创建,添加c3p0操作所需的jar包,编写c3p0-config.xml文件,编写User.java,编写jdbcUtils.java实现操作数据库的模板工具类,UserDao编写,Dao
- 利用Java编写简单的WebService实例
- Java实现的读取资源文件工具类ResourcesUtil实例【可动态更改值的内容】
- 利用Java编写简单的WebService实例
- Java正则表达式工具类实例
- Java实现的DES加密解密工具类实例
- Java并发工具类Semaphore应用实例