懵懵燈燈的BLOG

          寒夜孤燈點(diǎn)點(diǎn)星

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            56 隨筆 :: 10 文章 :: 22 評(píng)論 :: 0 Trackbacks

          ResourceBundle (Abstract):handleGetObject(String),getKeys()
          ?+ ListResourceBundle (Abstract) : getContent()
          ?+ PropertyResourceBundle
          ?
          ======================================

          用法:

          1. 取得PropertyResourceBundle的實(shí)例,我們使用以下靜態(tài)方法:

          ResourceBundle.getBundle(String base)
          ResourceBundle.getBundle(String base,Locale)
          ResourceBundle.getBundle(String base,Locale,ClassLoader)

          return ResourceBundle instance


          NOTE:? 其中的base可以使類名,也可以是資源文件的名稱。
          1) the Property File should be located as full qualified name,
          seperated by "/" or "." from class path, respect to the ClassLoader

          MyResource.properties?????? //default
          MyResource_zh.properties
          MyResource_de.properties


          2) the ResourceBundle Class should be located as full qualified name,
          seperated by "/" or "." from class path, respect to the ClassLoader

          MyResource.class?????//default parent by name
          MyResource_zh.class
          MyResource_de.class

          -------------------------------------------------------------
          public class MyResource extends ResourceBundle {

          ?@Override
          ?public Enumeration<String> getKeys() {
          ???? ResourceBundle parent = this.parent;
          ????
          ???? HashSet<String> s=new HashSet<String>();
          ????? return new Enumeration<String>(){
          ????? ?
          ????? ?int count=0;
          ?
          ????public boolean hasMoreElements() {
          ?????
          ?????return count<2;
          ????}
          ?
          ????public String nextElement() {
          ?????synchronized (MyResource.this) {
          ???????if(count++==0) return "okKey";
          ???????if(count++==1) return "cancelKey";
          ???????else return null;
          ?????}
          ????}
          ???};
          ?}

          ?@Override
          ?protected Object handleGetObject(String key) {
          ????if (key.equals("okKey")) return "Ok";
          ??????? if (key.equals("cancelKey")) return "Cancel";
          ??????? return null;
          ?}
          }

          //if some key not find, go to the parent
          public class MyResource_de extends MyResource {
          ?@Override
          ?protected Object handleGetObject(String key) {
          ????//if (key.equals("okKey")) return "Ok_de";
          ??????? if (key.equals("cancelKey")) return "Cancel_de";
          ??????? return null;
          ?}
          }


          3) the name convention:

          baseclass + "_" + language1 + "_" + country1 + "_" + variant1
          baseclass + "_" + language1 + "_" + country1
          baseclass + "_" + language1
          baseclass
          baseclass + "_" + language2 + "_" + country2 + "_" + variant2
          baseclass + "_" + language2 + "_" + country2
          baseclass + "_" + language2

          4) throws:

          java.lang.NullPointerException if baseName or locale is null
          MissingResourceException if no resource bundle for the specified base name can be found


          2. 使用ListResourceBundle

          public class MyResources extends ListResourceBundle {
          ???? protected Object[][] getContents() {
          ???????? return new Object[][] = {
          ???????? // LOCALIZE THIS
          ???????????? {"OkKey", "OK"},
          ???????????? {"CancelKey", "Cancel"},
          ???????? // END OF MATERIAL TO LOCALIZE
          ???????? };
          ???? }
          }

          3. 使用Parent

          // default (English language, United States)
          ?abstract public class MyResources extends ResourceBundle {
          ???? public Object handleGetObject(String key) {
          ???????? if (key.equals("okKey")) return "Ok";
          ???????? if (key.equals("cancelKey")) return "Cancel";
          ???????? return null;
          ???? }
          ?}

          ?// German language
          ?abstract public class MyResources_de extends MyResources {
          ???? public Object handleGetObject(String key) {
          ???????? // don't need okKey, since parent level handles it.
          ???????? if (key.equals("cancelKey")) return "Abbrechen";
          ???????? return null;
          ???? }
          ?}
          ?
          4. 取得bundle中的對(duì)象

          getString(String key)
          getStringArray(String key)
          getObject(String key)

          ?

          ?

          ?

          ?

          ?

          ?


          ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

          posted on 2007-12-11 13:53 懵懵燈燈 閱讀(1043) 評(píng)論(1)  編輯  收藏 所屬分類: Language_Java

          評(píng)論

          # re: [JAVA]java.util.ResourceBundle使用 2008-11-14 14:43 等等等
          什么玩意  回復(fù)  更多評(píng)論
            


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 湟源县| 惠州市| 盱眙县| 湖南省| 博客| 那曲县| 彰化市| 玉林市| 海林市| 绍兴县| 涡阳县| 曲周县| 哈巴河县| 惠安县| 大港区| 麻栗坡县| 上栗县| 吐鲁番市| 屯昌县| 新巴尔虎左旗| 肥西县| 女性| 宁都县| 陕西省| 呼玛县| 澄迈县| 江达县| 怀来县| 句容市| 雅江县| 阿合奇县| 依兰县| 靖江市| 含山县| 青浦区| 隆化县| 阿合奇县| 定结县| 罗定市| 仙游县| 娱乐|