上篇说道:需要国际化的变量配置文件就是properties那三个文件,国际化解析器,国际化拦截器
自定义版就是讲解析器和国际化拦截器的功能结合,写一个国际化解析器。
这个解析器实现LocaleReslover接口,实现resolveLocale方法中,逻辑是:判定是否有一个参数(自己定义的一个名字用于传递区域和语言信息),有的话就解析这个参数创建Locale对象,将这个对象返回,并存入session。如果没有这个参数的话,从session去取,如果还是没有就返回默认的Locale对象。
package com.example.demo.intercepter;
import com.alibaba.druid.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale;
public class MyLocaleResolver implements LocaleResolver {
private static final String I18N_LANGUAGE = "i18n_language";
private static final String I18N_LANGUAGE_SESSION = "i18n_language_session";
@Override
public Locale resolveLocale(HttpServletRequest req) {
String i18n_language = req.getParameter(I18N_LANGUAGE);
Locale locale = Locale.getDefault();
if(!StringUtils.isEmpty(i18n_language)) {
String[] language = i18n_language.split("_");
locale = new Locale(language[0], language[1]);
//将国际化语言保存到session
HttpSession session = req.getSession();
session.setAttribute(I18N_LANGUAGE_SESSION, locale);
}else {
//如果没有带国际化参数,则判断session有没有保存,有保存,则使用保存的,也就是之前设置的,避免之后的请求不带国际化参数造成语言显示不对
HttpSession session = req.getSession();
Locale localeInSession = (Locale) session.getAttribute(I18N_LANGUAGE_SESSION);
if(localeInSession != null) {
locale = localeInSession;
}
}
return locale;
}
@Override
public void setLocale(HttpServletRequest req, HttpServletResponse res, Locale locale) {
}
}
在写一个配置类,让容器初始化LocaleResolver对象时new我们自定义的。
package com.example.demo.config;
import com.example.demo.intercepter.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class CustomMvcConfig extends WebMvcConfigurerAdapter {
/**
* 配置自己的国际化语言解析器
* @return
*/
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
}
/**
* 配置自己的拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//super.addInterceptors(registry);
}
}
jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring MVC国际化</title>
</head>
<body>
<!-- 通过HTTP请求参数变化国际化 -->
<a href="./login?i18n_language=zh_CN">简体中文</a>
<a href="./login?i18n_language=en_US">美国英文</a>
<h2>
<!-- 找到属性文件变量名为welcome的配置 -->
<div>
<div>
<spring:message code="mess.user.name" />:<input />
</div>
</div>
<div>
<div>
<spring:message code="mess.user.password" />:<input />
</div>
<div>
<spring:message code="mess.user.title" />:<input />
</div>
</div>
<div>
<div>
<button><spring:message code="mess.user.btn" /></button>
</div>
</div>
</h2>
<!-- 当前国际化区域 -->
Locale: ${pageContext.response.locale }
</body>
</html>