Package java.util
Class ResourceBundle
java.lang.Object
java.util.ResourceBundle
- Direct Known Subclasses:
ListResourceBundle,PropertyResourceBundle
public abstract class ResourceBundle extends Object
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.
- All bundles for the same group of resources share a common base bundle. This base bundle acts as the root and is the last fallback in case none of its children was able to respond to a request.
- The first level contains changes between different languages. Only the
differences between a language and the language of the base bundle need to be
handled by a language-specific
ResourceBundle. - The second level contains changes between different countries that use
the same language. Only the differences between a country and the country of
the language bundle need to be handled by a country-specific
ResourceBundle. - The third level contains changes that don't have a geographic reason
(e.g. changes that where made at some point in time like
PREEUROwhere the currency of come countries changed. The country bundle would return the current currency (Euro) and thePREEUROvariant bundle would return the old currency (e.g. DM for Germany).
- BaseName (base bundle)
- BaseName_de (german language bundle)
- BaseName_fr (french language bundle)
- BaseName_de_DE (bundle with Germany specific resources in german)
- BaseName_de_CH (bundle with Switzerland specific resources in german)
- BaseName_fr_CH (bundle with Switzerland specific resources in french)
- BaseName_de_DE_PREEURO (bundle with Germany specific resources in german of the time before the Euro)
- BaseName_fr_FR_PREEURO (bundle with France specific resources in french of the time before the Euro)
- Since:
- 1.1
- See Also:
Properties,PropertyResourceBundle,ListResourceBundle
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classResourceBundle.ControlResourceBundle.Control is a static utility class defines ResourceBundle load access methods, its default access order is as the same as before. -
Field Summary
Fields Modifier and Type Field Description protected ResourceBundleparentThe parent of thisResourceBundlethat is used if this bundle doesn't include the requested resource. -
Constructor Summary
Constructors Constructor Description ResourceBundle()Constructs a new instance of this class. -
Method Summary
Modifier and Type Method Description static voidclearCache()static voidclearCache(ClassLoader loader)booleancontainsKey(String key)static ResourceBundlegetBundle(String bundleName)Finds the named resource bundle for the defaultLocaleand the caller'sClassLoader.static ResourceBundlegetBundle(String bundleName, Locale locale)Finds the namedResourceBundlefor the specifiedLocaleand the callerClassLoader.static ResourceBundlegetBundle(String bundleName, Locale locale, ClassLoader loader)Finds the named resource bundle for the specifiedLocaleandClassLoader.static ResourceBundlegetBundle(String baseName, Locale targetLocale, ClassLoader loader, ResourceBundle.Control control)Finds the named resource bundle for the specified base name and control.static ResourceBundlegetBundle(String baseName, Locale targetLocale, ResourceBundle.Control control)Finds the named resource bundle for the specified base name and control.static ResourceBundlegetBundle(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 thisResourceBundle.LocalegetLocale()Gets theLocaleof thisResourceBundle.ObjectgetObject(String key)Returns the named resource from thisResourceBundle.StringgetString(String key)Returns the named string resource from thisResourceBundle.String[]getStringArray(String key)Returns the named resource from thisResourceBundle.protected abstract ObjecthandleGetObject(String key)Returns the named resource from thisResourceBundle, or null if the resource is not found.protected Set<String>handleKeySet()Set<String>keySet()protected voidsetParent(ResourceBundle bundle)Sets the parent resource bundle of thisResourceBundle.
-
Field Details
-
parent
The parent of thisResourceBundlethat is used if this bundle doesn't include the requested resource.
-
-
Constructor Details
-
ResourceBundle
public ResourceBundle()Constructs a new instance of this class.
-
-
Method Details
-
getBundle
Finds the named resource bundle for the defaultLocaleand the caller'sClassLoader.- Parameters:
bundleName- the name of theResourceBundle.- Returns:
- the requested
ResourceBundle. - Throws:
MissingResourceException- if theResourceBundlecannot be found.
-
getBundle
Finds the namedResourceBundlefor the specifiedLocaleand the callerClassLoader.- Parameters:
bundleName- the name of theResourceBundle.locale- theLocale.- Returns:
- the requested resource bundle.
- Throws:
MissingResourceException- if the resource bundle cannot be found.
-
getBundle
public static ResourceBundle getBundle(String bundleName, Locale locale, ClassLoader loader) throws MissingResourceExceptionFinds the named resource bundle for the specifiedLocaleandClassLoader. The passed base name andLocaleare used to create resource bundle names. The first name is created by concatenating the base name with the result ofLocale.toString(). From this name all parent bundle names are derived. Then the same thing is done for the defaultLocale. This results in a list of possible bundle names. Example For the basename "BaseName", theLocaleof the German part of Switzerland (de_CH) and the defaultLocaleen_US the list would look something like this:- BaseName_de_CH
- BaseName_de
- Basename_en_US
- Basename_en
- BaseName
ResourceBundlewith 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.propertiesfile 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 callingClassLoader.getResource(String)it is used to initialize aPropertyResourceBundle. If this succeeds, it will also load the parents of thisResourceBundle. 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- the name of theResourceBundle.locale- theLocale.loader- theClassLoaderto use.- Returns:
- the requested
ResourceBundle. - Throws:
MissingResourceException- if theResourceBundlecannot be found.
-
getBundle
Finds the named resource bundle for the specified base name and control.- Parameters:
baseName- the base name of a resource bundlecontrol- the control that control the access sequence- Returns:
- the named resource bundle
- Since:
- 1.6
-
getBundle
public static ResourceBundle getBundle(String baseName, Locale targetLocale, ResourceBundle.Control control)Finds the named resource bundle for the specified base name and control.- Parameters:
baseName- the base name of a resource bundletargetLocale- the target locale of the resource bundlecontrol- the control that control the access sequence- Returns:
- the named resource bundle
- Since:
- 1.6
-
getBundle
public static 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- the base name of a resource bundletargetLocale- the target locale of the resource bundleloader- the class loader to load resourcecontrol- the control that control the access sequence- Returns:
- the named resource bundle
- Since:
- 1.6
-
getKeys
Returns the names of the resources contained in thisResourceBundle.- Returns:
- an
Enumerationof the resource names.
-
getLocale
Gets theLocaleof thisResourceBundle. In case a bundle was not found for the requestedLocale, this will return the actualLocaleof this resource bundle that was found after doing a fallback.- Returns:
- the
Localeof thisResourceBundle.
-
getObject
Returns the named resource from thisResourceBundle. If the resource cannot be found in this bundle, it falls back to the parent bundle (if it's not null) by calling thehandleGetObject(java.lang.String)method. If the resource still can't be found it throws aMissingResourceException.- Parameters:
key- the name of the resource.- Returns:
- the resource object.
- Throws:
MissingResourceException- if the resource is not found.
-
getString
Returns the named string resource from thisResourceBundle.- Parameters:
key- the name of the resource.- Returns:
- the resource string.
- Throws:
MissingResourceException- if the resource is not found.ClassCastException- if the resource found is not a string.- See Also:
getObject(String)
-
getStringArray
Returns the named resource from thisResourceBundle.- Parameters:
key- the name of the resource.- Returns:
- 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:
getObject(String)
-
handleGetObject
Returns the named resource from thisResourceBundle, or null if the resource is not found.- Parameters:
key- the name of the resource.- Returns:
- the resource object.
-
setParent
Sets the parent resource bundle of thisResourceBundle. The parent is searched for resources which are not found in thisResourceBundle.- Parameters:
bundle- the parentResourceBundle.
-
clearCache
public static void clearCache() -
clearCache
-
containsKey
-
keySet
-
handleKeySet
-