Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.util

Class ResourceBundle

java.lang.Object
|
+--java.util.ResourceBundle


public abstract class ResourceBundle

extends Object

A resource bundle contains locale-specific data. If you need localized data, you can load a resource bundle that matches the locale with getBundle. Now you can get your object by calling getObject or getString on that bundle.

When a bundle is demanded for a specific locale, the ResourceBundle is searched in following order (def. language stands for the two letter ISO language code of the default locale (see Locale.getDefault()).

baseName_language code_country code_variant
baseName_language code_country code
baseName_language code
baseName_def. language_def. country_def. variant
baseName_def. language_def. country
baseName_def. language
baseName

A bundle is backed up by less specific bundles (omitting variant, country or language). But it is not backed up by the default language locale.

If you provide a bundle for a given locale, say Bundle_en_UK_POSIX, you must also provide a bundle for all sub locales, ie. Bundle_en_UK, Bundle_en, and Bundle.

When a bundle is searched, we look first for a class with the given name, then for a file with .properties extension in the classpath. The name must be a fully qualified classname (with dots as path separators).

(Note: This implementation always backs up the class with a properties file if that is existing, but you shouldn't rely on this, if you want to be compatible to the standard JDK.)

Since:Authors:See Also:

Field Summary

java.util.ResourceBundleparent

The parent bundle.

Constructor Summary

ResourceBundle()

The constructor.

Method Summary

static java.util.ResourceBundlegetBundle(java.lang.String baseName)

Get the appropriate ResourceBundle for the default locale.
static java.util.ResourceBundlegetBundle(java.lang.String baseName, java.util.Locale locale)

Get the appropriate ResourceBundle for the given locale.
static synchronized java.util.ResourceBundlegetBundle(java.lang.String baseName, java.util.Locale locale, java.lang.ClassLoader classLoader)

Get the appropriate ResourceBundle for the given locale.
java.util.EnumerationgetKeys()

This method should return all keys for which a resource exists; you should include the enumeration of any parent's keys, after filtering out duplicates.
java.util.LocalegetLocale()

Return the actual locale of this bundle.
java.lang.ObjectgetObject(java.lang.String key)

Get an object from this resource bundle.
java.lang.StringgetString(java.lang.String key)

Get a String from this resource bundle.
java.lang.String[]getStringArray(java.lang.String key)

Get an array of Strings from this resource bundle.
java.lang.ObjecthandleGetObject(java.lang.String key)

Override this method to provide the resource for a keys.
voidsetParent(java.util.ResourceBundle parent)

Set the parent of this bundle.

Field Details

parent

protected ResourceBundle parent

The parent bundle. This is consulted when you call getObject and there is no such resource in the current bundle. This field may be null.


Constructor Details

ResourceBundle

public ResourceBundle()

The constructor. It does nothing special.


Method Details

getBundle

public static final ResourceBundle getBundle(java.lang.String baseName)

Get the appropriate ResourceBundle for the default locale. This is like calling getBundle(baseName, Locale.getDefault(), getClass().getClassLoader(), except that any security check of getClassLoader won't fail.

Parameters:

Returns:

Throws:


getBundle

public static final ResourceBundle getBundle(java.lang.String baseName, java.util.Locale locale)

Get the appropriate ResourceBundle for the given locale. This is like calling getBundle(baseName, locale, getClass().getClassLoader(), except that any security check of getClassLoader won't fail.

Parameters:

Returns:

Throws:


getBundle

public static final synchronized ResourceBundle getBundle(java.lang.String baseName, java.util.Locale locale, java.lang.ClassLoader classLoader)

Get the appropriate ResourceBundle for the given locale. The following strategy is used:

A sequence of candidate bundle names are generated, and tested in this order, where the suffix 1 means the string from the specified locale, and the suffix 2 means the string from the default locale:

  • baseName + "_" + language1 + "_" + country1 + "_" + variant1
  • baseName + "_" + language1 + "_" + country1
  • baseName + "_" + language1
  • baseName + "_" + language2 + "_" + country2 + "_" + variant2
  • baseName + "_" + language2 + "_" + country2
  • baseName + "_" + language2
  • baseName

In the sequence, entries with an empty string are ignored. Next, getBundle tries to instantiate the resource bundle:

  • First, an attempt is made to load a class in the specified classloader which is a subclass of ResourceBundle, and which has a public constructor with no arguments, via reflection.
  • Next, a search is made for a property resource file, by replacing '.' with '/' and appending ".properties", and using ClassLoader.getResource(). If a file is found, then a PropertyResourceBundle is created from the file's contents.
If no resource bundle was found, a MissingResourceException is thrown.

Next, the parent chain is implemented. The remaining candidate names in the above sequence are tested in a similar manner, and if any results in a resource bundle, it is assigned as the parent of the first bundle using the setParent method (unless the first bundle already has a parent).

For example, suppose the following class and property files are provided: MyResources.class, MyResources_fr_CH.properties, MyResources_fr_CH.class, MyResources_fr.properties, MyResources_en.properties, and MyResources_es_ES.class. The contents of all files are valid (that is, public non-abstract subclasses of ResourceBundle with public nullary constructors for the ".class" files, syntactically correct ".properties" files). The default locale is Locale("en", "UK").

Calling getBundle with the shown locale argument values instantiates resource bundles from the following sources:

  • Locale("fr", "CH"): result MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class
  • Locale("fr", "FR"): result MyResources_fr.properties, parent MyResources.class
  • Locale("de", "DE"): result MyResources_en.properties, parent MyResources.class
  • Locale("en", "US"): result MyResources_en.properties, parent MyResources.class
  • Locale("es", "ES"): result MyResources_es_ES.class, parent MyResources.class
The file MyResources_fr_CH.properties is never used because it is hidden by MyResources_fr_CH.class.

Since:Parameters:

Returns:

Throws:


getKeys

public Enumeration getKeys()

This method should return all keys for which a resource exists; you should include the enumeration of any parent's keys, after filtering out duplicates.

Returns:


getLocale

public Locale getLocale()

Return the actual locale of this bundle. You can use it after calling getBundle, to know if the bundle for the desired locale was loaded or if the fall back was used.

Returns:


getObject

public final Object getObject(java.lang.String key)

Get an object from this resource bundle. This will call handleGetObject for this resource and all of its parents, until it finds a non-null resource.

Parameters:

Throws:


getString

public final String getString(java.lang.String key)

Get a String from this resource bundle. Since most localized Objects are Strings, this method provides a convenient way to get them without casting.

Parameters:

Throws:


getStringArray

public final String[] getStringArray(java.lang.String key)

Get an array of Strings from this resource bundle. This method provides a convenient way to get it without casting.

Parameters:

Throws:


handleGetObject

protected Object handleGetObject(java.lang.String key)

Override this method to provide the resource for a keys. This gets called by getObject. If you don't have a resource for the given key, you should return null instead throwing a MissingResourceException. You don't have to ask the parent, getObject() already does this; nor should you throw a MissingResourceException.

Parameters:

Returns:

Throws:


setParent

protected void setParent(java.util.ResourceBundle parent)

Set the parent of this bundle. The parent is consulted when you call getObject and there is no such resource in the current bundle.

Parameters: