Skip to content

Most visited

Recently visited

navigation
Added in API level 1

ResourceBundle

public abstract class ResourceBundle
extends Object

java.lang.Object
   ↳ java.util.ResourceBundle
Known Direct Subclasses


ResourceBundle is an abstract class which is the superclass of classes which provide Locale-specific resources. A bundle contains a number of named resources, where the names are Strings. A bundle may have a parent bundle, and when a resource is not found in a bundle, the parent bundle is searched for the resource. If the fallback mechanism reaches the base bundle and still can't find the resource it throws a MissingResourceException.

Examples It's also possible to create variants for languages or countries. This can be done by just skipping the country or language abbreviation: BaseName_us__POSIX or BaseName__DE_PREEURO. But it's not allowed to circumvent both language and country: BaseName___VARIANT is illegal.

See also:

Summary

Nested classes

class ResourceBundle.Control

ResourceBundle.Control is a static utility class defines ResourceBundle load access methods, its default access order is as the same as before. 

Fields

protected ResourceBundle parent

The parent of this ResourceBundle that is used if this bundle doesn't include the requested resource.

Public constructors

ResourceBundle()

Constructs a new instance of this class.

Public methods

static void clearCache(ClassLoader loader)
static void clearCache()
boolean containsKey(String key)
static ResourceBundle getBundle(String bundleName, Locale locale, ClassLoader loader)

Finds the named resource bundle for the specified Locale and ClassLoader.

static ResourceBundle getBundle(String baseName, Locale targetLocale, ResourceBundle.Control control)

Finds the named resource bundle for the specified base name and control.

static ResourceBundle getBundle(String bundleName)

Finds the named resource bundle for the default Locale and the caller's ClassLoader.

static ResourceBundle getBundle(String baseName, Locale targetLocale, ClassLoader loader, ResourceBundle.Control control)

Finds the named resource bundle for the specified base name and control.

static ResourceBundle getBundle(String bundleName, Locale locale)

Finds the named ResourceBundle for the specified Locale and the caller ClassLoader.

static ResourceBundle getBundle(String baseName, ResourceBundle.Control control)

Finds the named resource bundle for the specified base name and control.

abstract Enumeration<String> getKeys()

Returns the names of the resources contained in this ResourceBundle.

Locale getLocale()

Gets the Locale of this ResourceBundle.

final Object getObject(String key)

Returns the named resource from this ResourceBundle.

final String getString(String key)

Returns the named string resource from this ResourceBundle.

final String[] getStringArray(String key)

Returns the named resource from this ResourceBundle.

Set<String> keySet()

Protected methods

abstract Object handleGetObject(String key)

Returns the named resource from this ResourceBundle, or null if the resource is not found.

Set<String> handleKeySet()
void setParent(ResourceBundle bundle)

Sets the parent resource bundle of this ResourceBundle.

Inherited methods

From class java.lang.Object

Fields

parent

Added in API level 1
ResourceBundle parent

The parent of this ResourceBundle that is used if this bundle doesn't include the requested resource.

Public constructors

ResourceBundle

Added in API level 1
ResourceBundle ()

Constructs a new instance of this class.

Public methods

clearCache

Added in API level 9
void clearCache (ClassLoader loader)

Parameters
loader ClassLoader

clearCache

Added in API level 9
void clearCache ()

containsKey

Added in API level 9
boolean containsKey (String key)

Parameters
key String
Returns
boolean

getBundle

Added in API level 1
ResourceBundle getBundle (String bundleName, 
                Locale locale, 
                ClassLoader loader)

Finds the named resource bundle for the specified Locale and ClassLoader. The passed base name and Locale are used to create resource bundle names. The first name is created by concatenating the base name with the result of toString(). From this name all parent bundle names are derived. Then the same thing is done for the default Locale. This results in a list of possible bundle names. Example For the basename "BaseName", the Locale of the German part of Switzerland (de_CH) and the default Locale en_US the list would look something like this:

  1. BaseName_de_CH
  2. BaseName_de
  3. Basename_en_US
  4. Basename_en
  5. BaseName
This list also shows the order in which the bundles will be searched for a requested resource in the German part of Switzerland (de_CH). As a first step, this method tries to instantiate a ResourceBundle with the names provided. If such a class can be instantiated and initialized, it is returned and all the parent bundles are instantiated too. If no such class can be found this method tries to load a .properties file with the names by replacing dots in the base name with a slash and by appending ".properties" at the end of the string. If such a resource can be found by calling getResource(String) it is used to initialize a PropertyResourceBundle. If this succeeds, it will also load the parents of this ResourceBundle. For compatibility with older code, the bundle name isn't required to be a fully qualified class name. It's also possible to directly pass the path to a properties file (without a file extension).

Parameters
bundleName String: the name of the ResourceBundle.
locale Locale: the Locale.
loader ClassLoader: the ClassLoader to use.
Returns
ResourceBundle the requested ResourceBundle.
Throws
MissingResourceException if the ResourceBundle cannot be found.

getBundle

Added in API level 9
ResourceBundle getBundle (String baseName, 
                Locale targetLocale, 
                ResourceBundle.Control control)

Finds the named resource bundle for the specified base name and control.

Parameters
baseName String: the base name of a resource bundle
targetLocale Locale: the target locale of the resource bundle
control ResourceBundle.Control: the control that control the access sequence
Returns
ResourceBundle the named resource bundle

getBundle

Added in API level 1
ResourceBundle getBundle (String bundleName)

Finds the named resource bundle for the default Locale and the caller's ClassLoader.

Parameters
bundleName String: the name of the ResourceBundle.
Returns
ResourceBundle the requested ResourceBundle.
Throws
MissingResourceException if the ResourceBundle cannot be found.

getBundle

Added in API level 9
ResourceBundle getBundle (String baseName, 
                Locale targetLocale, 
                ClassLoader loader, 
                ResourceBundle.Control control)

Finds the named resource bundle for the specified base name and control.

Parameters
baseName String: the base name of a resource bundle
targetLocale Locale: the target locale of the resource bundle
loader ClassLoader: the class loader to load resource
control ResourceBundle.Control: the control that control the access sequence
Returns
ResourceBundle the named resource bundle

getBundle

Added in API level 1
ResourceBundle getBundle (String bundleName, 
                Locale locale)

Finds the named ResourceBundle for the specified Locale and the caller ClassLoader.

Parameters
bundleName String: the name of the ResourceBundle.
locale Locale: the Locale.
Returns
ResourceBundle the requested resource bundle.
Throws
MissingResourceException if the resource bundle cannot be found.

getBundle

Added in API level 9
ResourceBundle getBundle (String baseName, 
                ResourceBundle.Control control)

Finds the named resource bundle for the specified base name and control.

Parameters
baseName String: the base name of a resource bundle
control ResourceBundle.Control: the control that control the access sequence
Returns
ResourceBundle the named resource bundle

getKeys

Added in API level 1
Enumeration<String> getKeys ()

Returns the names of the resources contained in this ResourceBundle.

Returns
Enumeration<String> an Enumeration of the resource names.

getLocale

Added in API level 1
Locale getLocale ()

Gets the Locale of this ResourceBundle. In case a bundle was not found for the requested Locale, this will return the actual Locale of this resource bundle that was found after doing a fallback.

Returns
Locale the Locale of this ResourceBundle.

getObject

Added in API level 1
Object getObject (String key)

Returns the named resource from this ResourceBundle. If the resource cannot be found in this bundle, it falls back to the parent bundle (if it's not null) by calling the handleGetObject(String) method. If the resource still can't be found it throws a MissingResourceException.

Parameters
key String: the name of the resource.
Returns
Object the resource object.
Throws
MissingResourceException if the resource is not found.

getString

Added in API level 1
String getString (String key)

Returns the named string resource from this ResourceBundle.

Parameters
key String: the name of the resource.
Returns
String the resource string.
Throws
MissingResourceException if the resource is not found.
ClassCastException if the resource found is not a string.

See also:

getStringArray

Added in API level 1
String[] getStringArray (String key)

Returns the named resource from this ResourceBundle.

Parameters
key String: the name of the resource.
Returns
String[] the resource string array.
Throws
MissingResourceException if the resource is not found.
ClassCastException if the resource found is not an array of strings.

See also:

keySet

Added in API level 9
Set<String> keySet ()

Returns
Set<String>

Protected methods

handleGetObject

Added in API level 1
Object handleGetObject (String key)

Returns the named resource from this ResourceBundle, or null if the resource is not found.

Parameters
key String: the name of the resource.
Returns
Object the resource object.

handleKeySet

Added in API level 9
Set<String> handleKeySet ()

Returns
Set<String>

setParent

Added in API level 1
void setParent (ResourceBundle bundle)

Sets the parent resource bundle of this ResourceBundle. The parent is searched for resources which are not found in this ResourceBundle.

Parameters
bundle ResourceBundle: the parent ResourceBundle.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.