springMVC源码分析--国际化实现Session和Cookie(二)

时间:2021-12-06 14:41:31

上一篇博客springMVC源码分析--国际化LocaleResolver(一)中我们介绍了springMVC提供的国际化的解决方案,接下来我们根据springMVC提供的解决方案来简单的实现一个多语言的支持。

一、基于Session的国际化实现

基于Session的国际化实现其实说来也简单,就是在Session中保存语言信息Locale,这样springMVC会从我们提供的多语言配置文件中读取相关语言的信息。

(1)首先我们需要配置springMVC的多语言配置,因为我们打算使用Session来实现,所有对于localeResolver我们要配置其实现类SessionLocaleResolver,在springMVC的配置文件中添加如下配置:

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>

(2)这里我们提供了一个多语言拦截器LanguageInterceptor,通过这个拦截器设置多语言信息,其实主要的代码就是request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale),这样springMVC会根据Locale的信息来获得多语言。

 public class LanguageInterceptor implements HandlerInterceptor{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String language = request.getParameter("language");
		if (language != null&&language.equals("zh")) {
			Locale locale = new Locale("zh", "CN");
			request.getSession()
					.setAttribute(
							SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
							locale);
			request.setAttribute("language", language);
		} else if (language != null&&language.equals("en")) {
			Locale locale = new Locale("en", "US");
			request.getSession()
					.setAttribute(
							SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
							locale);
			request.setAttribute("language", language);
		} else {
			request.getSession().setAttribute(
					SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
					LocaleContextHolder.getLocale());
			language = LocaleContextHolder.getLocale().getLanguage();
			request.setAttribute("language", language);
		}
		return true;
	}
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}

在springMVC的配置文件中配置拦截器

<mvc:interceptors>
	<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
	<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
	<bean class="com.tianjunwei.interceptor.LanguageInterceptor"></bean>
</mvc:interceptors>

在工程中添加多语言配置文件,在src/main/resources目录下新建messages目录,分别添加中英文的多语言配置文件message_en_US.properties和message_zh_CN.properties,文件中以key = value的形式保存多语言信息

springMVC源码分析--国际化实现Session和Cookie(二)

运行结果图:

springMVC源码分析--国际化实现Session和Cookie(二)springMVC源码分析--国际化实现Session和Cookie(二)

二、基于Cookie的国际化实现

与上面类似,我们首先创建一个拦截器用来将多语言信息写到Cookie中,这样就可以通过cookie来切换多语言了。

 public class LanguageCookieInterceptor implements HandlerInterceptor{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String language = request.getParameter("language");
		if (language != null&&language.equals("zh")) {
			Locale locale = new Locale("zh", "CN");
			(new CookieLocaleResolver()).setLocale (request, response, locale);
			request.setAttribute("language", language);
		} else if (language != null&&language.equals("en")) {
			Locale locale = new Locale("en", "US");
			(new CookieLocaleResolver()).setLocale (request, response, locale);
			request.setAttribute("language", language);
		} else {
			(new CookieLocaleResolver()).setLocale (request, response,
					LocaleContextHolder.getLocale());
			language = LocaleContextHolder.getLocale().getLanguage();
			request.setAttribute("language", language);
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

}

springMVC的配置文件如下:

<mvc:interceptors>
	<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
	<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
	<!-- <bean class="com.tianjunwei.interceptor.LanguageInterceptor"></bean> -->
	<bean class="com.tianjunwei.interceptor.LanguageCookieInterceptor"></bean>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>

这样我们也可以实现通过Session实现的效果。