A resource bundle is a resource used for internazionalization. It may be a property file or a class that maps key to value data in a locale-sensible way.
Here's an example
ResourceBundle rb = ResourceBundle.getBundle("ResBundle", Locale.ITALY); System.out.println(rb.getString("menu.history"));
The properties file should be named "ResBundle_it_IT.properties" and should contain:
menu.history=Mostra storico
The second type of resource bundle is a class that extend ListResourceBundle. This is usually used when the configuration is stored in a DB and when the value can be a string or any other object (the properties supports only strings).
import java.util.ListResourceBundle; public class ResBundle_it_IT extends ListResourceBundle { public Object[][] getContents() { // Retrieve data from DB here return list; } }
In both the methods the resource doesn't need to exactly match the locale. The VM simply loads the best match, based on the following logic:
1) First check for an exact match
2) Drop, one by one, the parts of the name of the locale, starting from the end.
3) Repeat the steps (1) and (2) with the default locale.
4) Search for the base class name without suffixes (in the example above ResBundle.properties)
5) The exception MissingBundleException is thrown.
It first look for a class resource bundle then repeat the above steps for a properties resource bundle.
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.