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

 

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);
 }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值