Spring国际化

参考地址: http://hi.baidu.com/risejohn/blog/item/e4d25eecfe5169d32e2e21f1.html


这只是在jsp上有用, 在java不会根据浏览器的语言变化而变化
一个解决办法是: http://blog.csdn.net/geloin/article/details/7553004

首先建立几个文件:
src/com/message/messages.properties
src/com/message/messages_zh_CN.properties
src/com/message/messages_en_US.properties


在applicationContext.xml里面加入:注意要从web-inf开始:/WEB-INF/classes/com/message/messages
<bean id="messageSource"
		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<value>/WEB-INF/classes/com/message/messages</value>
			</list>
		</property>
</bean>


在jsp国际化是:
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<spring:message code="msg_code"/>


在java国际化:
            ApplicationContext ctx = new ClassPathXmlApplicationContext("messages.xml");
            Object[] arg = new Object[] { "Erica", Calendar.getInstance().getTime() };
            String msg = ctx.getMessage("userinfo", arg,Locale.CHINA);



一个国际化类:

package com.message;


import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class MessageHelper implements ApplicationContextAware {
	
	private static ApplicationContext ctx = null;
	
	public static String getMessage(String key){
		return ctx.getMessage(key,null, null);
	}
	public static String getMessage(String key,String[] array){
		return ctx.getMessage(key,array, null);
	}
	public static String getMessage(String key,String[] array,Locale locale){
		return ctx.getMessage(key,array, locale);
	}
	public static String getMessage(String key,List<String> list ){
		return ctx.getMessage(key,list.toArray(), null);
	}
	public static String getMessage(String key,List<String> list,Locale locale){
		return ctx.getMessage(key,list.toArray(), locale);
	}
	public static String getMessage(String key,Map<String,String> map){
		return ctx.getMessage(key,null, null);
	}

	@Override
	public void setApplicationContext(ApplicationContext ctx) throws BeansException {
		// TODO Auto-generated method stub
		MessageHelper.ctx = ctx;
	}

}

你可能感兴趣的:(Spring国际化)