java的gettext_java版的getText

packagecom.csair.hunan.common;importjava.util.MissingResourceException;importjava.util.ResourceBundle;/*** gettext支持类

* 主要功能:可以自动获取调用者信息,从而自动的根据local寻找resource

* (1) 查找和类名相同的properties文件

* (2) 如果(1)失败,则默认查找调用者包中的message.properties文件

* 如果找不到相应的key,不抛出异常,而是返回msgId

* 另外它支持msgId带空格,但是在properties文件中,必须把空格全部替换成下划线

*@authorAlva

**/publicclassI18NUtils {/*A static instance holding the message*///multi-thread safety?publicstaticfinalThreadLocal resPool=newThreadLocal();publicstaticfinalThreadLocal callerPool=newThreadLocal();

@SuppressWarnings("unchecked")publicstaticString _(String s) {//get the direct caller class - FIXME how to deal with extend?Class caller=sun.reflect.Reflection.getCallerClass(2);

ResourceBundle resourceBundle=(ResourceBundle)resPool.get();//if no resourcebundle or the caller changed//initial a new resource bundleif(resourceBundle==null||!caller.equals(callerPool.get())) {try{//first try the property file who with the same name with the caller classresourceBundle=ResourceBundle.getBundle(caller.getCanonicalName());

}catch(MissingResourceException missingresourceexception) {try{//find the default: packageresourceBundle=ResourceBundle.getBundle(caller.getPackage().getName()+".message");

}catch(MissingResourceException e) {//do nothing, just suppress the exception, omit the missed resource}

}

resPool.set(resourceBundle);//record the new callercallerPool.set(caller);

}try{//replace key's blankString s1=(String)resourceBundle.getObject(s.trim().replaceAll("\\p{Blank}+","_"));if(s1!=null)returns1;

}catch(MissingResourceException missingresourceexception) { }returns;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值