Implementation of internationalization in Java Foundation---->java

Source: Internet
Author: User

The functionality and code design of the application takes into account the need to run in different regions, and its code simplifies the production of different local versions. The process of developing such a program is called internationalization. Today, we begin to learn about the internationalization of code implementations in Java.

Java internationalization is mainly done by the following 3 classes

    • Java.util.ResourceBundle: Used to load a resource bundle
    • Java.util.Locale: Corresponds to a specific country/region, locale.
    • Java.text.MessageFormat: Used to format messages

To achieve the internationalization of the program, you must provide the resource files required by the program. The contents of the resource file are made up of key-value pairs. The name of a resource file can be in 3 formats:

    • Basename_language_country.properties
    • Basename_language.properties
    • Basename_properties

Implementation of internationalization in Java SE

The internationalization of Java SE Implementation, the project structure is as follows:

The attribute file is defined first, and its naming convention is shown above. The specific code Resourcebundletest.java is as follows

First, get all the available locales, the results are too long to list, altogether 156 columns

 Public Static void Getlocales () {    = locale.getavailablelocales ();    System.out.println (availablelocales.length);      for (Locale locale:availablelocales) {        System.out.println (locale.tostring ());}    }

Second, use the system default locale

 Public Static void Usedefaultlocale () {    = Resourcebundle.getbundle ("Locale.myres");     = resourcebundle.getstring ("name");    System.out.println (string);}

Resourcebundle.getbundle ("Locale.myres") is the same as Resourcebundle.getbundle ("Locale.myres", Locale.getdefault ()).

Iii. using the locale of your own designation

 Public Static void Useownlocale () {    new Locale ("zh", "CN");     = Resourcebundle.getbundle ("Locale.myres", locale);     = resourcebundle.getstring ("name");    System.out.println (string);}

Iv. using messages with placeholders

 Public Static void GetMessage () {    = Resourcebundle.getbundle ("Locale/myres");     = resourcebundle.getstring ("message");    " Huhx "," Liu Li "));}

The Myres_zh_cn.properties file has:

Message=my name is {0}? --{1}

Print Result: My name is HUHX?, and I love you. --Liu Li

V. Using class files instead of resource files

Java allows a class file to be used instead of a resource file, which is to write code manually for internationalization.

    • The class requires inheriting from ListResourceBundle and overriding the Getcontents method that returns an object array, each of which is a key-value pair.
    • The name of the class must be basename_language_contry, which is similar to the naming of the property file

Under the locale package, add the Myres_zh_cn.java class with the following content:

 Package locale; Import Java.util.ListResourceBundle;  Public class extends listresourcebundle {    privatefinal Object mydata[][] = {{"message", "Hello, {0} and {1} "}, {" Test "," Test " }};    @Override    protected  object[][] getcontents () {        return  myData ;    }}

The tests in main use the code:

 Public Static void GetMessage () {    = Resourcebundle.getbundle ("Locale.myres");     = resourcebundle.getstring ("message");    " Huhx "," Liu Li "));}

Print Result: Hello, Huhx and Liu Li

Note: If the system has both a resource file and a class file, the system will host the class file instead of calling the resource file. For example, for this series of Chinese resource Files basename is myres, the system search order is as follows

    1. Myres_zh_cn.java
    2. Myres_zh_cn.properties
    3. Myres_zh.java
    4. Myres_zh.properties
    5. Myres.java
    6. Myres.properties

If the Getbundle parameter is "Locale/myres", then the corresponding class in the locale will not be searched, only the property file will be found. Only "Locale.myres" will find the class first, and then find the property file

Six, we look at ResourceBundle's GetString (key) method of the source code, is actually called GetObject (key) method

 Public FinalObject GetObject (String key) {Object obj=Handlegetobject (key); if(obj = =NULL) {        if(Parent! =NULL) {obj=Parent.getobject (key); }        if(obj = =NULL)            Throw NewMissingResourceException ("Can ' t find resource for bundle" + This. GetClass (). GetName ()+ ", key" +Key, This. GetClass (). GetName (), key); }    returnobj;}

The Handlegetobject method is called first, with the following code:

 Public Final Object handlegetobject (String key) {    //  lazily load the lookup hashtable.    if NULL ) {        loadlookup ();    }     if NULL {        thrownew  nullpointerexception ();    }     return // This class ignores locales}

Lookup is map<string,object>, the declaration is empty. Execute the Loadlookup () method

Private synchronized voidLoadlookup () {if(Lookup! =NULL)        return; Object[][] Contents=getcontents (); HashMap<String,Object> temp =NewHashmap<>(contents.length);  for(inti = 0; i < contents.length; ++i) {//key must is Non-null String, value must be Non-nullString key = (string) contents[i][0]; Object value= Contents[i][1]; if(Key = =NULL|| Value = =NULL) {            Throw NewNullPointerException ();    } temp.put (key, value); } Lookup=temp;}

Executes the Getcontens () method of the ListResourceBundle and stores the returned result with a regular corresponding key value pair in the map.

 Do Override

Implementation of internationalization in Java Foundation---->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.