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 参考指 ...
随机推荐
- 对React的研究-------------引用
DOM 的全称是 Document Object Model (文档对象模型) 响应式 UI const ThinkerWithHat = ({ hat }) => ( <div> ...
- vscode存盘时格式化
1.文件->首选项->设置
- JAVA学长
https://www.cnblogs.com/chenmingjun/p/9697371.html
- 在对 Angular 的文档 aio 进行编译的时候提示错误
error angular-examples-master@1.0.0: The engine "yarn" is incompatible with this module. E ...
- 记一次AD域控客户端不能正常加域的故障处理
1.1 症状现象 DNS 服务器无法在应用程序目录分区 < 分区名称 > 从 Active Directory 中打开区域 < 区域 >.这台 DNS 服务器配置获取并使用此 ...
- 如何将项目托管到Github上
将本地项目放到GitHub上托管并展示 传送门 利用Github Pages展示自己的项目 传送门 git Please tell me who you are解决方法 传送门 git config ...
- 如何在一个页面使用多个router-view显示不同的内容
todo https://www.jianshu.com/p/92f61bf9db81
- Linux上python3的安装和使用
centos7默认是装有python的,咱们先看一下 #检查python版本 [root@oldboy_python ~ 17:23:54]#python -V Python 2.7.5 但是 pyt ...
- java栈和队列
栈 可变长数组实现 链表实现 数组与链表的对比队列 链表实现 栈 下压栈(简称栈)是一种基于后进后出(LIFO)策略的集合类型.这里学习分别用数组和链表这两种基础数据结构来实现 ...
- Android采用pm命令静默卸载应用
卸载app的方式有多种,可以直接调用android系统的卸载程序,但是这样会调出android卸载提示框,问题就是真的不好看. 所以采用静默卸载的方式,避免弹出系统提示框. 方法一(调用系统卸载程序) ...