步骤:
1.编写国际化配置文件,抽取页面需要显示的国际化消息
2.spring-boot自动配置好了管理国际化资源文件的组件,
springBoot中有个类叫MessageSourceAutoConfiguration,这个类是专门配置国际化的,里面有一个方法,messageSource,里面返回了自动化配置信息:
@Bean
@ConfigurationProperties(prefix = "spring.messages")
public MessageSourceProperties messageSourceProperties() {
return new MessageSourceProperties();
}
@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;
}
编写配置:spring.messages.basename=i18n.login
3.去页面获取国际化配置的值;
在thymeleaf模板引擎中用#{}去获取国际化值
效果:根据浏览器的语言设置切换国际化
原理:
国际化Locale(区域信息对象),LocaleResolver(获取区域信息对象)
在WebMvcAutoConfiguration类中有一个方法叫localeResolver:
@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;
}
完整代码:
1.编写国际化配置文件:
在默认配置:login.properties中
login.btn=登录~
login.password=密码~
login.remember=记住我~
login.tip=请登录~
login.username=用户名~
在英文配置:login_en_US.properties中
login.btn=Sign In
login.password=PassWord
login.remember=Remember Me
login.tip=Please sign in
login.username=UserName
在中文配置:login_ch_CN.properties中
login.btn=登录
login.password=密码
login.remember=记住我
login.tip=请登录
login.username=用户名
2.在页面中引用:
<form class="form-signin" action="dashboard.html">
<img class="mb-4" th:src="@{/static/img/bootstrap-solid.svg}" src="/static/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" th:text="#{login.username}">Username</label>
<input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
<label class="sr-only" th:text="#{login.password}">Password</label>
<input type="password" class="form-control" placeholder="Password" 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="@{/login.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/login.html(l='en_US')}">English</a>
</form>
3,编写国际化配置类
package com.guigu.springboot.component;
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;
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String l= request.getParameter("l");
Locale locale = null ;//locale.getDefault();
if(!StringUtils.isEmpty(l)){
String [] splits = l.split("_");
locale = new Locale(splits[0],splits[1]);
}
if(StringUtils.isEmpty(locale)){
locale = locale.getDefault();
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
4,在配置类中引用:
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}