SpringBoot2.0—国际化
国际化源码解析
1.查看MessageSourceAutoConfiguration类,springBoot已经默认自动配置好了管理国际化资源文件的组件;
@Bean
@ConfigurationProperties(
prefix = "spring.messages"
)
默认配置文件为message,所以,我们的配置文件可以直接放在类路径下叫messages.properties;
2.查看该类下面的方法messageSource
@Bean
public MessageSource messageSource(MessageSourceProperties properties) {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
if (StringUtils.hasText(properties.getBasename())) {
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
}
if (properties.getEncoding() != null) {
messageSource.setDefaultEncoding(properties.getEncoding().name());
}
messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
Duration cacheDuration = properties.getCacheDuration();
if (cacheDuration != null) {
messageSource.setCacheMillis(cacheDuration.toMillis());
}
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
return messageSource;
}
该方法使用了springMVC的ResourceBundleMessageSource,通过该方法设置国际化资源文件的基础名(去掉语言国家代码),如果基础名不为根路径下的messages.properties就需要手动添加自定义的基础名,如果有多个就用逗号分隔
如何编写我们自己的国际化
1).我们的配置文件名为i18n文件下的login,则在配置文件中编写:
spring.messages.basename=i18n.login
2).编写国际化配置文件
![Alt]
3).去页面获取国际化的值
<!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:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="dashboard.html" th:action="@{/user/login}" method="post">
<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>
<p style="color:red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
<label class="sr-only" th:text="#{login.username}" >Username</label>
<input name="username" type="text" class="form-control" th:placeholder="#{login.username}" placeholder="Username" required="" autofocus="">
<label class="sr-only" th:text="#{login.password}">Password</label>
<input name="password" type="password" class="form-control" th:placeholder="#{login.password}" placeholder="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>
原理:国际化Locale(区域信息对象);LocaleResolver(获取区域信息对象),
默认的就是根据请求头带来的区域信息获取Locale进行国际化
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
public LocaleResolver localeResolver() {
if (this.mvcProperties
.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.mvcProperties.getLocale());
}
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
return localeResolver;
}
如果在配置文件中配置了spring.mvc.locale-resolver=fixed,且指定了spring.mvc.locale的值,则页面使用的是指定且固定的国际语言(FixedLocaleResolver);
spring.mvc.locale-resolver默认为accept-header,即通过浏览器请求头中的Accept-Language 作为判断依据(AcceptHeaderLocaleResolver):
public class AcceptHeaderLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
Locale defaultLocale = getDefaultLocale();
if (defaultLocale != null && request.getHeader("Accept-Language") == null) {
return defaultLocale;
}
Locale requestLocale = request.getLocale();
List<Locale> supportedLocales = getSupportedLocales();
if (supportedLocales.isEmpty() || supportedLocales.contains(requestLocale)) {
return requestLocale;
}
Locale supportedLocale = findSupportedLocale(request, supportedLocales);
if (supportedLocale != null) {
return supportedLocale;
}
return (defaultLocale != null ? defaultLocale : requestLocale);
}
//other code..
}
4).使用链接切换国际化语言只需覆盖默认的LocaleResolver对象,
编写一个自定义的LocalResolver
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/**
* 国际化
* 从请求中获取参数,设置Content-Language
*/
public class MyLocalResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
String l=httpServletRequest.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) {
}
}
5).修改页面切换语言的链接
<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>
6).注册我们自己的国际化语言组件
/**
* WevMvcConfigurerAdapter已经过时,2.0使用WebMvcConfigurer
*/
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
/**
* 讲组件注册到容器
* 所有的webMvcConfigurer组件都会一起起作用
* @return
*/
@Bean
public WebMvcConfigurer webMvcConfigurer(){
WebMvcConfigurer config=new WebMvcConfigurer() {
@Bean
public LocaleResolver localeResolver(){
return new MyLocalResolver();
}
}
这样配置了自定义的LocaleResolver对象就会把WebMvcAutoConfiguration自动配置类中预定义的LocaleResolver的Bean覆盖,Spring Boot就会使用自定义的LocaleResolver对象。
这样就可以通过点击中文和Englisth来进行切换了.