|
Java™ by example!
|
|
|
How do I use a ListResourceBundle?
A ListResourceBundle is one where you centralize all your locale-specific strings and objects in a class. Provide your own versions by extending the abstract class ListResourceBundle and implementing the method getContents that returns an 2d array containing key/values pairs. Keep in mind that:- keys are case-sensitive
- a value doesn't need to be a String, it may be an array or any type of object
- you should put the resourcebundle in separate files and declare the class public
To instantiate one of your resourcebundles, determine a locale (or use the default one) and call ResourceBundle.getResourceBundle(name of family class, locale). This method will look up the class using a certain lookup mechanism that goes from specific to general, in terms of locale information. For example, if the default locale is "en_US" and your specify "nl_BE", the lookup will search for the following classes (assuming just MyResources.class exists):
If the lookup doesn't find the class, it will throw a MissingResourceException. Once you have an instance of ResourceBundle, you can look up its contents using the three methods getString, getStringArray and getObject. If you want another type of array, for example an int[][], just use getObject and cast the result to int[][]. Main.java:
LabelResourceBundle_nl_BE.java:
LabelResourceBundle_fr_BE.java:
LabelResourceBundle_en.java:
Output after running Main:
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|