public abstract class ResourceBundle extends Object
String for example, your program
can load it from the resource bundle that is appropriate for the current
user's locale. In this way, you can write program code that is largely
independent of the user's locale isolating most, if not all, of the
locale-specific information in resource bundles.
This allows you to write programs that can:
Resource bundles belong to families whose members share a common base name,
but whose names also have additional components that identify their locales.
For example, the base name of a family of resource bundles might be
"MyResources". The family should have a default resource bundle which simply
has the same name as its family - "MyResources" - and will be used as the
bundle of last resort if a specific locale is not supported. The family can
then provide as many locale-specific members as needed, for example a German
one named "MyResources_de".
Each resource bundle in a family contains the same items, but the items have
been translated for the locale represented by that resource bundle. For
example, both "MyResources" and "MyResources_de" may have a
If there are different resources for different countries, you can make
specializations: for example, "MyResources_de_CH" contains objects for the
German language (de) in Switzerland (CH). If you want to only modify some of
the resources in the specialization, you can do so.
When your program needs a locale-specific object, it loads the
Resource bundles contain key/value pairs. The keys uniquely identify a
locale-specific object in the bundle. Here's an example of a
You retrieve an object from resource bundle using the appropriate getter
method. Because "OkKey" and "CancelKey" are both strings, you would use
Besides
The Java 2 platform provides two subclasses of
If
The following is a very simple example of a
Example: String
that's used on a button for canceling operations. In "MyResources" the
String may contain "Cancel" and in "MyResources_de" it may
contain "Abbrechen". ResourceBundle
class using the getBundle method:
ResourceBundle myResources =
ResourceBundle.getBundle("MyResources", currentLocale);
ListResourceBundle
that contains two key/value pairs:
Keys are always
public class MyResources extends ListResourceBundle {
public Object[][] getContents() {
return contents;
}
static final Object[][] contents = {
// LOCALIZE THIS
{"OkKey", "OK"},
{"CancelKey", "Cancel"},
// END OF MATERIAL TO LOCALIZE
};
}
Strings. In this example,
the keys are "OkKey" and "CancelKey". In the above example, the values are
also Strings--"OK" and "Cancel"--but they don't have to be. The
values can be any type of object. getString to retrieve them:
The getter methods all require the key as an argument
and return the object if found. If the object is not found, the getter
method throws a
button1 = new Button(myResources.getString("OkKey"));
button2 = new Button(myResources.getString("CancelKey"));
MissingResourceException. getString, ResourceBundle also provides a method for
getting string arrays, getStringArray, as well as a generic
getObject method for any other type of object. When using
getObject, you'll have to cast the result to the appropriate
type. For example:
int[] myIntegers = (int[]) myResources.getObject("intList");
ResourceBundle,
ListResourceBundle and PropertyResourceBundle,
that provide a fairly simple way to create resources. As you saw briefly in
a previous example, ListResourceBundle manages its resource as
a List of key/value pairs. PropertyResourceBundle uses a
properties file to manage its resources. ListResourceBundle or PropertyResourceBundle do
not suit your needs, you can write your own ResourceBundle
subclass. Your subclasses must override two methods: handleGetObject
and getKeys(). ResourceBundle
subclass, MyResources, that manages two resources (for a larger number of
resources you would probably use a Hashtable). Notice that you
don't need to supply a value if a "parent-level" ResourceBundle
handles the same key with the same value (as for the okKey below).
You do not have to restrict yourself to using a single
family of
// default (English language, United States)
public class MyResources extends ResourceBundle {
public Object handleGetObject(String key) {
if (key.equals("okKey")) return "Ok";
if (key.equals("cancelKey")) return "Cancel";
return null;
}
}
// German language
public class MyResources_de extends MyResources {
public Object handleGetObject(String key) {
// don't need okKey, since parent level handles it.
if (key.equals("cancelKey")) return "Abbrechen";
return null;
}
}
ResourceBundles. For example, you could have a set of
bundles for exception messages, ExceptionResources (ExceptionResources_fr
, ExceptionResources_de, ...), and one for widgets, WidgetResource
(WidgetResources_fr, WidgetResources_de, ...);
breaking up the resources however you like.
ListResourceBundle,
PropertyResourceBundle| 限定符和类型 | 字段和说明 |
|---|---|
protected ResourceBundle |
parent
The parent bundle of this bundle.
|
| 构造器和说明 |
|---|
ResourceBundle()
Sole constructor.
|
| 限定符和类型 | 方法和说明 |
|---|---|
String |
format(String key,
Object[] args)
Retrieve a string from resource bundle and format it with specified args.
|
boolean |
getBoolean(String key)
Retrieve a boolean from bundle.
|
boolean |
getBoolean(String key,
boolean defaultValue)
Retrieve a boolean from bundle.
|
static ResourceBundle |
getBundle(String baseName)
Gets a resource bundle using the specified base name, the default locale,
and the caller's class loader.
|
static ResourceBundle |
getBundle(String baseName,
Locale locale)
Gets a resource bundle using the specified base name and locale, and the
caller's class loader.
|
static ResourceBundle |
getBundle(String baseName,
Locale locale,
ClassLoader loader) |
byte |
getByte(String key)
Retrieve a byte from bundle.
|
byte |
getByte(String key,
byte defaultValue)
Retrieve a byte from bundle.
|
char |
getChar(String key)
Retrieve a char from bundle.
|
char |
getChar(String key,
char defaultValue)
Retrieve a char from bundle.
|
Date |
getDate(String key)
Retrieve a date from bundle.
|
Date |
getDate(String key,
Date defaultValue)
Retrieve a date from bundle.
|
Date |
getDateTime(String key)
Retrieve a date + time from bundle.
|
Date |
getDateTime(String key,
Date defaultValue)
Retrieve a time from bundle.
|
double |
getDouble(String key)
Retrieve a double from bundle.
|
double |
getDouble(String key,
double defaultValue)
Retrieve a double from bundle.
|
float |
getFloat(String key)
Retrieve a float from bundle.
|
float |
getFloat(String key,
float defaultValue)
Retrieve a float from bundle.
|
int |
getInteger(String key)
Retrieve a integer from bundle.
|
int |
getInteger(String key,
int defaultValue)
Retrieve a integer from bundle.
|
abstract Enumeration |
getKeys()
Returns an enumeration of the keys.
|
Locale |
getLocale()
Returns the locale of this resource bundle.
|
long |
getLong(String key)
Retrieve a long from bundle.
|
long |
getLong(String key,
long defaultValue)
Retrieve a long from bundle.
|
Object |
getObject(String key)
Gets an object for the given key from this resource bundle or one of its
parents.
|
short |
getShort(String key)
Retrieve a short from bundle.
|
short |
getShort(String key,
short defaultValue)
Retrieve a short from bundle.
|
String |
getString(String key)
Gets a string for the given key from this resource bundle or one of its
parents.
|
String |
getString(String key,
Object arg1)
Retrieve a string from resource bundle and format it with specified args.
|
String |
getString(String key,
Object arg1,
Object arg2)
Retrieve a string from resource bundle and format it with specified args.
|
String |
getString(String key,
Object arg1,
Object arg2,
Object arg3)
Retrieve a string from resource bundle and format it with specified args.
|
String |
getString(String key,
Object arg1,
Object arg2,
Object arg3,
Object arg4)
Retrieve a string from resource bundle and format it with specified args.
|
String |
getString(String key,
Object arg1,
Object arg2,
Object arg3,
Object arg4,
Object arg5)
Retrieve a string from resource bundle and format it with specified args.
|
String[] |
getStringArray(String key)
Gets a string array for the given key from this resource bundle or one of
its parents.
|
Date |
getTime(String key)
Retrieve a time from bundle.
|
Date |
getTime(String key,
Date defaultValue)
Retrieve a time from bundle.
|
protected abstract Object |
handleGetObject(String key)
Gets an object for the given key from this resource bundle.
|
protected void |
setParent(ResourceBundle parent)
Sets the parent bundle of this bundle.
|
protected ResourceBundle parent
getObject when this bundle does not contain a particular
resource.public ResourceBundle()
public final String getString(String key)
(String) getObject(key)
. key - the key for the desired stringpublic boolean getBoolean(String key, boolean defaultValue) throws MissingResourceException
key - the key of resourcedefaultValue - the default value if key is missingMissingResourceExceptionpublic boolean getBoolean(String key) throws MissingResourceException
key - the key of resourceMissingResourceExceptionpublic byte getByte(String key, byte defaultValue) throws MissingResourceException
key - the key of resourcedefaultValue - the default value if key is missingMissingResourceException - Description of the Exceptionpublic byte getByte(String key) throws MissingResourceException
key - the key of resourceMissingResourceException - Description of the Exceptionpublic char getChar(String key, char defaultValue) throws MissingResourceException
key - the key of resourcedefaultValue - the default value if key is missingMissingResourceException - Description of the Exceptionpublic char getChar(String key) throws MissingResourceException
key - the key of resourceMissingResourceException - Description of the Exceptionpublic short getShort(String key, short defaultValue) throws MissingResourceException
key - the key of resourcedefaultValue - the default value if key is missingMissingResourceException - Description of the Exceptionpublic short getShort(String key) throws MissingResourceException
key - the key of resourceMissingResourceException - Description of the Exceptionpublic int getInteger(String key, int defaultValue) throws MissingResourceException
key - the key of resourcedefaultValue - the default value if key is missingMissingResourceException - Description of the Exceptionpublic int getInteger(String key) throws MissingResourceException
key - the key of resourceMissingResourceException - Description of the Exceptionpublic long getLong(String key, long defaultValue) throws MissingResourceException
key - the key of resourcedefaultValue - the default value if key is missingMissingResourceException - Description of the Exceptionpublic long getLong(String key) throws MissingResourceException
key - the key of resourceMissingResourceException - Description of the Exceptionpublic float getFloat(String key, float defaultValue) throws MissingResourceException
key - the key of resourcedefaultValue - the default value if key is missingMissingResourceException - Description of the Exceptionpublic float getFloat(String key) throws MissingResourceException
key - the key of resourceMissingResourceException - Description of the Exceptionpublic double getDouble(String key, double defaultValue) throws MissingResourceException
key - the key of resourcedefaultValue - the default value if key is missingMissingResourceException - Description of the Exceptionpublic double getDouble(String key) throws MissingResourceException
key - the key of resourceMissingResourceException - Description of the Exceptionpublic Date getDate(String key, Date defaultValue) throws MissingResourceException
key - the key of resourcedefaultValue - the default value if key is missingMissingResourceException - Description of the Exceptionpublic Date getDate(String key) throws MissingResourceException
key - the key of resourceMissingResourceException - Description of the Exceptionpublic Date getTime(String key, Date defaultValue) throws MissingResourceException
key - the key of resourcedefaultValue - the default value if key is missingMissingResourceException - Description of the Exceptionpublic Date getTime(String key) throws MissingResourceException
key - the key of resourceMissingResourceException - Description of the Exceptionpublic Date getDateTime(String key, Date defaultValue) throws MissingResourceException
key - the key of resourcedefaultValue - the default value if key is missingMissingResourceException - Description of the Exceptionpublic Date getDateTime(String key) throws MissingResourceException
key - the key of resourceMissingResourceException - Description of the Exceptionpublic String getString(String key, Object arg1)
key - the key for resourcearg1 - an argpublic String getString(String key, Object arg1, Object arg2)
key - the key for resourcearg1 - an argarg2 - an argpublic String getString(String key, Object arg1, Object arg2, Object arg3)
key - the key for resourcearg1 - an argarg2 - an argarg3 - an argpublic String getString(String key, Object arg1, Object arg2, Object arg3, Object arg4)
key - the key for resourcearg1 - an argarg2 - an argarg3 - an argarg4 - an argpublic String getString(String key, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5)
key - the key for resourcearg1 - an argarg2 - an argarg3 - an argarg4 - an argarg5 - an argpublic String format(String key, Object[] args)
key - the key for resourceargs - an array of argspublic final String[] getStringArray(String key)
(String[]) getObject(key)
. key - the key for the desired string arraypublic final Object getObject(String key)
handleGetObject.
If not successful, and the parent resource bundle is not null, it calls
the parent's getObject method. If still not successful, it
throws a MissingResourceException.key - the key for the desired objectpublic Locale getLocale()
protected void setParent(ResourceBundle parent)
getObject when this bundle does not contain a
particular resource.parent - this bundle's parent bundle.public static final ResourceBundle getBundle(String baseName)
getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader())
, except that getClassLoader() is run with the
security privileges of ResourceBundle. See getBundle for a complete description of the search and instantiation
strategy.baseName - the base name of the resource bundle, a fully qualified
class namepublic static final ResourceBundle getBundle(String baseName, Locale locale)
getBundle(baseName, locale, this.getClass().getClassLoader())
, except that getClassLoader() is run with the
security privileges of ResourceBundle. See getBundle for a complete description of the search and instantiation
strategy.baseName - the base name of the resource bundle, a fully qualified
class namelocale - the locale for which a resource bundle is desiredpublic static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader)
protected abstract Object handleGetObject(String key)
key - the key for the desired objectpublic abstract Enumeration getKeys()
Copyright © 2023 onecode. All rights reserved.