编写工具化配置文件,抽取页面需要显示的国际化消息
SpringBoot自动配置好了管理国际化资源文件的组件;
MessageSourceAutoConfiguration(国际化资源文件)部分源码:
@Configuration
@ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties
public class MessageSourceAutoConfiguration {
private static final Resource[] NO_RESOURCES = {};
@Bean
@ConfigurationProperties(prefix = "spring.messages")//我们的配置文件可以直接放在类路径messages.properties;
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
根据浏览器语言设置的信息切换了国际化
用法如下(Thymeleaf):
<div class="row">
<div class="col-sm-6 col-sm-offset-3 form-box">
<div class="form-top">
<div class="form-top-left">
<!-- 使用国际化 -->
<h3>[[#{longin.welcome}]]</h3>
<p>[[#{longin.hint}]]</p>
</div>
<div class="form-top-right">
<i class="fa fa-key"></i>
</div>
</div>
<div class="form-bottom">
<form role="form" action="" method="post" class="login-form">
<div class="form-group">
<!-- 使用国际化 -->
<label class="sr-only" for="form-username">[[#{longin.form-username}]]</label>
<input type="text" name="form-username" th:placeholder="#{longin.form-username}" placeholder="Username..." class="form-username form-control" id="form-username">
</div>
<div class="form-group">
<!-- 使用国际化 -->
<label class="sr-only" for="form-password">[[#{longin.form-username}]]</label>
<input type="password" name="form-password" th:placeholder="#{longin.form-username}" class="form-password form-control" id="form-password">
</div>
<button type="submit" class="btn" th:value="#{longin.submit}"></button>
</form>
</div>
</div>
</div>
乱码问题:
解决方法:
中英文切换:
原理:国际化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());
}
//从请求头获取(Content-Language: zh-CN)
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
return localeResolver;
}
前端代码:
<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>
创建MyLocaleResolver类:
package com.java.project.component;
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 = request.getLocale();
if(l!=null&&!"".equals(l)){
String[] split = l.split("_");
//split[0]是语言代码,split[1]是国家代码
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
创建MyMvcConfig类:
package com.java.project.config;
import com.java.project.component.MyLocaleResolver;
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.WebMvcConfigurer;
//使用WebMvcConfigurer可以来扩展SpringMVC的功能
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//交给spring管理
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}