首先看一下我们的登录页面
需求:
当我们点击下方的中文或者英文,切换中文和英文页面
首先创建一个默认的login.properties
然后创建
英文:login_en_US.properties
中文:login_zh_CN.properties
然后点击下面的:Resource Bundle
进行创建
这时我们可以同时配置三个properties文件
页面全部代码:
主要的国际化代码有:
国际化使用#{} 这个结构进行引入
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<input type="text" class="form-control"
th:placeholder="#{login.username}"
required="" autofocus="">
<input type="password" class="form-control" 0
th:placeholder="#{login.password}" required="">
<input type="checkbox" value="remember-me" >[[#{login.remember}]]
<!DOCTYPE html>
<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">
<meta name="description" content="">
<meta name="author" content="">
<title>Signin Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link href="asserts/css/bootstrap.min.css"
th:ref="@{/webjars/bootstrap/4.1.2/css/bootstrap.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="asserts/css/signin.css"
th:ref="@{asserts/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="dashboard.html">
<img class="mb-4" th:src="@{asserts/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<label class="sr-only" ></label>
<input type="text" class="form-control"
th:placeholder="#{login.username}"
required="" autofocus="">
<label class="sr-only" >Password</label>
<input type="password" class="form-control" 0
th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me" >[[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
</form>
</body>
</html>
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
英文:
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
国际化Locale(区域信息对象);LocaleResolver(获取区域信息对象);
这个方法返回了区域信息对象
package cn.kitey.springboot.component;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/**
* 可以连接上携带区域信息
*/
public class MyLocalResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
String l = httpServletRequest.getParameter("l");
//l为空的时候返回默认值
Locale locale = Locale.getDefault();
if(!StringUtils.isEmpty(l)){
//获取一个数组,通过 _ 分隔符分隔
String[] split = l.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
//将我们获取的国际化信息传入容器
@Bean
public LocaleResolver localeResolver(){
return new MyLocalResolver();
}
这里可以注意一下地址栏中请求信息!
加油偶!