1. Message Resource class. The internal resourcebundle corresponds to a message resource file.
Import Java. text. *; <br/> Import Java. util. *; </P> <p> public class messageresource {<br/> private resourcebundle RB; // resource binding <br/> private clientapplogger logger = clientapplogger. getinstance (); </P> <p>/** <br/> * constructor <br/> * @ Param props properties resource binding <br/> */<br/> Public messageresource (resourcebundle RB) {<br/> This. RB = RB; <br/>}</P> <p>/** <br/> * get the message character <br/> * @ Param key string <br/> * @ return string <br/> */<br/> Public String getstring (string key) {<br/> try {<br/> return RB. getstring (key); <br/>} catch (missingresourceexception ex) {<br/> logger. error ("missingmessageresource: Key =" + key); <br/> return "undefined "; <br/>}</P> <p>/** <br/> * obtain the message resource. <br/> * @ Param key string <br /> * @ Param ARGs object [] Variable Message <br/> * @ return string <br/> */<br/> Public String getstring (string key, object [] ARGs) {<br/> try {<br/> string temp = RB. getstring (key); <br/> messageformat mformat = new messageformat (temp); <br/> return mformat. format (temp, argS); <br/>} catch (missingresourceexception ex) {<br/> logger. error ("missingmessageresource: Key =" + key); <br/> return "undefined"; <br/>}< br/>
2. Message resource Factory
Import Java. util. *; </P> <p> public class messageresourcefactory {<br/> Private Static messageresourcefactory factory = new messageresourcefactory (); <br/> private map msgresourcemap = NULL; // store Message Resources </P> <p> private messageresourcefactory () {<br/> msgresourcemap = new hashmap (); <br/>}</P> <p> Public static messageresourcefactory getinstance () {<br/> return factory; <br/>}</P> <p>/** <br/> * create a message resource <br/> * @ Param name string message resource file name <br/> * @ Param locale <br/> * @ return messageresource <br/> */<br/> Public messageresource createmessageresource (string fileresname, locale locale) {<br/> Object msgresobj = msgresourcemap. get (name); <br/> If (msgresobj = NULL) {<br/> resourcebundle RB = resourcebundle. getbundle (fileresname, Loc); <br/> messageresource msgres = new messageresource (RB); <br/> msgresourcemap. put (name, msgres); <br/> return msgres; <br/>}else {<br/> return (messageresource) msgresobj; <br/>}< br/>}
3. Use
For example, the message resource file is
Message_en_us.properties file content:
Button. Add = add
Button. Update = Modify
Button. Delete = Delete
Button. Refresh = Refresh
State. Success = operation success
State. Failure = Operation Failure
Message = the disk {0} contains {1} files.
Message_zh_cn.properties file content:
Button. Add =/u6dfb/u52a0
Button. Update =/u439/u6539
Button. Delete =/u5220/u9664
Button. Refresh =/u5237/u65b0
State. Success =/u64cd/u4f5c/u6210/u529f
State. Failure =/u64cd/u4f5c/u5931/u8d25
Message = the disk {0} contains {1} files.
(The Chinese file is converted using the native2ascii command)
The Code is as follows:
Messageresourcefactory factory = messageresourcefactory. getinstance (); <br/> messageresource res = factory. createmessageresource ("message", new locale ("ZH", "cn"); <br/> string value = res. getstring ("button. add "); <br/> string value_args = res. getstring ("message", new object [] {"C", "12"}); <br/>