SpringMvc多语言配置

applicationContext.xml文件中加入如下代码:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basenames">  
	    	<list>
	        	<value>messages</value>
	        </list>  
	 	</property>
	 	
	 	<!-- 这个是配置单一路径的做法,也好用
	 	<property name="basename">
            <value>messages</value>
        </property>
	 	 -->
	 	
	 	<property name="defaultEncoding" value="UTF-8" />
  	</bean>
  	
	
  	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
  		<property name="cookieName" value="clientlanguage"/>
     	<property name="cookieMaxAge" value="94608000"/>
	    <property name="defaultLocale" value="en" />
    </bean>


spring-servlet.xml文件加入如下代码:
<context:component-scan base-package="com.easeye.ymail.web.eventmail.appbusiness.multilanguage.control"/>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
		<property name="paramName" value="language" />
	</bean>


messages_en.properties文件加入如下代码:
contact.mytestkey=hello\!


messages_zh.properties文件加入如下代码:
contact.mytestkey=\u60A8\u597D\!


web.xml文件加入如下代码:
<filter>
		<filter-name>systemLanguageFilter</filter-name>
		<filter-class>com.easeye.ymail.web.webcore.lang.SystemLanguageFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>systemLanguageFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>systemLanguageFilter</filter-name>
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>


SpringUtil.java代码如下:
package com.shihuan.mypro.web.webcore.utils;

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

public class SpringUtil implements ApplicationContextAware {

	private static ApplicationContext applicationContext;
	
	public static Object getBean(String beanName){
		return applicationContext.getBean(beanName);		
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getBean(Class cla){
		return (T)applicationContext.getBean(cla);		
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringUtil.applicationContext=applicationContext;
	}
	
}


SystemLanguageFilter.java代码如下:
import java.io.IOException;
import java.util.Locale;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.easeye.ymail.web.webcore.utils.SpringUtil;


public class SystemLanguageFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, SpringUtil.getBean(org.springframework.web.servlet.i18n.CookieLocaleResolver.class));
		Locale locale = RequestContextUtils.getLocale((HttpServletRequest) request);
		request.setAttribute("easeyeI18nLang", locale.getLanguage());
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
	}

}


ChangeLanguageControl.java代码如下:
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.LocaleEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.easeye.ymail.web.webcore.messaging.OpMessage;

/**功能: 动态改变系统的语言*/
@Controller
@RequestMapping("/")
public class ChangeLanguageControl {

protected Logger logger = LoggerFactory.getLogger(getClass().getName());

@RequestMapping(value = "changelanguage.do")
public String changeLanguage(HttpServletRequest request, HttpServletResponse response) {
return "redirect:" + request.getParameter("originalUrl");
}

}


easeye-en.js文件加入如下代码(注意引用顺序一定是先引用easeye-en.js后引用easeye-ch.js):
var easeyeI18n={};
easeyeI18n["en"] = {
"contactName" : "contact name",
"userName" : "user name"
}


easeye-ch.js文件加入如下代码:
easeyeI18n["ch"] = {
"contactName" : "联系人姓名",
"userName" : "用户帐号"
}


mybusiness.js文件加入如下代码:
function getEaseyeI18nText(langKey){
return easeyeI18n[easeyeI18nLang][langKey];
}
function switchLanguage(language){
if(window.location.href.indexOf(".do")==-1){
open("/eventmail/changelanguage.do?language="+language+"&originalUrl="+encodeURI(window.location.href),"_self");
}else{
if(window.location.href.indexOf(".do?")==-1){
open("?language="+language,"_self");
}else{
open("&language="+language,"_self");
}
}
}


在login.jsp文件中代码如下:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<font color="red">
																	mytestkey: <spring:message code="contact.mytestkey"  />
																	Current Locale : ${pageContext.response.locale}
																	</font>

<a href="${ctx}/changelanguage.do?language=en">EN</a>
																	<a href="${ctx}/changelanguage.do?language=zh_CN">CN</a>




上面是笔者自己写的代码, 下面的这个链接是网上提供的文章, 可供参考:
http://www.open-open.com/doc/view/6df7b8aa00584a19ba74bc6f130dadcf

你可能感兴趣的:(java,spring,springMVC)