国际化配置详解
国际化配置的步骤:
1、编写国际化配置文件;
2、使用ResourceBundleMessageSource管理国际化资源文件(springboot自带,不用配置)
3、在页面取出国际化内容(即通过点击切换中文/英文)
1、编写国际化配置文件
编写国际化配置文件,抽取页面需要显示的国际化消息
配置文件一共有三个,一个默认配置文件,一个英文,一个中文(页面html中会通过模板引擎读取对应的值)
如何设置?
1)设置之前记得把编码改为utf-8,不然页面可能乱码
2)新建对应的属性配置文件
3)添加属性值
2、使用ResourceBundleMessageSource管理国际化资源文件
对应源码:
3、在页面取出国际化内容(即通过点击切换中文/英文)
1)html页面使用模板引擎读取属性值
<!DOCTYPE html>
<!--<!–<html >–> 这个是默认的,后面加上对应的模板引擎-->
<html 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" target="_blank" rel="external nofollow" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="asserts/css/signin.css" target="_blank" rel="external nofollow" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="dashboard.html">
<img class="mb-4" 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">Username</label>
<input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" autofocus="">
<label class="sr-only" th:text="#{login.password}">Password</label>
<input type="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}">
<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>
<!-- **加粗样式**下面是根据点击请求后,链接带上对应的zh_CN/en_US字符放到连接中,后面就去链接中截取这个关键字,在LocaleResolver中定义,然后切换视图-->
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}" target="_blank" rel="external nofollow" >中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}" target="_blank" rel="external nofollow" >English</a>
</form>
</body>
</html>
这个是登录页面的html文件,其中th:text="#{login.password}" 表示使用模板引擎从配置文件中取值。
2)根据点击链接切换国际化
package Main.config;
import com.sun.corba.se.spi.orbutil.closure.Closure;
import com.sun.corba.se.spi.resolver.LocalResolver;
import org.apache.tomcat.jni.Local;
import org.codehaus.groovy.util.StringUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import java.util.Set;
//国际化配置文件
@Configuration
public class myLocalResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request){
String l = request.getParameter("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) {
}
// @Configuration 和@Bean 把自定义的设置国际化文件注入bean中,替代默认根据请求来设置国际化
@Bean
public LocaleResolver localeResolver(){
return new myLocalResolver();
}
}
相关注释: