Java Resource Kit (ResourceBundle) detailed "2"

Source: Internet
Author: User
Tags locale static class string format custom name
Java.util


Class Resourcebundle.control


Java.lang.Object
Java.util.ResourceBundle.Control
Enclosing class:
ResourceBundle



The public static class Resourcebundle.control extends Objectresourcebundle.control defines a set of callback methods that are in the package load process by the Resourcebundle.getbundle Factory method call.
In other words, Resourcebundle.control with factory methods can be used to load resource bundles. The default implementation of the callback method provides the factory method with the necessary information to perform the default behavior.


In addition to the callback method, the Tobundlename and Toresourcename methods are defined, with the primary purpose of facilitating the implementation of callback methods.
However, to provide different conventions in the organization and packaging of localized resources, you can also override the Tobundlename method. The Toresourcename method is the final method to avoid using the wrong resource and class name separator.


The two factory methods GetControl (list) and Getnofallbackcontrol (list) provide Resourcebundle.control instances that implement common variables for the default package loading process.


In all Resourcebundle.getbundle calls to the same base package, the format returned by the GetFormats method and the candidate language environment returned by the Getcandidatelocales method must be consistent.


Otherwise, the Resourcebundle.getbundle method returns an unexpected package.
For example, if the GetFormats method is
The first call to Resourcebundle.getbundle only returns "Java.class",
The second call returns only "Java.properties",
Then the second call of the class-based package is the one that was cached during the first call.


If the Resourcebundle.control instance is used by multiple threads at the same time, it must be thread-safe.
Resourcebundle.getbundle does not call the Resourcebundle.control method at the same time. The default implementation of the method is thread-safe.


The application can specify the Resourcebundle.control instance returned by the GetControl factory method.
You can also specify a Resourcebundle.control instance created from the Resourcebundle.control subclass.
The process is loaded with a custom package. The following is an example of a change to the default package load process.


Example 1


The following code lets Resourcebundle.getbundle find only resources that are based on attributes.


Import java.util.*;
Import static java.util.resourcebundle.control.*;
...
ResourceBundle bundle =
Resourcebundle.getbundle ("MyResources", New Locale ("fr", "CH"),
ResourceBundle.Control.getControl (format_properties));


Given the resource bundle in the example described by Resourcebundle.getbundle,
This resourcebundle.getbundle call loads the myresources_fr_ch.properties,
Its Parent property is Myresources_fr.properties,
The Parent property of the latter is myresources.properties. (No hidden myresources_fr_ch.properties, but hidden myresources_fr_ch.class.) )


Example 2:

Package com.itm.ResourceBundle;
Import Java.io.BufferedInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.io.Serializable;
Import java.net.URLConnection;
Import Java.util.Arrays;
Import java.util.Enumeration;
Import java.util.List;
Import Java.util.Locale;
Import java.util.Properties;

Import Java.util.ResourceBundle; 
 
 /****** JDK: An example of loading an xml-based package using Properties.loadfromxml. */public class Twoxmlresourcebundle {ResourceBundle RB = Resourcebundle.getbundle ("Messages", New resourcebundle.co Ntrol () {public list<string> getformats (String baseName) {if (BaseName = null) throw new Nullpoint
					Erexception ();
				Return arrays.aslist ("xml"); Public ResourceBundle Newbundle (string baseName, Locale Locale, String format, ClassLoader loader, Boolean re  Load) throws Illegalaccessexception, Instantiationexception, IOException {if (BaseName = null | | locale = = NULL | | format = = NULL | | Loader = = null) throw new NullPointerException ();
					ResourceBundle bundle = NULL;
						if (format.equals ("xml")) {String bundlename = tobundlename (baseName, locale);
						String resourcename = toresourcename (bundlename, format);
						InputStream stream = null;
							if (reload) {Serializable URL = loader.getresource (resourcename);
								if (URL!= null) {URLConnection connection = ((Java.net.URL) URL). OpenConnection ();
									if (connection!= null) {//Disable caches to get fresh data for//reloading.
									Connection.setusecaches (FALSE);
								stream = Connection.getinputstream ();
						}} else {stream = Loader.getresourceasstream (resourcename);
							} if (stream!= null) {Bufferedinputstream bis = new Bufferedinputstream (stream);
							bundle = new Xmlresourcebundle (bis);
						Bis.close ();
				} return bundle;

	}
			}); ... private staticClass Xmlresourcebundle extends ResourceBundle {private Properties props;
			Xmlresourcebundle (InputStream stream) throws IOException {props = new Properties ();
		Props.loadfromxml (stream);
		} protected Object Handlegetobject (String key) {return Props.getproperty (key);
			Public enumeration<string> Getkeys () {return null;
	// ...
		} }
}


The following reprint: http://blog.csdn.net/shanliangliuxing/article/details/6827972

I. Understanding of International resource documents

This class provides a shortcut to the internationalization of software. This class allows you to write programs that:
Easily localized or translated into different languages
Handle multiple locales at once
can be easily modified in the future to support more language environment

To put it simply, the function of this class is to read the resource properties file (properties) and then match the country language information (or program designation) of the current system according to the name information (localized information) of the. properties file, and then get the contents of the corresponding properties file.

To use this class, it is important to note that the name of the properties file is canonical:

The

Generic naming conventions are: Custom Name _ Language code _ Country code. Properties,
If the default, write directly as: Custom name. Properties
For example:
Myres_en_us.properties
Myres_zh_cn.properties
Myres.properties
 
When under Chinese operating system, if myres_zh_cn.properties, Myres.properties two files exist, the myres_zh_cn.properties is preferred, and the default myres.properties is used when myres_zh_cn.properties does not exist.
 
A resource file that does not provide language and locale is the default resource file for the system.
Resource files must be iso-8859-1 encoded, so for all non-western language processing, it must first be converted to the Java Unicode escape format. The conversion method is native2ascii with a tool that is self-contained by the JDK.
&NBSP
Two, instance
 
defines three resource files, put them under the root of SRC (this is necessary, or you put them under your own configured Calsspath.)
 
myres.properties
aaa=good 
bbb=thanks


myres_en_us.properties
Aaa=good &NBSP
Bbb=thanks


myres_zh_cn.properties
aaa=\u597d 
Bbb=\u591a\u8c22

Package com.itm.ResourceBundle;

Import Java.util.Locale;
Import Java.util.ResourceBundle;
/**
 * International Resource Binding Test
 * Reprint--->>   http://lavasoft.blog.51cto.com/62575/184605
 *x '
* * public class Testresourcebundle {public 
        static void Main (string[] args) { 
        		//chinese
                Locale locale1 = new Locale ("en", "CN"); 
                ResourceBundle resb1 = Resourcebundle.getbundle ("Myres", locale1); 
                System.out.println (resb1.getstring ("AAA")); Chinese

                resourcebundle resb2 = Resourcebundle.getbundle ("Myres", Locale.getdefault ()); 
                System.out.println (resb1.getstring ("AAA")); 
                
                English:
                Locale locale3 = new Locale ("en", "US"); 
                ResourceBundle RESB3 = Resourcebundle.getbundle ("Myres", locale3); 
                System.out.println (resb3.getstring ("AAA")); 
        } 

Print:

Good  
good  
good 

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.