esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

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):

 
This code sample is only viewable to esus.com members
Login or become a member!


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:

This code sample is only viewable to esus.com members
Login or become a member!



LabelResourceBundle_nl_BE.java:

This code sample is only viewable to esus.com members
Login or become a member!



LabelResourceBundle_fr_BE.java:

This code sample is only viewable to esus.com members
Login or become a member!



LabelResourceBundle_en.java:

This code sample is only viewable to esus.com members
Login or become a member!



Output after running Main:

 
This code sample is only viewable to esus.com members
Login or become a member!




Further Information
Author of answer: Joris Van den Bogaert

Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.