前言
SpringBoot自动配置好了管理国际化资源文件的组件
@ConfigurationProperties(prefix = "spring.messages")
public class MessageSourceAutoConfiguration {
/**
Comma-separated list of basenames, each following the ResourceBundle convention.
Essentially a fully-qualified classpath location.
If it doesn't contain a package qualifier (such as "org.mypackage"),
it will be resolved from the classpath root.
*/
//我们的配置文件可以直接放在类路径下叫message.properties
private String basename = "messages";
@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;
}
}
具体操作
1、创建国际化的配置文件,放在resources下的一个文件夹里(这里只配置中文和英文)
2、点开配置文件login.properties配置文件,选择中间部分左下角的Resource Bundle ,然后选择中间部分的左上角 “+” 号 ,创建一个引用参数,然后点进参数,配置浏览器所显示的内容
3、application.properties配置文件中,引入login.properties配置文件
spring.messages.basename=login/login
4、编写html页面
首先在html页面中的html标签中引入thymeleaf模板引擎
<html lang="en" xmlns:th="http://www.thymeleaf.org">
然后在body里面写login.properties的参数
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<div th:text="#{login.top}">hello world</div>
<div th:text="#{login.username}">username</div>
</body>
</html>
写一个HelloController,访问html页面
@Controller
public class HelloController {
//查询数据在html页面展示
@RequestMapping("/login")
public String success(){
return "login";
}
}
这样如果浏览器的语言是中文,将显示中文的配置信息,英文显示英文的配置信息
如果中文显示乱码,需要修改idea的一些配置
原理:
国际化Locale(区域信息对象)有一个组件LocaleResolve(获取区域对象信息);
@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;
}
//默认的就是根据请求头带来的区域信息获取Locale进行国际化
点击链接切换国际化
1、创建一个组件MyLocaleResolver让他继承LocaleResolver然后重写里面的方法
/**
* 可以在连接上携带区域信息
*/
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
//获取参数的值
String l = request.getParameter("l");
Locale locale = Locale.getDefault();
//如果不为null
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) {
}
}
2、写一个配置类MyMvcConfig,让这个组件生效
@Configuration
public class MyMvcConfig {
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
3、完善html页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<div th:text="#{login.top}">hello world</div>
<div th:text="#{login.username}">username</div>
<a th:href="@{/login.html(l='zh_CN')}">中文</a>
<a th:href="@{/login.html(l='en_US')}">英文</a>
</body>
</html>
点击中文或英文即可切换对应的内容