Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4624486

Resource bundle lookup search implementation does not match with the spec

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P2 P2
    • None
    • cdc-hi_1.0.1, 1.3.1
    • core-libs
    • None

      Java API 1.3 doc for ResourceBundle says:

      The resource bundle lookup searches for classes with various suffixes on the basis of (1) the desired
      locale and (2) the current default locale as returned by Locale.getDefault(), and (3) the root
      resource bundle (baseclass), in the following order from lower-level (more specific) to
      parent-level (less specific):

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

      But when I run the following test program the behavior is like this and this is what the "Java class library" book(page 1438) says too.

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


      Test Program(Test.java)
      -----------------------
      import java.util.*;

      public class Test {

         public ResourceBundle fetchResourceBundle(String language, String country){
            return(ResourceBundle.getBundle("MessagesBundle",
                                             new Locale(language, country)));

         }

         public void testCase1() {
            ResourceBundle rb = fetchResourceBundle("fr", "FR");

            System.out.println(rb.getString("greetings"));
            System.out.println(rb.getString("farewell"));
            System.out.println(rb.getString("inquiry"));
         }

         public static void main(String[] args) {
            Test test = new Test();

            test.testCase1();
         }
      }

      MessagesBundle.java
      --------------------
      import java.util.*;

      public class MessagesBundle extends ResourceBundle {

         public Object handleGetObject(String key) {
            if(key.equals("greetings"))
               return "Hi";
            if(key.equals("farewell"))
               return "Goodbye";
            if(key.equals("inquiry"))
               return "How are you?";
            return null;
         }

        String [] keys = {"greetings", "farewell", "inquiry"};

        public Enumeration getKeys() {
           return new Enumeration() {
              int count;
       
              public boolean hasMoreElements() {
                 return count < keys.length;
              }

              public Object nextElement() {
                 if(count < keys.length) {
                    return keys[count++];
                 }
                 throw new NoSuchElementException("Enumeration");
             }
          };
        }
      }

      MessagesBundle_en_US.java
      -------------------------
      import java.util.*;

      public class MessagesBundle_en_US extends ResourceBundle {

         public Object handleGetObject(String key) {
            if(key.equals("greetings"))
               return "Hello";
            return null;
         }

         public Enumeration getKeys() {
           if(parent != null)
              return parent.getKeys();
           return null;
          
         }
      }

      MessagesBundle_fr_Fr.java
      --------------------------
      import java.util.*;

      public class MessagesBundle_fr_FR extends ResourceBundle {

         public Object handleGetObject(String key) {
            if(key.equals("farewell"))
               return "Au revoir";
            return null;
         }

        public Enumeration getKeys() {
           if(parent != null)
              return parent.getKeys();
           return null;
        }
      }

      Run result
      ----------
      akila@sectra > /import/java/jdk1.3.1/solsparc/bin/java Test
      Hi
      Au revoir
      How are you?

      According to the spec the expected result should be
      Hello
      Au revoir
      How are you?

      Same behavior is seen 1.4 also, but the 1.4 spec does not say anything about
      lookup search order.



      ###@###.### 2002-01-16

            nlindenbsunw Norbert Lindenberg (Inactive)
            aksivaku Akila Sivakumar (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: