Abstract Class ResourceBundle in Java

digitization, transformation, binary

Abstract Class ResourceBundle in Java

ResourceBundle is an abstract class that allows subclasses to define sets of localized resources which can then be dynamically loaded as needed by internationalized programs. Such resources may include user-visible text and images that appear in an application, and even more complex things such as Menu objects.

The ResourceBundle class is an abstract class that represents a set of localized data. An application retrieves a ResourceBundle based on its locale. A ResourceBundle can contain GUI labels and other locale-specific information that the application needs to run in a specific locale.

Ads code goes here

Conceptually, a resource bundle is a set of related classes that all inherit from a particular subclass of ResourceBundle. The base resource bundle defines all of the resources for a particular application, while each of the subclasses specifies the appropriate values for a particular locale. Each subclass has the same base name, plus a suffix that identifies its locale.

A static method, getBundle(), is used to locate a resource bundle for a particular locale. This method searches for resources in two forms. First, it looks for a subclass of ResourceBundle or ListResourceBundle with the appropriate name. If one is found, an instance of the class is created and returned. If no appropriate subclass can be found, getBundle() then searches for a property file with the appropriate name. If one is found, a PropertyResourceBundle is created from the file and returned.

The getBundle() method constructs a name from a specified base resource name and the locale. It then searches for either a class or a property file with this name. If the method fails to find an exact match, it tries to find a close match.

READ  Class FileWriter in Java

The method constructs names by dropping to the next name on the list if the current name cannot be found:

  • base + “_” + language + “_” + country + “_” + variant
  • base + “_” + language + “_” + country
  • base + “_” + language
  • base
  • base + “_” + default language + “_” + default country + “_” + default variant
  • base + “_” + default language + “_” + default country
  • base + “_” + default language

For example, if we call getBundle(‘Labels’, new Locale(‘it’, ‘IT’, ‘Be’)), the method looks for a
class or property file with one of the following names (assuming the default locale is the United States):

  • Labels_it_IT_Be
  • Labels_it_IT
  • Labels_it
  • Labels
  • Labels_en_US_Be
  • Labels_en_US
  • Labels_en

A particular ResourceBundle object contains a set of key/value pairs that define the resources for a particular application. The keys are always String objects that name the resources, while the values can be any sort of object needed for the application. The ResourceBundle class defines convenience methods for retrieving String and String[] objects. If we need to use other kinds of objects, we can use the getObject() method to retrieve them and simply cast the results to the appropriate types.

We need to use getBundle() to load a ResourceBundle subclass that is appropriate for the default or the specified locale.
We need to use getObject(), getString(), and getStringArray() to look up a named resource in a bundle. To define
a bundle, provide implementations of handleGetObject() and getKeys(). It is often easier, however, to subclass ListResourceBundle, or to provide a property file that is used by PropertyResourceBundle.

The name of any localized ResourceBundle class we define should include the locale language code, and, optionally, the locale country code.

The structure of the abstract class ResourceBundle is given by

public abstract class java.util.ResourceBundle extends java.lang.Object {
// Default Constructor: 
public ResourceBundle();
// Protected Instance Variables
protected ResourceBundle parent;
// Class Methods
public static final ResourceBundle getBundle(String baseName) throws MissingResourceException;
public static final ResourceBundle getBundle(String baseName, Locale locale);
// Public Instance Methods
public abstract Enumeration getKeys();
public final Object getObject(String key) throws MissingResourceException;
public final String getString(String key) throws MissingResourceException;
public final String[] getStringArray(String key) throws MissingResourceException;
// Protected Instance Methods
protected abstract Object handleGetObject(String key) throws MissingResourceException;
protected void setParent(ResourceBundle parent);
}

The details of the class structure are given as follows:

protected ResourceBundle parent;

protected ResourceBundle parent represents the parent ResourceBundle of this ResourceBundle. If this ResourceBundle does not contain a particular resource, its parent is searched. The parent can be set using setParent().

public final static ResourceBundle getBundle(String baseName);

public final static ResourceBundle getBundle(String baseName) method finds or constructs the appropriate ResourceBundle subclass specified by baseName and localized for the default locale. See the description of the ResourceBundle class for more information about how this method works.

READ  How To Communicate Between Two Machines Using UDP in Java

This method returns the named ResourceBundle for the default locale.

Parameter
baseName – The resource name.

public final static ResourceBundle getBundle(String baseName, Locale locale);

public final static ResourceBundle getBundle(String baseName, Locale locale) method finds or constructs the appropriate ResourceBundle subclass specified by baseName and localized for the given locale. See the description of the ResourceBundle class for more information about how this method works.

This method returns the named ResourceBundle for the given locale.

Parameter
baseName – The resource name.
locale – The Locale to use.

public abstract Enumeration getKeys();

public abstract Enumeration getKeys() method returns an Enumeration that iterates through the keys in this ResourceBundle. A subclass of ResourceBundle must provide an implementation for this method.

This method returns the keys in the ResourceBundle as an Enumeration.

public final Object getObject(String key);

public final Object getObject(String key); the method returns the named resource as an Object. If the named resource is not found in this ResourceBundle, the parent ResourceBundle is searched.

This method returns the Object identified by the key.

Parameter
key – The key of the resource to retrieve.

public final String getString(String key);

public final String getString(String key) method returns the named resource as a String object. This method is a convenience routine that calls getObject() and casts the result to a String object.

This method the String object identified by the key.

Parameter
key – The key of the resource to retrieve.

public final String[] getStringArray(String key);

public final String[] getStringArray(String key) method returns the named resource as an array of String objects. This method is a convenience routine that calls getObject() and casts the result to a String[] object.

READ  Dangling Else Problem in Java

This method the String[] array identified by the key.

Parameter
key – The key of the resource to retrieve.

protected abstract Object handleGetObject(String key);

protected abstract Object handleGetObject(String key) method returns the resource that corresponds to the given key. This method is called directly by getObject(), so it is called indirectly by getMenu(), getMenuBar(), getString(), and getStringArray().

A subclass of ResourceBundle must provide an implementation for this method.

This method returns the resource that corresponds to this key.

Parameter
key – The key of the resource to retrieve.

protected void setParent(ResourceBundle parent);

protected void setParent(ResourceBundle parent) method sets the parent ResourceBundle of this ResourceBundle. If a requested resource cannot be found in this ResourceBundle, the parent is searched.

Parameter
parent – The new parent of this ResourceBundle.

Apart from these ResourceBundle class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *