Java ResourceBundle introduction, javaresourcebundle

Source: Internet
Author: User

Java ResourceBundle introduction, javaresourcebundle

Public abstract class ResourceBundle
Extends Object
Parent class of the following classes:
ListResourceBundle, PropertyResourceBundle
A resource bundle contains language-specific objects. When a program needs a resource specific to the language environment, such as String, the program can be loaded into the resource bundle suitable for the current user's language environment. In this way, you can write a majority of program code independent of the user's language environment, which is isolated from the information specific to the language environment in most resource bundles.
This allows programming:
Easy to localize or translate into different languages
One-time processing of multiple language Environments
It will be easy to change in the future to support more Language Environments
A resource bundle is a collection of related classes. These classes are inherited from ResourceBundle. Each related subclass of ResourceBundle has the same base name and an additional component that identifies its language environment. For example, assume that 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 language-specific classes as needed: For example, you may provide a German name MyResources_de.
Each related subclass of ResourceBundle contains the same project, but the project has been translated for the language environment described by the ResourceBundle subclass. For example, both MyResources and MyResources_de may have a String used on the confirm OPERATION button. In MyResources, String may contain OK. In MyResources_de, it may contain Gut.
If you have different resources for different countries, you can specify that, for example, MyResources_de_CH is the resource of Switzerland. If you only want to change some of the required resources, you can do so.
When your program needs a language-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 containing the problematic object. The second parameter specifies the expected language environment. GetBundle uses these two parameters to construct the name of the ResourceBundle subclass. It should be loaded as follows.
The resource bundle uses different suffixes to search for classes. Based on (1) expected language environment and (2) default language environment (base class ), the following describes the sequence from lower (more specified) to parent (less specified:
Baseclass + "_" + language1 + "_" + country1 + "_" + variant1
Baseclass + "_" + language1 + "_" + country1
Baseclass + "_" + language1
Baseclass
Baseclass + "_" + language2 + "_" + country2 + "_" + variant2
Baseclass + "_" + language2 + "_" + country2
Baseclass + "_" + language2
The search result is a class, but the class may be supported by the feature files on the disk. If the query fails, getBundle () throws a MissingResourceException.
The base class must be fully qualified (for example, myPackage. MyResources, not just MyResources ). It must be executable in your code; it cannot be a private class for the package that calls ResourceBundle. getBundle.
Note: ResourceBundle is used internally to access NumberFormats and Collation. The search policy is the same.
The resource bundle contains key/value pairs. The key uniquely identifies the language-specific objects in the resource bundle. The following is an example of ListResourceBundle containing 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 above example, the values are also String -- OK and Cancel -- but they are not necessarily the same. The value can be any type of object.
Use the appropriate method to obtain an object from the resource bundle. Because OkKey and CancelKey are both strings, you can use the getString method to retrieve them:
Button1 = new Button (myResourceBundle. getString ("OkKey"); button2 = new Button (myResourceBundle. getString ("CancelKey "));
The key must be used as a parameter for obtaining methods, and this object will be returned if it is found. If the object is not found, the system throws a MissingResourceException.
In addition to getString, the resource bundle supports other methods for obtaining different types of objects, such as getStringArray. If no object matches these methods, you can use getObject and map the result to an appropriate type. For example:
Int [] myIntegers = (int []) myResources. getObject ("intList ");
Note: The base class without a suffix should always be provided. If the required language environment does not exist, this will be the "last choice" of the class ". For example, the following class is MyResources. It happens to contain US strings, so we do not need to use explicit MyResource_en or MyResource_en_US.
JDK provides two subclasses of ResourceBundle: ListResourceBundle and PropertyResourceBundle. They provide a very simple method to create resources. (Once serialization is fully integrated, another method is provided .) As shown in the previous example, ListResourceBundle manages its resources as key/value pairs. PropertyResourceBundle uses feature files to manage its resources.
If ListResourceBundle or PropertyResourceBundle does not meet your requirements, you can write your own ResourceBundle subclass. The subclass you write must cover two methods: handleGetObject and getKeys ().
The following example shows how to manage a few resources in the ResourceBundle subclass (Hashtable should be used for larger resources ). NOTE: If no key is found, handleGetObject must return null. Note: If a "parent class" ResourceBundle processes 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") return "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, since parent level handles it. if (key. equals ("cancelKey") return "Dispose"; return null ;}}
You do not have to restrict your use of a single series of ResourceBundle. For example, you can have a bundle set for error information, ExceptionResources (ExceptionResources_fr, ExceptionResources_de ,...), and a bundle set for gadgets, WidgetResource (WidgetResources_fr, WidgetResources_de ,...); disconnect resources as needed.
 
See:
ListResourceBundle, PropertyResourceBundle, MissingResourceException
Variable Index
Parent
When the parent class bundle does not contain a specific resource, you can use getObject to view it.
Create a sub-index
ResourceBundle ()
Method Index
GetBundle (String)
Obtain the appropriate resource bundle subclass.
GetBundle (String, Locale)
Obtain the appropriate resource bundle subclass.
GetKeys ()
Enumeration of the Return key.
GetObject (String)
Obtain an object from the resource bundle.
GetString (String)
Obtain an object from the resource bundle.
GetStringArray (String)
Obtain an object from the resource bundle.
HandleGetObject (String)
Obtain an object from the resource bundle.
SetParent (ResourceBundle)
Set the parent class bundle of the bundle.
Variable
Parent
Protected ResourceBundle parent
When the parent class bundle does not contain specific resources, you can view it through getObject.
 
Constructor
ResourceBundle
Public ResourceBundle ()
Method
GetString
Public final String getString (String key) throws MissingResourceException
Obtain an object from the resource bundle.
A convenient way to save mappings.
 
Parameters:
Key-see the class description.
GetStringArray
Public final String [] getStringArray (String key) throws MissingResourceException
Obtain an object from the resource bundle.
A convenient way to save mappings.
 
Parameters:
Key-see the class description.
GetObject
Public final Object getObject (String key) throws MissingResourceException
Obtain an object from the resource bundle.
 
Parameters:
Key-see the class description.
GetBundle
Public static final ResourceBundle getBundle (String baseName) throws MissingResourceException
Obtain the appropriate resource bundle subclass.
 
Parameters:
BaseName-see the class description.
GetBundle
Public static final ResourceBundle getBundle (String baseName, Locale locale)
Obtain the appropriate resource bundle subclass.
 
Parameters:
BaseName-see the class description.
Locale-see the class description.
SetParent
Protected void setParent (ResourceBundle parent)
Set the parent class bundle of the bundle. If the parent class bundle does not contain a specific resource, you can search for it using getObject.
 
Parameters:
Parent-parent bundle of the bundle.
HandleGetObject
Protected abstract Object handleGetObject (String key) throws MissingResourceException
Obtain an object from the resource bundle. Note: The subclass must be overwritten.
 
Parameters:
Key-see the class description.
GetKeys
Public abstract Enumeration getKeys ()
Enumeration of the Return key. Note: The subclass must be overwritten.

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.