Java ResourceBundle Introduction

Source: Internet
Author: User

Public abstract class ResourceBundle
Extends Object
The parent class of the following classes:
ListResourceBundle, propertyResourceBundle
Resource bundles contain locale-specific objects. When a program requires a locale-specific resource, such as a String, the program can load it from a resource bundle that is appropriate for the current user locale. In this way, most of the program code that is independent of the user's locale can be written, and it is isolated from the language-specific information in most resource bundles.
This allows programming to:
Easy to localize or translate into different languages
Handle multiple locales at once
Easy to change in the future to support more locales
A resource bundle is conceptually a collection of related classes that are inherited from ResourceBundle. Each related subclass of ResourceBundle has the same base name plus additional components that identify its locale. For example, suppose your resource bundle is named MyResources. The first class you write may be the default resource bundle, which has the same name as its family--myresources. You can also provide many locale-specific classes as needed: For example, you might give it a German name Myresources_de.
Each related subclass of ResourceBundle contains the same project, but the project has been translated for the locale described by the ResourceBundle subclass. For example, MyResources and Myresources_de may have a String that is used on the Confirm action button. In MyResources, String may contain OK, and in Myresources_de it may contain Gut.
If you have different resources for different countries, you can make a provision: for example, Myresources_de_ch is a resource for Switzerland. If you want to change only some of the resources in the rules, you can do so.
When your program requires a locale-specific object, it uses the Getbundle method to load the ResourceBundle class:
ResourceBundle myresources = Resourcebundle.getbundle ("MyResources", Currentlocale);
The first parameter specifies the family name of the resource bundle that contains the question object. The second parameter specifies the desired locale. Getbundle uses these two parameters to construct the name of the ResourceBundle subclass, which should be loaded in the following way.
Resource bundles look for classes with different suffixes, based on (1) the desired locale and (2) The default locale (base class), to find the following order from lower (more specified) to parent class level (specified less):
BaseClass + "_" + Language1 + "_" + Country1 + "_" + variant1
BaseClass + "_" + Language1 + "_" + Country1
BaseClass + "_" + language1
BaseClass
BaseClass + "_" + Language2 + "_" + Country2 + "_" + Variant2
BaseClass + "_" + Language2 + "_" + Country2
BaseClass + "_" + language2
The result of the lookup is a class, but that class may be supported by the properties file on disk. If the lookup fails, Getbundle () throws a MissingResourceException exception.
The base class must be fully qualified (for example, mypackage.myresources, not just myresources). It must be executable by your code; it cannot be a class that is private to a package that calls Resourcebundle.getbundle.
Note: ResourceBundle is used internally to access Numberformats, Collation, and so on. The lookup policy is the same.
The resource bundle contains key/value pairs. The key is used to uniquely identify the locale-specific object in the resource bundle. The following is an example of a listresourcebundle that contains key/value pairs:
Class MyResource extends ListResourceBundle {public object[][] getcontents () {return contents;} Static final object[][] Contents = {///LOCALIZE This {"Okkey", "OK"}, {"Cancelkey", "Cancel"},//END of MATERIAL to LOCALIZE}; }
The key is always String. In this example, the keys are Okkey and Cancelkey. In the example above, the values are String--ok and cancel--but they are not necessarily so. The value can be any type of object.
Gets an object from the resource bundle using the appropriate fetch method. Because both Okkey and Cancelkey are strings, you can retrieve them using the GetString method:
button1 = new Button (myresourcebundle.getstring ("Okkey")); Button2 = new Button (myresourcebundle.getstring ("Cancelkey"));
The Fetch method requires the key as a parameter, and returns the object if it is found. The Get method throws a MissingResourceException exception if the object is not found.
In addition to getString, resource bundles support the acquisition of other methods for different types of objects, such as Getstringarray. If no objects match these methods, you can use GetObject and map the results to the appropriate type. For example:
Int[] Myintegers = (int[]) myresources.getobject ("Intlist");
Note: You should always provide a base class without a suffix. If the required locale does not exist, this will be the "last Choice" for the class. For example, the following class is a myresources. It happens to contain the US string, so we don't have to use an explicit myresource_en or Myresource_en_us.
The JDK provides two subclasses of ResourceBundle: ListResourceBundle and propertyResourceBundle, which provide a very simple way to create resources. (once serialization is fully integrated, another method is provided.) As seen in the previous example, ListResourceBundle manages its resources as key/value pairs. propertyResourceBundle uses an attribute file to manage its resources.
If ListResourceBundle or propertyresourcebundle does not meet your needs, you can write your own resourcebundle subclass. The subclass you write must cover two methods: Handlegetobject and Getkeys ().
The following is an example of a resourcebundle subclass that manages a few resources (Hashtable should be used for larger resources). Note that if the key is not found, handlegetobject must return NULL. Note that if a "parent class" ResourceBundle handles the same key with the same value (see UK below), you do not have to provide any value.
Example:
Abstract class MyResources extends ResourceBundle {public Object handlegetobject (String key) {if (Key.equals ("Okkey")) R Eturn "OK"; if (Key.equals ("Cancelkey")) return "Cancel"; return null; }}} abstract class Myresources_de extends MyResources {public Object handlegetobject (String key) {if (Key.equals ("Okkey" )) return "Gut"; if (Key.equals ("Cancelkey")) return "Vernichten"; return null; }}} abstract class Myresources_uk extends MyResources {public Object handlegetobject (String key) {//don ' t need Okkey, S Ince parent level handles it. if (Key.equals ("Cancelkey")) return "Dispose"; return null; } }
You don't have to limit yourself to using a single series of ResourceBundle. For example, you can have a bundle collection for error messages, Exceptionresources (EXCEPTIONRESOURCES_FR, Exceptionresources_de, ...), and a bundle set for gadgets, Widgetresource (Widgetresources_fr, Widgetresources_de, ...); Disconnect the resources as you want.

See:
ListResourceBundle, propertyResourceBundle, missingresourceexception
Variable index
Parent
When a parent class bundle does not contain a specific resource, it can be consulted through GetObject.
Construct Sub-index
ResourceBundle ()
Method Index
Getbundle (String)
Get the appropriate resource bundle subclasses.
Getbundle (String, Locale)
Get the appropriate resource bundle subclasses.
Getkeys ()
Returns an enumeration of the keys.
GetObject (String)
Gets an object from the resource bundle.
GetString (String)
Gets an object from the resource bundle.
Getstringarray (String)
Gets an object from the resource bundle.
Handlegetobject (String)
Gets an object from the resource bundle.
SetParent (ResourceBundle)
Sets the parent bundle of the bundle.
Variable
Parent
Protected ResourceBundle Parent
When a parent class bundle does not contain a specific resource, it can be consulted through GetObject.

Construction Sub
ResourceBundle
Public ResourceBundle ()
Method
GetString
Public final String getString (string key) throws MissingResourceException
Gets an object from the resource bundle.
A convenient way to save a map.

Parameters:
Key-See class description.
Getstringarray
Public final string[] Getstringarray (String key) throws MissingResourceException
Gets an object from the resource bundle.
A convenient way to save a map.

Parameters:
Key-See class description.
GetObject
Public final Object GetObject (String key) throws MissingResourceException
Gets an object from the resource bundle.

Parameters:
Key-See class description.
Getbundle
public static final ResourceBundle Getbundle (String baseName) throws MissingResourceException
Gets the appropriate resource bundle subclasses.

Parameters:
BaseName-See class description.
Getbundle
public static final ResourceBundle Getbundle (String baseName, locale locale)
Gets the appropriate resource bundle subclasses.

Parameters:
BaseName-See class description.
Locale-See class description.
SetParent
protected void SetParent (resourcebundle parent)
Sets the parent bundle of the bundle. When a parent class bundle does not contain a specific resource, it can be searched through GetObject.

Parameters:
Parent-a bundle of parents of the bundle.
Handlegetobject
Protected abstract Object Handlegetobject (String key) throws MissingResourceException
Gets an object from the resource bundle. Note: Subclasses must be overwritten.

Parameters:
Key-See class description.
Getkeys
Public abstract enumeration Getkeys ()
Returns an enumeration of the keys. Note: Subclasses must be overwritten.

Java ResourceBundle Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.