Java International Message Resources

Source: Internet
Author: User

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/>

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.