Using a ListResourceBundle

A ListResourceBundle is one where you centralize all your locale-specific strings and objects in a class. Provide your own versions by extending the abstract class ListResourceBundle and implementing the method getContents that returns an 2d array containing key/values pairs.

Keep in mind that:

  • keys are case-sensitive

  • a value doesn’t need to be a String, it may be an array or any type of object
  • you should put the resourcebundle in separate files and declare the class public

    To instantiate one of your resourcebundles, determine a locale (or use the default one) and call ResourceBundle.getResourceBundle(name of family class, locale). This method will look up the class using a certain lookup mechanism that goes from specific to general, in terms of locale information. For example, if the default locale is “en_US” and your specify “nl_BE”, the lookup will search for the following classes (assuming just MyResources.class exists):

       MyResources_nl_BE
       MyResources_nl
       MyResources_en_US
       MyResources_en
       MyResources
    

    If the lookup doesn’t find the class, it will throw a MissingResourceException.

    Once you have an instance of ResourceBundle, you can look up its contents using the three methods getString, getStringArray and getObject. If you want another type of array, for example an int[][], just use getObject and cast the result to int[][].

    Main.java:

    import java.util.*;
     
    public class Main {
       public static void main(String []args) {
          // use default locale 
          printLabelValues("LabelResourceBundle");
     
          // use locale en/../..
          Locale.setDefault(new Locale("en", "", ""));	
          printLabelValues("LabelResourceBundle");
     
          // use locale nl/BE/Unix
          Locale.setDefault(new Locale("nl", "BE", "Unix"));
          printLabelValues("LabelResourceBundle");
        
          // use locale nl					// MissingResourceException!
          Locale.setDefault(new Locale("nl", "", ""));	
          printLabelValues("LabelResourceBundle");
     
          // use locale ../BE/..				// MissingResourceException!
          Locale.setDefault(new Locale("", "BE", ""));
          printLabelValues("LabelResourceBundle");
     
          // use locale ../../.. 				// MissingResourceException!
          Locale.setDefault(new Locale("", "", ""));
          printLabelValues("LabelResourceBundle");
       }
     
       public static void printLabelValues(String resourceName) {
          ResourceBundle rb = ResourceBundle.getBundle(resourceName);  
     
          System.out.println("For locale: " + Locale.getDefault());
          System.out.println("tContents for LBL_NAME: " + rb.getString("LBL_NAME"));
          System.out.print("tContents for LBL_NUMBERS: ");
          String array[] = rb.getStringArray("LBL_NUMBERS");
          for (int i=0; i<array.length; i++) 
             System.out.print(array[i] + " ");
          System.out.println();
          System.out.println("tContents for LBL_OBJECT: of " + 
                                              rb.getObject("LBL_OBJECT").getClass());
       }
    }
    

    LabelResourceBundle_nl_BE.java:

    import java.util.*;
     
    public class LabelResourceBundle_nl_BE extends ListResourceBundle
    {
       private Object[][] contents = 
          { { "LBL_NAME", 		"Naam:" },
            { "LBL_NUMBERS",	new String[] { "een", "twee", "drie" }},
            { "LBL_OBJECT", 	new Integer(10) } };
      
       public Object[][] getContents() {
          return contents;
       }
    }
    

    LabelResourceBundle_fr_BE.java:

    import java.util.*;
     
    public class LabelResourceBundle_fr_BE extends ListResourceBundle
    {
       private Object[][] contents = 
          { { "LBL_NAME", 		"Nom:" },
            { "LBL_NUMBERS",	new String[] { "un", "deux", "troi" }},
            { "LBL_OBJECT", 	new Long(100) } };
     
       public Object[][] getContents() {
          return contents;
       }
    }
    

    LabelResourceBundle_en.java:

    import java.util.*;
     
    public class LabelResourceBundle_en extends ListResourceBundle
    {
       private Object[][] contents = 
          { { "LBL_NAME", 		"Name:" },
            { "LBL_NUMBERS",	new String[] { "one", "two", "three" }},
            { "LBL_OBJECT", 	new Double(1000.1) } };
     
       public Object[][] getContents() {
          return contents;
       }
    }
    

    Output after running Main:

    For locale: en_US
            Contents for LBL_NAME: Name:
            Contents for LBL_NUMBERS: one two three
            Contents for LBL_OBJECT: of class java.lang.Double
    For locale: en
            Contents for LBL_NAME: Name:
            Contents for LBL_NUMBERS: one two three
            Contents for LBL_OBJECT: of class java.lang.Double
    For locale: nl_BE_UNIX
            Contents for LBL_NAME: Naam:
            Contents for LBL_NUMBERS: een twee drie
            Contents for LBL_OBJECT: of class java.lang.Integer
    Exception in thread "main" java.util.MissingResourceException: Can't find resource
    for base name LabelResourceBundle, locale nl
            at java.util.ResourceBundle.getBundle(ResourceBundle.java:423)
            at java.util.ResourceBundle.getBundle(ResourceBundle.java:339)
            at Main.printLabelValues(Main.java, Compiled Code)
            at Main.main(Main.java:18)