Presents your JAVA E-NEWSLETTER for May 12, 2003 <-------------------------------------------> USE RESOURCE BUNDLES FOR INTERNATIONALIZATION Resource bundles, which have been a part of Java since 1.1, allow you to store and access locale-specific resource objects for use in your application. The simplest example of a locale-specific resource is probably a String object. For example, in the application below, the output needs to be a different message depending on the locale specified on the command line. To create an instance of a resource bundle, use the static method: java.util.ResourceBundle.getBundle(): ResourceBundle messages = ResourceBundle.getBundle("messages",Locale.ENGLISH); The getBundle() method has three forms that allow you to choose a specific locale and classloader, or you can choose to let them default to the default locale and classloader. As you can see from the preceding example code, getting an instance of a resource bundle is almost trivial. The example code locale is English, so when the method call above is executed, ResourceBundle searched the application's classpath for a file named "messages_en.properties". If the file is not found, a java.util.MissingResourceException error is thrown. In a real application, you would choose your locale based on a user action or system setting. Below is an example that selects the locale based on an argument passed at start up: Locale locale = Locale.ENGLISH; if ( args.length != 0 ) { locale = new Locale(args[0]); } ResourceBundle messages = ResourceBundle.getBundle("messages", locale); After you have an instance of resource bundle, you then call one of the get methods to get the localized resource you want. The ResourceBundle type defines methods for getting strings, arrays of strings, and objects. In this case, a String welcome message is the target: String message = messages.getString("welcome.message"); When the getString() method is called, the ResourceBundle looks for a String object in the previously loaded properties files that has the key name "welcome.message". If the key cannot be found, a java.util.MissingResourceException error is thrown. The properties files that are used look like this: messages_en.properties: welcome.message=Welcome to fantastic application and thanks for choosing Acme, Inc. messages_de.properties welcome.message=Heibetaen Sie willkommen zu phantastischer Anwendung und Dank zum Wahlen von Gipfel, Inc. messages_fr.properties: welcome.message=Bienvenu a l'application fantastique et remercie pour choisir de Point Culminant, Inc. (Please note that the text above was converted to French and German using an automatic translator.) You can use resource bundles for other objects besides strings. For instance, you can create your own implementations of resource bundles that are independent of properties files. Whether you need to store locale-specific strings or you need to handle more complex objects, resource bundles offer you a choice. import java.util.Locale; import java.util.ResourceBundle; public class ResourceBundleTip { public static void main(String[] args) { Locale locale = Locale.ENGLISH; if ( args.length != 0 ) { locale = new Locale(args[0]); } ResourceBundle messages = ResourceBundle.getBundle("messages", locale); String message = messages.getString("welcome.message"); System.out.println(message); } } ----------------------------------------