springboot-项目实战:国际化
1 在resources目录下新建一个文件夹 i18n
2 在 i18n 文件夹下创建国际化的配置文件
login.properties
login_en_US.properties
login_zh_CN.properties
注意名称不能写错,创建完成后三个文件会自动合并
3 指定国际化配置文件的位置
application.properties
#我们定义的国际化配置文件放在的真实位置
spring.messages.basename=i18n.login
4 确定项目的编码格式为UTF-8
5 国际化配置切换到可视化编辑模式
安装Resources Bundle 插件
这个插件安装完成后,在国际化配置文件的右下角会出现切换按钮,点击按钮切换成功后创建
这样就能创建一条配置,点击这个配置可以同时编辑三个文件的内容
6 编写国际化的配置文件
创建以下五个配置并对配置进行编辑
login.btn
login.password
login.remember
login.tip
login.username
这样编写完成后,会自动在三个配置文件中生成对应的配置信息
login.properties
login.btn=登录
login.password=密码
login.remember=记住我
login.tip=请登录
login.username=用户名
login_en_US.properties
login.btn=Sing in
login.password=Password
login.remember=remember me
login.tip=Please sign in
login.username=Username
login_zh_CN.properties
login.btn=登录
login.password=密码
login.remember=记住我
login.tip=请登录
login.username=用户名
7 修改index.html
使用thymeleaf语法替换index.html里面的值
index.html
<!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>
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<link th:href="@{/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="dashboard.html">
<img class="mb-4" th:src="@{/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">Username</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" 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">[[#{login.btn}]]</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a class="btn btn-sm">中文</a>
<a class="btn btn-sm">English</a>
</form>
</body>
</html>
8 重启程序,访问主页
数据成功显示了,接下来实现中英文切换的效果
9 给index.html里面中英文切换的按钮绑定跳转的链接
index.html
<a class="btn btn-sm" th:href="@{/index.html(language='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(language='en_US')}">English</a>
10 在config包下创建一个国际化组件
用自定义的MyLocaleResolver组件去替换springboot默认的LocaleResolver
MyLocaleResolver.java
package com.lv.config;
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 MyLocaleResolver implements LocaleResolver {
//解析请求
@Override
public Locale resolveLocale(HttpServletRequest request) {
//获取请求中的语言参数
String language = request.getParameter("language");
Locale locale = Locale.getDefault();//获取默认的,如果没有就是用默认的
//如果请求的链接携带了国际化的参数
if (!StringUtils.isEmpty(language)){
//zh_CN
String[] split = language.split("_");
//国家,地区
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
11 在mvc配置中注册MyLocaleResolver
这步操作完成后,springboot默认的国际化组件就会失效,我们自定义的国际化组件就会生效
package com.lv.config;
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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
//自定义的国际化组件就生效了
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
12 重启程序,访问首页
默认显示,点English按钮,切换英文
切换成功,注意地址栏的变化,接下来再点击中文切换到中文
成功实现了切换中英文操作
13 总结
- 我们需要配置i18n文件
- 我们如果需要在项目中进行按钮切换语言,需要自定义一个LocaleResolver
- 记得将自己写的组件配置到spring容器中 Bean
- 使用 #{}取值