1. Create three properties files in the project's Resource folder (source folders): Messages.properties (default), Messages_zh_cn.properties (Chinese), Messages_en_ Us.properties (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", locale.us); /*** @description Get the internationalized string for key *@paramLocale Language: Chinese (Locale.china), English (locale.us) *@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 (locale.us, "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