Background string internationalization in spring

Source: Internet
Author: User
Tags locale

1. Create three properties files in the project's Resource folder (source folders): (default), (Chinese), Messages_en_ (English).

The string resource in the properties file fills in the information in the form of a key-value pair, as follows:

Helloworld= Greeting: @0 greeting time: @1;

2. Tool class universalmsgto get internationalized strings:


 Packagecom.luxl.action;ImportJava.util.Locale;ImportJava.util.ResourceBundle;/*** @description used to get internationalized strings *@authorLUXL **/ Public classuniversalmsg {Private StaticResourceBundle rb_ch = Resourcebundle.getbundle ("Messages", Locale.china); Private StaticResourceBundle rb_en = Resourcebundle.getbundle ("Messages",; /*** @description Get the internationalized string for key *@paramLocale Language: Chinese (Locale.china), English ( *@paramkey to get the string of key *@return     * @throwsException*/     Public StaticString getString (locale locale, string key)throwsexception{if(key!=NULL&& (!Key.trim (). IsEmpty ())) {            if(Locale.equals (Locale.china)) {returnrb_ch.getstring (key); }Else{                returnrb_en.getstring (key); }        }Else{            return""; }    }        /*** @description Get the Chinese string of key *@paramKey *@return     * @throwsException*/     Public StaticString getString (String key)throwsexception{if(key!=NULL&& (!Key.trim (). IsEmpty ())) {            returnrb_ch.getstring (key); }Else{            return""; }    }}

3. Application Examples:

Try {    String us_msg;     = Universalmsg.getstring (, "HelloWorld");     = Us_msg.replaceall ("@0", Helloworld.getmsg ());     = Us_msg.replaceall ("@1", Calendar.getinstance (). GetTime (). toString ());     Catch (Exception e) {    //  TODO auto-generated catch block    System.out.println (" Error ");    E.printstacktrace ();}  

Note: If you do not find a string that corresponds to key, an exception is thrown.

Background string internationalization in spring

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: 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.