1.在依赖中加入Thymeleaf SpringWeb
2.在resoruces下创建i18n文件夹,配置国际化翻译配置
在i18n包下新建login.properties,等语言文件
.
编写相关配置
3.在application.properties中添加国际化文件的基础名
4.自定义配置类,以用于定制解析国际化功能
@Configuration
public class MyLocalResovel implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String l =request.getParameter("1");
// String l =request.getParameter("language");
String header =request.getHeader("Accept-Language");
Locale locale=null;
if (!StringUtils.isEmpty(l)){
// _分割 由 login_zh_CN 决定
String[] split =l.split("_");
locale =new Locale(split[0],split[1]);
}else {
String[] splits =header.split(",");
String[] split =splits[0].split("-");
locale =new Locale(split[0],split[1]);
}
return locale;
}
//覆盖系统配置
@Bean
public LocaleResolver localeResolver(){
return new MyLocalResovel();
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
5.在html页面实现功能,并启动mian方法所在类
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1 ,shrink-to-fit=no">
<title>用户登录界面</title>
<link th:href="@{/login/css/bootstrap.min.css}" rel="stylesheet">
<link th:href="@{/login/css/signin.css}" rel="stylesheet">
</head>
<!--保持正常的框架,以便于连接-->
<body class="text-center">
<from class="form-signin">
<img class="mb-4" th:src="@{/login/img/login.jpg}" width="72 " height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">请登录</h1>
<input type="text" class="form-control"
th:placeholder="#{login.username}" required="" autofocus="">
<input type="text" class="form-control"
th:placeholder="#{login.password}" required="" >
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me">[[#{login.rememberme}]]
<!-- 同login.properties rememberme-->
</label>
</div>
<botton class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.button}">登录</botton>
<p class="mt-5 mb-3 text-muted " >@<span th:text="${currentYear}">2000</span>-
<span th:text="${currentYear}+1">2004</span> </p>
<a class="btn btn-sm "th:href="@{/toLoginPage(1='zh_CN')}">中文</a>
<a class="btn btn-sm "th:href="@{/toLoginPage(1='en_US')}">English</a>
<!-- <a class="btn btn-sm "th:href="@{/toLoginPage(language='zh_CN')}">中文</a>-->
<!-- <a class="btn btn-sm "th:href="@{/toLoginPage(language='en_US')}">English</a>-->
</from>
</body>
</html>
即可实现Thymeleaf 配置的国际化页面