【1】SpringBoot的自动配置

SpringBoot自动配置好了管理国际化资源文件的组件:

@Configuration
@ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "spring.messages")
public class MessageSourceAutoConfiguration {

private static final Resource[] NO_RESOURCES = {};

/**
* Comma-separated list of basenames (essentially a fully-qualified classpath
* location), each following the ResourceBundle convention with relaxed support for
* slash based locations. If it doesn't contain a package qualifier (such as
* "org.mypackage"), it will be resolved from the classpath root.
*/
private String basename = "messages";
//我们的配置文件可以直接放在类路径下叫messages.properties;
/**
* Message bundles encoding.
*/
private Charset encoding = Charset.forName("UTF-8");

/**
* Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles
* are cached forever.
*/
private int cacheSeconds = -1;

/**
* Set whether to fall back to the system Locale if no files for a specific Locale
* have been found. if this is turned off, the only fallback will be the default file
* (e.g. "messages.properties" for basename "messages").
*/
private boolean fallbackToSystemLocale = true;

/**
* Set whether to always apply the MessageFormat rules, parsing even messages without
* arguments.
*/
private boolean alwaysUseMessageFormat = false;
//为容器添加了ResourceBundleMessageSource 组件
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
if (StringUtils.hasText(this.basename)) {
//设置国际化资源文件的基础名(去掉语言国家代码的)
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
StringUtils.trimAllWhitespace(this.basename)));
}
if (this.encoding != null) {
messageSource.setDefaultEncoding(this.encoding.name());
}
messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
messageSource.setCacheSeconds(this.cacheSeconds);
messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat);
return messageSource;
}

还可以在SpringBoot的配置文件中使用spring.messages前缀配置国际化:

# 国际化配置文件(包名.基础名)
spring.messages.basename=i18n.login

【2】编写国际化资源文件

抽取页面需要显示的国际化消息,编写国际化资源文件。

login.properties

SpringBoot - 资源国际化_mvc

如图所示,idea会自动切换到国际化视图。另外,在编写properties时,除了传统的使用key:value在properties中编写外,还可以如下所示:

SpringBoot - 资源国际化_资源国际化_02


【3】页面示例

如下所示,在页面中使用properties的属性:

<label class="sr-only" th:text="#{login.username}">Username</label>
<input type="text" name="username" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
<label class="sr-only" th:text="#{login.password}">Password</label>
<input type="password" name="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}]]
//这里使用行内写法,input是自结束标签,没有标签体
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>

注意这里使用的是#{…}语法,可以参考Thymeleaf中第四章Standard Expression Syntax中4.1Messages讲解,如下图.

SpringBoot - 资源国际化_mvc_03
SpringBoot - 资源国际化_SpringBoot_04


测试结果:

浏览器设置英文:

SpringBoot - 资源国际化_bundle_05


浏览器设置中文:

SpringBoot - 资源国际化_mvc_06


【4】SpringBoot对资源国际化解析原理

首先明白两个组件:国际化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;
}

如果spring.mvc.locale没有指定,那么就返回一个AcceptHeaderLocaleResolver 。如果指定了spring.mvc.locale,那么就返回FixedLocaleResolver。


② 查看AcceptHeaderLocaleResolver

@Override
public Locale resolveLocale(HttpServletRequest request) {
Locale defaultLocale = getDefaultLocale();
if (defaultLocale != null && request.getHeader("Accept-Language") == null) {
return defaultLocale;
}
Locale requestLocale = request.getLocale();
if (isSupportedLocale(requestLocale)) {
return requestLocale;
}
Locale supportedLocale = findSupportedLocale(request);
if (supportedLocale != null) {
return supportedLocale;
}
return (defaultLocale != null ? defaultLocale : requestLocale);
}

从请求头里面解析Locale!这也就是为什么浏览器切换语言显示不同资源的原理。


【5】中英切换

如下图所示,通过点击中文英文按钮切换不同资源显示:

SpringBoot - 资源国际化_资源国际化_07


① 页面修改如下:

<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>

这里使用的是Thymeleaf模板引擎中链接参数的写法​​(key=value)​​,也可以使用原始写法如下:

<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>

② 编写自定义区域信息解析器

/**
* 可以在连接上携带区域信息
*/
public class MyLocaleResolver 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 request, HttpServletResponse response, Locale locale) {

}
}

将其添加到容器中:

@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver(){

return new MyLocaleResolver();
}
}

③ Locale.getDefault()

该方法根据系统环境拿到Locale,如本机上如果没有传 l 参数,那么就会返回defaultLocale。此时无论浏览器语言设置什么,都显示中文。

/**
* Gets the current value of the default locale for this instance
* of the Java Virtual Machine.
* <p>
* The Java Virtual Machine sets the default locale during startup
* based on the host environment. It is used by many locale-sensitive
* methods if no locale is explicitly specified.
* It can be changed using the
* {@link #setDefault(java.util.Locale) setDefault} method.
*
* @return the default locale for this instance of the Java Virtual Machine
*/
public static Locale getDefault() {
// do not synchronize this method - see 4071298
return defaultLocale;
}