spring boot-10.国际化
1.在原来spring MVC 中国际化实现步骤
(1)编写国际化配置文件
(2)使用ResourceBundleMessageSource管理国际化资源文件
(3)在页面中取国际化信息
2.spring boot实现
spring boot 默认为我们配置了 ResourceBundleMessageSource。org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration 为我们提供了国际化的自动配置。
@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;
}
其中指定了默认的国际化资源配置文件是类路径下的messages.properties。我们可以在全局配置文件中修改国际化配置文件的名称,例如指定国际化配置文件为类路径下的i18n下的login.properties
spring.messages.basename=i18n.login
当然国际化配置文件可以根据国家和语言信息来指定,如果当前浏览器环境 的语言信息没有匹配的国际化配置文件,就读取默认的国际化配置文件。例如编写了三个国际化配置文件,分别为login_en_US.properties,login_zh_CN.properties,login.properties,分别对应英文环境,中文环境,默认环境。

3.页面输出国际化信息
jsp:通过fmt:message
freemaker:<@spring.message code="loginFrom.username"/>
themleaf:#{message.password}
4.实现效果


4.自定义区域信息解析器
@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;
}
以上是org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration 中关于区域信息解析器的配置,可以看到注解,@ConditionalOnMissingBean是在容器中没有LocaleResolver 时才生效,所有我们可以实现自己的区域信息解析器,并将它添加到容器中。可以通过在全局配置文件中配置spring.mvc来添加,或者在@Configuration配置类中通过@Bean来添加自定义的区域信息解析器。
(1)实现自己的区域信息解析器
public class MylocalResolver implements LocaleResolver{
@Override
public Locale resolveLocale(HttpServletRequest request) {
// TODO Auto-generated method stub
Locale locale = Locale.getDefault();
String local =request.getParameter("local");
System.out.println("====="+local);
if(!StringUtils.isEmpty(local)) {
locale = new Locale(local.split("_")[0], local.split("_")[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
// TODO Auto-generated method stub
}
}
(2)添加进容器
@Configuration
public class MyMVCcCConfig extends WebMvcConfigurerAdapter{ @Bean
public LocaleResolver localeResolver() {
return new MylocalResolver();
}
}
spring boot-10.国际化的更多相关文章
- spring boot(10) 基础学习内容
A Spring boot(10) 基础学习内容 B SpringBoot(16) 基础学习内容
- spring boot(10)-tomcat jdbc连接池
默认连接池 tomcat jdbc是从tomcat7开始推出的一个连接池,相比老的dbcp连接池要优秀很多.spring boot将tomcat jdbc作为默认的连接池,只要在pom.xml中引入了 ...
- Spring Boot Security 国际化 多语言 i18n 趟过巨坑
网上很多的spring boot国际化的文章都是正常情况下的使用方法 如果你像我一样用了Spring Security 那么在多语言的时候可能就会遇到一个深渊 Spring Security里面的异常 ...
- Spring Boot (10) mybatis三种动态sql
脚本SQL xml配置方式见mybatis讲解,下面是用<script>的方式把它照搬过来,用注解来实现.适于xml配置转换到注解配置 @Select("<script&g ...
- 58. Spring Boot国际化(i18n)【从零开始学Spring Boot】
国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不 ...
- Spring Boot with Spring-Data-JPA学习案例
0x01 什么是Spring Boot? Spring Boot是用来简化Spring应用初始搭建以及开发过程的全新框架,被认为是Spring MVC的"接班人",和微服务紧密联系 ...
- Spring Boot 2.0 入门指南
0x01 什么是Spring Boot? Spring Boot是用来简化Spring应用初始搭建以及开发过程的全新框架,被认为是Spring MVC的“接班人”,和微服务紧密联系在一起. 0x02 ...
- Spring Boot系列学习文章(一) -- Intellij IDEA 搭建Spring Boot项目
前言: 最近做的一个项目是用Spring Boot来做的,所以把工作中遇到的一些知识点.问题点整理一下,做成一系列学习文章,供后续学习Spring Boot的同仁们参考,我也是第一次接触Spring ...
- Spring Boot Reference Guide
Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, ...
- spring boot 中文文档地址
spring boot 中文文档地址 http://oopsguy.com/documents/springboot-docs/1.5.4/index.html Spring Boot 参考指 ...
随机推荐
- js-点击+加关注变成已关注,已关注状态时,鼠标滑动上的状态时取消关注
效果: HTML: <div class="rightBtn cur">+关注</div> CSS: .rightBtn{ width: 80px; hei ...
- TTTTTTTTTTTT 百度之星D map+hash
Problem D Accepts: 2806 Submissions: 8458 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6 ...
- TTTTTTTTTTTTTTTT POJ 2723 楼层里救朋友 2-SAT+二分
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8211 Accepted: 3162 Des ...
- Libraries&Workflow for a modern geospatial processing(现代地理空间处理的库与工作流)
Libraries for a modern geospatial workflow现代地理空间工作的类库 Distribution Writing, Running, and Distributin ...
- Android 一般动画animation和属性动画animator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- Java缓存技术有哪些
我们用ehcache在本地,分布式用redis和memcache,各有各的好处,现在企业都是应用很多种中间件供俺们码农选择.
- 【转】如何在Oracle中复制表结构和表数据
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- Eclipse中文插件安装教程
先运行英文版Eclipse 点击Help选择Install New Software 直接点击Add按钮 Name输入: Babel Location输入: http://download.eclip ...
- 选题 Scrum立会报告+燃尽图 02
此作业要求参见[https://edu.cnblogs.com/campus/nenu/2019fall/homework/8683] 一.小组介绍 组长:贺敬文 组员:彭思雨 王志文 位军营 杨萍 ...
- echart 饼图图例legend支持滑动
ps: 以下针对option操作 文章目录 图例过多加上滚动条图例形状图例自定义显示图例过多加上滚动条 legend:{ top:'50', bottom:'50', type:'scroll',} ...