Java internationalization, using ResourceBundle to read configuration files __java

Source: Internet
Author: User
Tags locale stringbuffer

Frameworks such as struts,spring and Tomcat containers are automatically read and resource-bound from resource files (such as Messages.properties) in a resourcebundle manner. The Java.util.ResourceBundle class is very flexible and can be internationalized by selecting the Read files by setting the locale (locale).

Looking at Tomcat's source code recently and seeing how Tomcat is internationalized, here's an example of how to use:


Resource configuration file:

Localstrings_fr.java:

Package terry.codex;

Import java.util.Collections;
Import java.util.Enumeration;
Import Java.util.ResourceBundle;

/**
 * @ Writer: Yh.zeng
 * @ Write Time: 2017-12-3 pm 4:58:26
 * @ file Description: localstrings French configuration File/
 public
class LOCALSTRINGS_FR extends resourcebundle{

	@Override
	protected Object handlegetobject (String key) {
		if ( Key.equals ("Tmpdir")) {return
			"{0} itinéraires désignés avec moins";
		}
		return null;
	}

	@Override public
	enumeration<string> Getkeys () {return
		 collections.enumeration (keyset ());
	}
} 
Localstrings_en.properties:

Tmpdir=cannot find specified temporary folder at {0}
Localstrings_zh.properties:

tmpdir=\u5728{0}\u8def\u5f84\u4e0b\u627e\u4e0d\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u5939
Localstrings.properties:

tmpdir=\u5728{0}\u8def\u5f84\u4e0b\u627e\u4e0d\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u5939

Test Example Demo


Resourcebundletest:

Package terry.codex;
Import Java.io.File;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Java.net.URLClassLoader;
Import Java.text.MessageFormat;
Import Java.util.Locale;
Import java.util.MissingResourceException;


Import Java.util.ResourceBundle; /** * @ Writer: Yh.zeng * @ Write Time: 2017-12-3 12:35:42 * File Description: Automatically read and resource-bound de from resource files (such as messages.properties) in ResourceBundle Way MO * can be used for internationalization */public class Resourcebundletest {public static void main (String args[]) {string
        
        Bundlename = "Terry.codex.LocalStrings";
         /** * 1, under the current class loader, look for the Java Virtual Machine instance of the current default locale value of the configuration file, will find the following file content * Terry/codex/localstrings_zh.class * Terry/codex/localstrings_zh.properties * Terry/codex/localstrings.class * Terr Y/codex/localstrings.properties * file content from top to bottom cover * localstrings_zh.class file will overwrite Localstrings_zh.properti ES file content * localstrings_zh.properties file will overwrite LocalstrIngs.class file content * Localstrings.class file will overwrite the contents of localstrings.properties file * Note: Localstrings_zh.class
         The piece must be a subclass of the ResourceBundle to implement the Handlegetobject and Getkeys methods.
        	* * try {System.out.println ("Example1:");
            ResourceBundle bundle = Resourcebundle.getbundle (bundlename);
            	if (bundle!= null) {String value = bundle.getstring ("Tmpdir");
                String params[] = new string[]{"D:\\tmp"};
                Messageformat MF = new Messageformat (value);
            System.out.println (Mf.format (params, new StringBuffer (), null). ToString ());
		} catch (MissingResourceException ex) {ex.printstacktrace (); /** * 2, Locale.english find the contents of the following file under the current class loader: * terry/codex/localstrings_en       . class * Terry/codex/localstrings_en.properties * Terry/codex/localstrings.class * Terry/codex/localstrings.properties * file content from above andOverwrite * localstrings_en.class file will overwrite the contents of localstrings_en.properties file * localstrings_en.properties file will overwrite The contents of the Localstrings.class file * localstrings.class file will overwrite the contents of the Localstrings.properties file * Note: localstrings_
         The En.class file must be a ResourceBundle subclass to implement the Handlegetobject and Getkeys methods.
        	* * try {System.out.println ("Example2:");
            ResourceBundle bundle = Resourcebundle.getbundle (Bundlename, locale.english);
            	if (bundle!= null) {String value = bundle.getstring ("Tmpdir");
                String params[] = new string[]{"D:\\tmp"};
                Messageformat MF = new Messageformat (value);
                Mf.setlocale (Locale.english);
            System.out.println (Mf.format (params, new StringBuffer (), null). ToString ());
		} catch (MissingResourceException ex) {ex.printstacktrace ();
   /** * 3, find the following file under the current class loader: * Terry/codex/localstrings_zh.class      * Terry/codex/localstrings_zh.properties * Terry/codex/localstrings.class * Terr Y/codex/localstrings.properties * file content from top to bottom cover * localstrings_zh.class file will overwrite Localstrings_zh.properti The contents of the ES file * localstrings_zh.properties file will overwrite the contents of the Localstrings.class file * Localstrings.class file will be overwritten locals
         Trings.properties File Content * Note: The Localstrings_zh.class file must be a subclass of ResourceBundle to implement the Handlegetobject and Getkeys methods.
        	* * try {System.out.println ("Example3:");
            ResourceBundle bundle = Resourcebundle.getbundle (Bundlename, Locale.china);
            	if (bundle!= null) {String value = bundle.getstring ("Tmpdir");
                String params[] = new string[]{"D:\\tmp"};
                Messageformat MF = new Messageformat (value);
                Mf.setlocale (Locale.china);
            System.out.println (Mf.format (params, new StringBuffer (), null). ToString ()); catch (MIssingresourceexception ex) {ex.printstacktrace (); /** * 4, find the following file under the current class loader: * Terry/codex/localstrings_fr.class * terry/c Odex/localstrings_fr.properties * Terry/codex/localstrings.class * terry/codex/localstrings.    Properties * file content from top to bottom cover * localstrings_fr.class file will overwrite the contents of localstrings_fr.properties file * The localstrings_fr.properties file will overwrite the contents of the Localstrings.class file * localstrings.class file will overwrite the localstrings.properties file
         * Note: The Localstrings_fr.class file must be a ResourceBundle subclass to implement the Handlegetobject and Getkeys methods.
        	* * try {System.out.println ("Example4:");
            ResourceBundle bundle = Resourcebundle.getbundle (Bundlename, locale.france);
            	if (bundle!= null) {String value = bundle.getstring ("Tmpdir");
                String params[] = new string[]{"D:\\tmp"};
 Messageformat MF = new Messageformat (value);               Mf.setlocale (locale.france);
            System.out.println (Mf.format (params, new StringBuffer (), null). ToString ());
		} catch (MissingResourceException ex) {ex.printstacktrace ();
         /** * 5, find the following file under the D:\\loader loading path: * Terry/codex/localstrings_zh.class * Terry/codex/localstrings_zh.properties * Terry/codex/localstrings.class * terry/cod Ex/localstrings.properties * file content from top to bottom cover * localstrings_zh.class file will overwrite localstrings_zh.properties file The content * localstrings_zh.properties file will overwrite the contents of the Localstrings.class file * Localstrings.class file will be overwritten localstring
         S.properties File Content * Note: The Localstrings_zh.class file must be a subclass of ResourceBundle to implement the Handlegetobject and Getkeys methods.
			* * try {bundlename = "terry.codex2.LocalStrings";
	        System.out.println ("Example5:");
	        String fileurlstring = new File ("D:\\loader"). Touri (). toString ();fileurlstring = Fileurlstring.replaceall ("!/", "%21/");
	        ClassLoader ClassLoader = new URLClassLoader (new Url[]{new URL (fileurlstring)}); if (ClassLoader!= null) {try {resourcebundle bundle = Resourcebundle.getbundle (Bundlename, L Ocale.
	                , ClassLoader);
	                	if (bundle!= null) {String value = bundle.getstring ("Tmpdir");
	                    String params[] = new string[]{"D:\\tmp"};
	                    Messageformat MF = new Messageformat (value);
	                    Mf.setlocale (Locale.china);
	                System.out.println (Mf.format (params, new StringBuffer (), null). ToString ());
	            } catch (MissingResourceException ex2) {ex2.printstacktrace ();
		(Malformedurlexception e) {//TODO auto-generated catch block E.printstacktrace ();
 }
		
	}
}

Program Run Result:

Example1:
The specified folder could not be found under the D:\TMP path
Example2:
cannot find specified temporary folder at D:\TMP
Example3: The
specified folder
could not be found under the D:\TMP path Example4:
D:\TMP itinéraires désignés avec moins Example5
: The
specified folder cannot be found under the D:\TMP path

Demo: See Https://github.com/zengyh/CodeLibary/tree/master/src/resource/bundle/ResourceBundleTest.java


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.