Locating, Loading, and Searching Resource Bundles

Once the necessary resource bundle families have been specified, the application can access the resources in a specific resource bundle family for a particular locale using the services of the java.util.ResourceBundle class. First, a locale-specific resource bundle is created from a resource bundle family using the getBundle() method—an elaborate process explained later in this section.

Click here to view code image

static ResourceBundle getBundle(String baseName)
static ResourceBundle getBundle(String baseName, Locale locale)

Return a resource bundle using the specified base name for a resource bundle family, either for the default locale or for the specified locale, respectively.

The resource bundle returned by this method is chained to other resource bundles (called parent resource bundles) that are searched if the key-based lookup in this resource bundle fails to find the value of a resource.

An unchecked java.util.MissingResourceException is thrown if no resource bundle for the specified base name can be found.

Also, it should be noted that bundles are loaded by the classloader, and thus their bundle names are treated exactly like fully qualified class names; that is, the package name needs to be specified when a resource bundle is placed in a package, such as “resources.BasicResources”.

If the resource bundle found by the getBundle() method was defined as a property resource file, its contents are read into an instance of the concrete class Prop-ertyResourceBundle (a subclass of the abstract ResourceBundle class) and this instance is returned.

The resource bundle instances returned by the getBundle() methods are immutable and are cached for reuse.

Click here to view code image

Object getObject(String key)
String getString(String key)

The first method returns an object for the given key from this resource bundle.

The second method returns a string for the given key from this resource bundle. This is a convenience method, if the value is a string. Calling this method is equivalent to (String) getObject(key). A ClassCastException is thrown if the object found for the given key is not a string.

An unchecked java.util.MissingResourceException is thrown if no value for the key can be found in this resource bundle or any of its parent resource bundles. A NullPointerException is thrown if the key is null.

Set<String> keySet()

Returns a Set of all keys contained in this ResourceBundle.

Locale getLocale()

Returns the locale of this resource bundle. The locale is derived from the naming scheme for resource bundles.

We will use Example 18.3 to illustrate salient features of localizing an application using resource bundles. The application has the following data, which should be localized:

Company: GLOBUS
Greeting: Hi!
Gratitude: Thank you!
Farewell: See you!

The example illustrates how the application can localize this data for the following locales: default (“en_US”), Norway (“no_NO”), French-Canada (“fr_CA”), and France (“fr_FR”). The company name is the same in every locale. The greeting, gratitude, and farewell messages are all grouped in one resource bundle family (BasicResources). For each locale, the application reads this data from the appropriate resource bundles and prints it to the terminal (see the output from Example 18.3).

Example 18.3 Using Resource Bundles (See Also Example 18.2)

Click here to view code image

import java.util.Locale;
import java.util.ResourceBundle;
public class UsingResourceBundles {
  // Supported locales:                                                   // (1)
  public static final Locale[] locales = {
    Locale.getDefault(),                                // Default: US (English)
    new Locale(“no”, “NO”),                             // Norway (Norwegian)
    Locale.FRANCE,                                      // France (French)
    Locale.CANADA_FRENCH                                // Canada (French)
  };
  // Localized data from property resource files:                         // (2)
  private static String company;
  private static String greeting;
  private static String gratitude;
  private static String farewell;
  public static void main(String[] args) {                                // (3)
    for (Locale locale : locales) {
      setLocaleSpecificData(locale);
      printLocaleSpecificData(locale);
    }
  }
  private static void setLocaleSpecificData(Locale locale) {              // (4)
    // Get resources from property resource files:
    ResourceBundle properties =
        ResourceBundle.getBundle(“resources.BasicResources”, locale);     // (5)
    company = properties.getString(“company”);                            // (6)
    greeting = properties.getString(“greeting”);
    gratitude = properties.getString(“gratitude”);
    farewell = properties.getString(“farewell”);
  }
  private static void printLocaleSpecificData(Locale locale) {            // (7)
    System.out.println(“Resources for ” + locale.getDisplayName() + ” locale:”);
    System.out.println(“Company: ” + company);
    System.out.println(“Greeting: ” + greeting);
    System.out.println(“Gratitude: ” + gratitude);
    System.out.println(“Farewell: ” + farewell);
    System.out.println();
  }
}

Output from running the program:

Click here to view code image

Resources for English (United States) locale:
Company: GLOBUS
Greeting: Hi!
Gratitude: Thank you!
Farewell: See you!
Resources for Norwegian (Norway) locale:
Company: GLOBUS
Greeting: Hei!
Gratitude: Takk!
Farewell: Ha det!
Resources for French (France) locale:
Company: GLOBUS
Greeting: Bonjour!
Gratitude: Merci!
Farewell: Au revoir!
Resources for French (Canada) locale:
Company: GLOBUS
Greeting: Bonjour!
Gratitude: Merci!
Farewell: Au revoir!

In Example 18.3, the static method getBundle() of the ResourceBundle class is called at (5) to create a resource bundle for the specified locale from the resource bundle family with the base name BasicResources. In Example 18.3, the resource bundle family is located in the resources directory, which is also the location of the package with the same name.

Click here to view code image

String company;

ResourceBundle properties =
    ResourceBundle.getBundle(“resources.BasicResources”, locale);    // (5)
company = properties.getString(“company”);                           // (6)

Since all the resource bundles in the resource bundle family BasicResources are property resource files, both the key and the value are String objects. It is convenient to use the getString() method in the ResourceBundle class to do lookup for resources, as shown at (6). The key is passed as an argument. Searching the resource for a given key is explained later in this section.

Leave a Reply

Your email address will not be published. Required fields are marked *