JAVA中怎么实现国际化(ResourceBundle 与locale的使用 )

package org.bbs.util;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class MessageResource {
	private ResourceBundle rb; //资源绑定  
	/** 
	 * 构造器 
	 * @param props Properties 资源绑定 
	 */
	public MessageResource(ResourceBundle rb) {
		this.rb = rb;
	}
	/** 
	 * 得到消息字符 
	 * @param key String 键 
	 * @return String 
	 */
	public String getString(String key) {
		try {
			return rb.getString(key);
		} catch (MissingResourceException ex) {
			return "undefined";
		}
	}
	/** 
	 * 得到消息资源 
	 * @param key String 键 
	 * @param args Object[] 可变消息 
	 * @return String 
	 */
	public String getString(String key, Object[] args) {
		try {
			String temp = rb.getString(key);
			MessageFormat mFormat = new MessageFormat(temp);
			return mFormat.format(temp, args);
		} catch (MissingResourceException ex) {
			return "undefined";
		}
	}
}

package org.bbs.util;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

public class MessageResourceFactory {  
    private static MessageResourceFactory factory = new MessageResourceFactory();  
   //存放消息资源 
    private Map msgResourceMap = null; 
     
  
    private MessageResourceFactory() {  
        msgResourceMap = new HashMap();  
    }  
  
    public static MessageResourceFactory getInstance() {  
        return factory;  
    }  
    /** 
     * 创建消息资源 
     * @param name String 消息资源文件名 
     * @param locale Locale  
     * @return MessageResource 
     */  
    public MessageResource createMessageResource(String fileResName, Locale locale) {  
        Object msgResObj = msgResourceMap.get(fileResName);  
        if (msgResObj == null) {               
            ResourceBundle rb = ResourceBundle.getBundle(fileResName, locale);  
            MessageResource msgRes = new MessageResource(rb);  
            msgResourceMap.put(fileResName, msgRes);  
            return msgRes;  
        } else {  
            return (MessageResource)msgResObj;  
        }  
    }  
}  


package org.bbs.util;

import java.util.Locale;

public class MessageFormatIin18 {

	public static void main(String[] args) {
		MessageResourceFactory factory = MessageResourceFactory.getInstance();
		MessageResource res = factory.createMessageResource("message",
				new Locale("zh", "CN"));
		String value = res.getString("button.add");
		System.out.println(value);
		String value_args = res.getString("message", new Object[] { "C", "12" });
		System.out.println(value_args);
	}

}


button.add=Add
button.update=Modify
button.delete=Delete
button.refresh=Refresh
state.success=Operation Success
state.failure=Operation Failure
message=The disk's {0} contains {1} files.



button.add=\u6dfb\u52a0
button.update=\u4fee\u6539
button.delete=\u5220\u9664
button.refresh=\u5237\u65b0
state.success=\u64cd\u4f5c\u6210\u529f
state.failure=\u64cd\u4f5c\u5931\u8d25
message=The disk''s {0} contains {1} files.




你可能感兴趣的:(java,C++,c,C#,bbs)