<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    vulcan

    低頭做事,抬頭看路

       :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      41 Posts :: 7 Stories :: 28 Comments :: 0 Trackbacks
    需要這個功能,但是卻發(fā)現(xiàn)gnu提供的,還是sun的ResourceBundle都不是怎么好用,所以根據需要在resourceBundle的基礎上封裝了一下。
    package com.csair.hunan.common;

    import java.util.MissingResourceException;
    import java.util.ResourceBundle;

    /**
     * gettext支持類
     * 主要功能:可以自動獲取調用者信息,從而自動的根據local尋找resource
     * (1) 查找和類名相同的properties文件
     * (2) 如果(1)失敗,則默認查找調用者包中的message.properties文件
     * 如果找不到相應的key,不拋出異常,而是返回msgId
     * 另外它支持msgId帶空格,但是在properties文件中,必須把空格全部替換成下劃線
     * 
    @author Alva
     * 
     
    */
    public class I18NUtils {
        
    /* A static instance holding the message */
        
    //multi-thread safety?
        public static final ThreadLocal resPool = new ThreadLocal();
        
    public static final ThreadLocal callerPool = new ThreadLocal();

        @SuppressWarnings(
    "unchecked")
        
    public static String _(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 bundle
            if(resourceBundle == null || !caller.equals(callerPool.get())) {
                
    try {
                    
    // first try the property file who with the same name with the caller class
                    resourceBundle = ResourceBundle.getBundle(caller.getCanonicalName());
                } 
    catch(MissingResourceException missingresourceexception) {
                    
    try {
                        
    //find the default: package
                        resourceBundle = 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 caller
                callerPool.set(caller);
            }
            
    try
            {
                
    // replace key's blank
                String s1 = (String)resourceBundle.getObject(s.trim().replaceAll("\\p{Blank}+""_"));
                
    if(s1 != null)
                    
    return s1;
            }
            
    catch(MissingResourceException missingresourceexception) { }
            
    return s;
        }
    }
    使用起來很方便,在需要使用的類中
    import static com.csair.hunan.common.I18NUtils._;
    //
    System.out.println(_("msgId"));
    不過還欠缺一點功能:對于翻譯文本中數(shù)字參數(shù)等的支持,需要加強
    posted on 2008-03-21 18:13 vulcan 閱讀(976) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網站導航:
     
    主站蜘蛛池模板: 中文亚洲AV片不卡在线观看| 国产一二三四区乱码免费| 精品国产无限资源免费观看| 亚洲αv在线精品糸列| 国产精品内射视频免费| www.亚洲精品| 一级毛片a女人刺激视频免费| 国产无遮挡吃胸膜奶免费看| 亚洲国产成人无码AV在线| 好男人看视频免费2019中文 | 亚洲国产成人久久三区| 99视频在线精品免费| 久久久亚洲裙底偷窥综合| 最近中文字幕无免费| 亚洲av永久无码精品天堂久久| 日本免费xxxx色视频| 国产精品亚洲精品| 日本黄色免费观看| 狼人大香伊蕉国产WWW亚洲| 永久久久免费浮力影院| 国产精品亚洲lv粉色| 亚洲精品亚洲人成在线观看下载| 国产精品免费久久久久影院| 亚洲综合无码精品一区二区三区| 国产一级在线免费观看| 亚洲三级电影网址| 无码人妻精品中文字幕免费东京热| 亚洲综合无码一区二区痴汉 | 91免费在线视频| 亚洲av日韩av高潮潮喷无码| 亚洲av无码不卡久久| 四虎www免费人成| 一级做a爰黑人又硬又粗免费看51社区国产精品视 | 国产大陆亚洲精品国产| 色噜噜亚洲精品中文字幕| 久久99精品免费视频| 亚洲国产熟亚洲女视频| 亚洲av片一区二区三区| 亚洲精品免费在线观看| 国产精品无码亚洲精品2021| 亚洲宅男天堂在线观看无病毒|