SpringBoot国际化(jsp)
2019-12-30 本文已影响0人
你笑时很美丶
原来的项目用的是jsp,改成html太麻烦,这里介绍一下jsp国际化的(如果是html使用thymeleaf,它自带了的):
#国际化标签
<spring:message code='xxx'/>
1.项目路径如图:
image.png
2.相关依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- tomcat的支持-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
3.新增I18nConfig(设置默认语言)
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Locale;
/**
* 设置默认语言
*
* @author xxx
* @date 2019/12/30
* @since 1.0.0
*/
@Configuration
public class I18nConfig {
private static Logger logger = LoggerFactory.getLogger(I18nConfig.class);
@Bean(name = "localeResolver")
public LocaleResolver localeResolver() {
logger.info("创建cookieLocaleResolver");
LocaleResolver localeResolver = new LocaleResolver();
localeResolver.setDefaultLocale(Locale.CHINA);
logger.info("cookieLocaleResolver:");
return localeResolver;
}
}
4.新增LocaleResolver(修改local的值)文件
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/**
* 每次加载页面时都会被调用,加载页面时都会设置并修改语言
*
* @author xxx
* @date 2019/12/30
* @since 1.0.0
*/
public class LocaleResolver extends AcceptHeaderLocaleResolver {
private Locale myLocal;
@Override
public Locale resolveLocale(HttpServletRequest request) {
//获取cookie数组
Cookie[] cookies = request.getCookies();
String lang = "";
if (cookies != null) {
//遍历cookie数组
for (Cookie cookie : cookies) {
if ("ClientLanguage".equals(cookie.getName())) {
lang = cookie.getValue();
break;
}
}
}
LocaleResolver localeResolver = (LocaleResolver) RequestContextUtils.getLocaleResolver(request);
if (lang == null || "".equals(lang)) {
myLocal = Locale.CHINA;
} else {
if (lang.equals("zh")) {
myLocal = Locale.CHINA;
} else if (lang.equals("en")) {
myLocal = Locale.ENGLISH;
}
}
return myLocal;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
myLocal = locale;
}
}
5.application.yml文件
spring:
http:
encoding:
charset: UTF-8
mvc:
view:
prefix: /WEB-INF/view/
suffix: .jsp
messages:
basename: i18n.messages
cache-duration: 3600
encoding: UTF-8
server:
port: 80
tomcat:
uri-encoding: UTF-8
max-threads: 100
min-spare-threads: 30
servlet:
context-path: /ChineseDemo
6.新建messages.properties,messages_zh.properties,messages_en.properties个文件:
- messages为默认语言,zh中文,en英文
- messages和zh文件内容:
commom.xxx.logout=登出
commom.xxx.login=登录
- en文件内容:
commom.xxx.logout=logout
commom.xxx.login=login
7.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<spring:message code='commom.xxx.logout'/>
<spring:message code='commom.xxx.login'/>
<a href="javascript:;" onclick="changeLang('zh')"> 中文 </a>
<a href="javascript:;" onclick="changeLang('en')"> english </a>
</body>
<script>
function changeLang(lang) {
//cookie持久化
$.cookie = "ClientLanguage=" + lang;
location.reload();
}
</script>
</html>
效果:
中文
英文