The ResourceBundle, ListResourceBundle, and PropertyResourceBundle Classes

ResourceBundle loads different properties file based on the value from Locale. If a requested locale matches the language code, then that version of the resource bundle is used. A country code is a two-character uppercase identifier. For example, AU for Australia or IN for India.

Suppose we have a resource bundle with a family name of SampleRB. The family name is the resource file name without locale name. We can have have a German version called SampleRB_de. And we can have another Russian version called SampleRB_ru. A resource bundle that has only the family name is the default bundle. It is used when no language-specific bundles are applicable.

If the locale is Locale.GERMAN, SampleRB_de will be used.

We can add language code to a country code. The language codes are defined by ISO standard 639 and the country codes by ISO standard 3166.

The methods defined by ResourceBundle are summarized in the following table.

There are two subclasses of ResourceBundle. The first is PropertyResourceBundle, which manages resources by using property files. PropertyResourceBundle adds no methods of its own.

The second is the abstract class ListResourceBundle, which manages resources in an array of key/value pairs.

ListResourceBundle adds the method getContents( ), which all subclasses must implement.

It is shown here:


protected abstract Object[ ][ ] getContents( )

It returns a two-dimensional array that contains key/value pairs that represent resources.

The keys must be strings. The values are typically strings, but can be other types of objects.

The following example shows two members of a resource bundle family with the base name "MyResources". "MyResources" is the default member of the bundle family, and "MyResources_fr" is the French member.


public class MyResources extends ListResourceBundle {
     protected Object[][] getContents() {
         return new Object[][] = {
             {"Name", "disk"}, 
         };
     }
 }

 public class MyResources_fr extends ListResourceBundle {
     protected Object[][] getContents() {
         return new Object[][] = {
             {"Name", "disque"},
         };
     }
 }
Home 
  Java Book 
    Essential Classes  

ResourceBundle:
  1. The ResourceBundle, ListResourceBundle, and PropertyResourceBundle Classes
  2. Use getBundle(String baseName) to load resource file
  3. Use getBundle(String baseName, Locale locale) to load resource based on Locale information
  4. getString(String key) load the value by the key