ResourceBundle and ListResourceBundle class in Java with Examples

The ResourceBundle and ListResourceBundle classes are part java.util package. These classes are designed to aid in the internationalization of programs.
ResourceBundle: The class ResourceBundle is an abstract class. It defines methods that enable you to manage a collection of locale-sensitive resources. Resource bundles are identified by their family name. To the family name is added a two-character lowercase language code that specifies the language. We can also specify a country code after the language code. It is a two-character uppercase identifier and is preceded by an underscore when linked to a resource bundle name.
Class Hierarchy:
java.lang.Object ↳java.util.ResourceBundle
Constructors:
1. ResourceBundle(): The default constructor which is mainly designed for use by the subclasses and the factory methods.
public ResourceBundle()
Methods:
1. clearCache(): This method deletes all resource bundles from the cache that were loaded by the default class loader.
static final void clearCache()
2. containsKey(): This method returns true if the passed string argument is a key within the invoking resource bundle.
boolean containsKey(String k)
3. getBundle(): This method loads the resource bundle with the given name and the specified locale.
static final ResourceBundle getBundle(String familyName) static final ResourceBundle getBundle(String familyName, Locale loc)
4. setParent(): This method sets the passed bundle as parent of the invoking bundle. In case of a lookup, if the key is not found in the invoking object, then it is looked up in the parent bundle.
protected void setParent(ResourceBundle parent)
5. getObject(): This method retrieves and returns the Object associated with the key passed as argument either from the current resource bundle or the parent.
public final Object getObject(String key)
6. getHandleObject(): This method returns the object associated with the given key from the resource bundle. If no object is available null is returned.
protected abstract Object handleGetObject(String key)
7. getString(): This method retrieves and returns the string associated with the key passed as argument either from the current resource bundle or the parent.
public final String getString(String key)
8. getStringArray(): This method retrieves and returns the string array associated with the key passed as argument either from the current resource bundle or the parent.
public final String[] getStringArray(String key)
9. getLocale(): This method returns the Locale associated with the current bundle.
public Locale getLocale()
10. containsKey(): This method checks whether a given key exists within a resource bundle or its parent or not.
public boolean containsKey(String key)
11. keySet(): This method returns the set of all the keys in the current bundle or its parent bundle.
public Set keySet()
ListResourceBundle: It is a subclass of ResourceBundle. It is an abstract class, which manages the resources in an array of key/value pairs. It adds only one new method getContents(), which must be implemented by every subclass.
Constructors: 
 
1. ListResourceBundle(): The default constructor to create an object. 
public ListResourceBundle()
Methods:
1. getContents(): This method returns a two dimensional array that contains key/value pairs that represent resources.
protected abstract Object[][] getContents()
2. handleGetObject(): This method returns the object associated with the key in the current bundle if it exists.
public final Object handleGetObject(String key)
3. getKeys(): This method returns an enumeration of the keys in the resource bundle.
public Enumeration getKeys()
4. handleKeySet(): This method returns the set of all the keys in the current resource bundle.
protected Set handleKeySet()
Example to demonstrate the use of a ResourceBundle
Java
import java.util.Locale;import java.util.ResourceBundle;import java.util.ListResourceBundle;class SampleRB extends ListResourceBundle {    protected Object[][] getContents()    {        Object[][] resources = new Object[3][2];        resources[0][0] = "title";        resources[0][1] = "My Program";        resources[1][0] = "StopText";        resources[1][1] = "Stop";        resources[2][0] = "StartText";        resources[2][1] = "Start";        return resources;    }}class SampleRB_de extends ListResourceBundle {    protected Object[][] getContents()    {        Object[][] resources = new Object[3][2];        resources[0][0] = "title";        resources[0][1] = "Mein Program";        resources[1][0] = "StopText";        resources[1][1] = "Anschlag";        resources[2][0] = "StartText";        resources[2][1] = "Anfang";        return resources;    }}public class LRBDemo {    public static void main(String[] args)    {        ResourceBundle rd            = ResourceBundle                  .getBundle("SampleRB",                             Locale.ENGLISH);        System.out.println("English Version:");        System.out.println("String for Title key: "                           + rd.getString("title"));        System.out.println("String for StopText key: "                           + rd.getString("StopText"));        System.out.println("String for StartText key: "                           + rd.getString("StartText"));        rd = ResourceBundle                 .getBundle("SampleRB",                            Locale.GERMAN);        System.out.println("\nGerman Version");        System.out.println("String for Title key: "                           + rd.getString("title"));        System.out.println("String for StopText key: "                           + rd.getString("StopText"));        System.out.println("String for StartText key: "                           + rd.getString("StartText"));    }} | 
Output:
English Version: String for Title key: My Program String for StopText key: Stop String for StartText key: Start German Version String for Title key: Mein Program String for StopText key: Anschlag String for StartText key: Anfang
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html
 
				
					


