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

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

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

    vulcan

    低頭做事,抬頭看路

       :: 首頁 :: 聯系 :: 聚合  :: 管理
      41 Posts :: 7 Stories :: 28 Comments :: 0 Trackbacks
    需要這個功能,但是卻發現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"));
    不過還欠缺一點功能:對于翻譯文本中數字參數等的支持,需要加強
    posted on 2008-03-21 18:13 vulcan 閱讀(977) 評論(0)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲人色大成年网站在线观看 | 日韩a毛片免费观看| 国产四虎免费精品视频| 亚洲午夜久久久精品影院| 蜜芽亚洲av无码精品色午夜| 中文在线观看永久免费| 亚洲日韩精品一区二区三区| 日韩电影免费在线观看网址| 久久精品国产亚洲AV不卡| 久久免费国产精品| 午夜视频免费在线观看| 亚洲综合精品一二三区在线| 最近中文字幕大全中文字幕免费| 亚洲美女一区二区三区| 99在线视频免费观看视频| 性xxxx黑人与亚洲| 日本二区免费一片黄2019| 亚洲人成亚洲精品| 在线永久免费的视频草莓| 亚洲色中文字幕在线播放| 永久免费看bbb| fc2成年免费共享视频网站| 成人免费在线观看网站| 亚洲无线电影官网| 免费精品国偷自产在线在线| 亚洲avav天堂av在线网毛片| 啦啦啦完整版免费视频在线观看| 亚洲免费福利视频| 免费一级黄色毛片| 久久国产乱子精品免费女| 亚洲国产精品xo在线观看| 在线观着免费观看国产黄| 国产午夜精品理论片免费观看| 亚洲日本一区二区三区| 青青青国产免费一夜七次郎| 一本到卡二卡三卡免费高| 亚洲综合小说久久另类区| 又黄又爽无遮挡免费视频| 香港a毛片免费观看| 亚洲AV永久无码精品放毛片| 亚洲韩国精品无码一区二区三区|