1. 确认环境都为UTF-8
2. 建立i18n文件以及配置文件
2.1 在resources建立i18n文件
2.2 在i18n中建立配置文件
- 首先建立一个叫login.properties配置文件
- 接着继续建立一个叫login_en_US.properties配置文件,然后会看到系统会自动识别将这两个放入一个文件夹中
- 接着建立第三个文件,我们可以点击合并的文件夹右击选中Add Property Files to Resource Bundle
- 点击加号
- 输入en_us,点击ok,再ok
2.3 编辑配置文件
- 我们在IDEA中选择Resources Bundle进行可视化编辑
- 点击+号,建立key在右边输入对应的值(三个配置文件同时进行)
3. 识别国际化
MessageSourceAutoConfiguration类是专门处理国家化信息的类
查看可以配置的属性
进行使用:
在application.properties中配置
# 绑定我们国际化配置文件
spring.messages.basename=i18n.login
4. thymeleaf使用
注意:
- 记得导入thymeleaf约束
xmlns:th="http://www.thymeleaf.org"
- 普通文本用th:text="#{…}"
- 表单提示用th:placeholder="#{…}
- 显示外面用[[#{…}]]
<!DOCTYPE html>
<!-- saved from url=(0039)https://v3.bootcss.com/examples/signin/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="icon" th:href="@{https://v3.bootcss.com/favicon.ico}">
<title>Signin Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="@{/css/signin.css}" rel="stylesheet">
</head>
<body>
<div class="container">
<form class="form-signin" th:action="@{/user/login}">
<h2 class="form-signin-heading" th:text="#{login.tip}">
</h2>
<!--如果error值为空则不显示消息-->
<p style="color: #ff0000" th:text="${error}" th:if="${not #strings.isEmpty(error)}"></p>
<label for="inputUserName" class="sr-only" th:text="#{login.username}"></label>
<input type="text" id="inputUserName" name="username" class="form-control" th:placeholder="#{login.username}"
required="" autofocus="">
<label for="inputPassword" class="sr-only" th:text="#{login.password}"></label>
<input type="password" id="inputPassword" name="password" class="form-control"
th:placeholder="#{login.password}" required="">
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me">[[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.btn}]]</button>
<p class="text-muted">2017-2021</p>
<a class="btn btn-sm" th:href="@{/index.html(l='zn_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">英文</a>
</form>
</div> <!-- /container -->
</body>
</html>
5. 切换语言实现
5.1 thymeleaf中传入参数
<a class="btn btn-sm" th:href="@{/index.html(l='zn_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">英文</a>
5.2 自定义组件LocaleResolver
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
// 获取请求中的语言参数
String lanaguage = request.getParameter("l");
Locale locale = Locale.getDefault();
if (!StringUtils.isEmpty(lanaguage)) {
//zh_CN
String[] split = lanaguage.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
5.3 在核心配置类中注入bean
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//视图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//返回/abc会跳转到test.html
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
//注入到Bean
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
}
}