How do I use a ResourceBundle?

John Zukowski

In order to localize labels, you need to pull them all out and do lookups at runtime. The Java means to do this is through the ResourceBundle class. Resource bundles provide a simple key-value lookup and may be provided via .class or .properties files. (Other means can also be provided, however you have to do all the work yourself.)

To provide a lookup list via a class, you subclass ListResourceBundle and override the getContents method to return an Object[][].


import java.util.ListResourceBundle;
public class TrainingLabelsBundle
    extends ListResourceBundle {
  static final Object[][] contents = {
    { "Date",     "Date: " },
    { "Cost",     "Cost: " }, 
    { "Location", "Location: " },
    { "Title",    "Training" }
  };
  public Object[][] getContents() {
    return contents;
  }
}

This defines four strings that can be localized:

  • Date
  • Cost
  • Location
  • Title

Then, lookup of resources is a two step process: load resource list and search. To load the list, use ResourceBundle.getBundle(). This will load the bundle for the appropriate Locale. Since we only have one so far, and it is the default, that is what is loaded.

Locale locale = getLocale();
ResourceBundle labels = ResourceBundle.getBundle (
    "TrainingLabelsBundle", locale);

Once you have a bundle, you lookup entries via its getString() method:

p.add (new Label (labels.getString("Date")));

For instance the following would look up all four and place them in labels:

ResourceBundle labels = ResourceBundle.getBundle (
    "TrainingLabelsBundle", locale);
p.add (new Label (labels.getString("Date")));
p.add (new Label (labels.getString ("Cost")));
p.add (new Label (labels.getString ("Location")));
add (new Label (labels.getString ("Title"),
    Label.CENTER), BorderLayout.NORTH);
}

Then, if we want the program to support Italy and Finland locales, we have to create two new classes of TrainingLabelsBundle_it and TrainingLabelsBundle_fi. Only those entries that are different in the new locale need to be specified in the bundle. Then, based on whatever the current locale is, the getBundle() method fetches the appropriate class.

For Finnish:


public class TrainingLabelsBundle_fi 
          extends ListResourceBundle {
  static final Object[][] contents = {
    { "Date",     "Päivämäärä: " },
    { "Cost",     "Hinta: " }, 
    { "Location", "Sijainti: " },
    { "Title",    "Koulutus" }
  };
  public Object[][] getContents() {...}
}

For Italian:

import java.util.ListResourceBundle;
public class TrainingLabelsBundle_it extends ListResourceBundle {
  static final Object[][] contents = {
    { "Date",     "Data: " },
    { "Cost",     "Costo: " }, 
    { "Location", "Posto: " }
  };
  // The Training title remains the same (or addestramento)
  public Object[][] getContents() {
    return contents;
  }
}

Property Resource Bundles

In addition to providing ListResourceBundle classes, you can provide PropertyResourceBundles. These are property files and are named Bundlename_Localization.properties.

To define three properties for the actual date, cost, and location, you create another bundle in filename TrainingSettingsBundle.properties:

Date=June 3, 1998
Cost=1200.00
Location=San Mateo

Access to PropertyResourceBundle members is identical to ListResourceBundle.

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact