Spring实现国际化

覆写ResourceBundleMessage.
Spring留接口,其设计给力啊,很容易扩展。
package org.frame.base.message;

import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.context.support.ResourceBundleMessageSource;

/**
 * 
 * 扩展Spring的resourceBundleMessageSource
 * 使其支持中文,因为properties文件天生不支持中文,如果要其支持,需要转码,麻烦!
 * 
 * 于是扩展一下,实现自动转码
 */
public class ResourceBundleMessageSourceExtend extends
		ResourceBundleMessageSource {

	private static final String ENCODING = "GBK";// 注意属性文件使用GBK编码
	private static final String NULL = "null";

	/** cache the encoding key value * */
	Map<String, String> encodingCache = new ConcurrentHashMap<String, String>(
			20);

	/**
	 * resolve no argus
	 */
	protected String resolveCodeWithoutArguments(String code, Locale locale) {
		String message = super.resolveCodeWithoutArguments(code, locale);
		return decodeString(message, ENCODING);

	}

	/**
	 * resolve args
	 * @see resolveCode(String code, Locale locale)
	 */
	protected MessageFormat createMessageFormat(String msg, Locale locale) {
		if (logger.isDebugEnabled()) {
			logger.debug("Creating MessageFormat for pattern [" + msg
					+ "] and locale '" + locale + "'");
		}
		msg = decodeString(msg, ENCODING);
		return new MessageFormat((msg != null ? msg : ""), locale);
	}

	/**
	 * 转码 
	 * @param msg
	 * @param encode
	 * @return
	 */
	private String decodeString(String message, String encode) {
		String encodMessage = encodingCache.get(message);
		if (encodMessage == null) {
			try {
				encodMessage = new String(message.getBytes("ISO8859-1"), encode);
				if (message != null) {
					encodingCache.put(message, encodMessage);
				} else {
					encodingCache.put(message, NULL);
					// log the code is not exist in properties
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return encodMessage;
	}

}



配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd ">
<beans default-autowire="byName">
 
	
	<!-- 国际化资源文件 -->
	<bean id="messageSource" class="org.frame.base.message.ResourceBundleMessageSourceExtend">  
           <property name="basenames">  
                <list>  
                   <value>message/message</value>  
                   <value>message/error</value>   
               </list>  
           </property>  
    </bean>  
	 
</beans>


配置文件内容
message.user.username = 用户名 !
message.user.password = 密码 !

message.user.context = 内容{0}真好 !


message.user.username = user name !
message.user.password = password !


package org.frame.base.message;

import java.util.Locale;

import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MessageSourceTest {
 
	public static void main(String[] args) { 
		   MessageSource messageSource = new ClassPathXmlApplicationContext("message/testMessage.xml");  
		   String message1 = messageSource.getMessage("message.user.username", null, Locale.CHINESE);
		   //String message3 = messageSource.getMessage("message.user.username", null, Locale.CHINESE);
		   String message4 = messageSource.getMessage("message.user.context", new Object[]{"ycl"}, Locale.CHINESE);
		   String message2 = messageSource.getMessage("error.user.validName", null, Locale.CHINESE);
		   System.out.println(message1);
		   System.out.println(message2);
		   System.out.println(message4);
	}

}



配置文件使用GBK编码,不需要转码,就能够自动转码了

你可能感兴趣的:(spring)