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.国际化的更多相关文章

  1. spring boot(10) 基础学习内容

    A Spring boot(10) 基础学习内容 B SpringBoot(16) 基础学习内容

  2. spring boot(10)-tomcat jdbc连接池

    默认连接池 tomcat jdbc是从tomcat7开始推出的一个连接池,相比老的dbcp连接池要优秀很多.spring boot将tomcat jdbc作为默认的连接池,只要在pom.xml中引入了 ...

  3. Spring Boot Security 国际化 多语言 i18n 趟过巨坑

    网上很多的spring boot国际化的文章都是正常情况下的使用方法 如果你像我一样用了Spring Security 那么在多语言的时候可能就会遇到一个深渊 Spring Security里面的异常 ...

  4. Spring Boot (10) mybatis三种动态sql

    脚本SQL xml配置方式见mybatis讲解,下面是用<script>的方式把它照搬过来,用注解来实现.适于xml配置转换到注解配置 @Select("<script&g ...

  5. 58. Spring Boot国际化(i18n)【从零开始学Spring Boot】

    国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不 ...

  6. Spring Boot with Spring-Data-JPA学习案例

    0x01 什么是Spring Boot? Spring Boot是用来简化Spring应用初始搭建以及开发过程的全新框架,被认为是Spring MVC的"接班人",和微服务紧密联系 ...

  7. Spring Boot 2.0 入门指南

    0x01 什么是Spring Boot? Spring Boot是用来简化Spring应用初始搭建以及开发过程的全新框架,被认为是Spring MVC的“接班人”,和微服务紧密联系在一起. 0x02 ...

  8. Spring Boot系列学习文章(一) -- Intellij IDEA 搭建Spring Boot项目

    前言: 最近做的一个项目是用Spring Boot来做的,所以把工作中遇到的一些知识点.问题点整理一下,做成一系列学习文章,供后续学习Spring Boot的同仁们参考,我也是第一次接触Spring ...

  9. Spring Boot Reference Guide

    Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch,  ...

  10. spring boot 中文文档地址

    spring boot 中文文档地址     http://oopsguy.com/documents/springboot-docs/1.5.4/index.html Spring Boot 参考指 ...

随机推荐

  1. Python网络爬虫_爬取Ajax动态加载和翻页时url不变的网页

    1 . 什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新 ...

  2. CentOS7安装MySQL报错Failed to start mysqld.service: Unit not found解决办法

    1 ~]# systemctl start mysql.service 要启动MySQL数据库是却是这样的提示 1 ~]# Failed to start mysqld.service: Unit n ...

  3. docker和Dockerfile

    目录: 1.docker为什么会出现? 2.docker的理念 3.容器化技术 4.docker三要素 5.docker安装 6.docker帮助命令 7.Docker阿里云镜像加速器配置. 8.do ...

  4. php重写与重载

    转载:https://blog.csdn.net/binghui1990/article/details/9105237 重写/覆盖 override   指:子类重写了父类的同名方法 (注:1.重写 ...

  5. mongodb性能测试:long时间戳与string格式时间

    string格式时间写入数据: { "_id" : ObjectId("5d314731a96f332d6c3193d4"), "news_id&qu ...

  6. ubuntu搭建、安装gitlab服务器以及初始化密码

    本为14.04 在搭建之前要确定其网络环境是没有问题.用root身份进行操作 1.安装和配置必要的依赖关系 apt-get update apt-get install -y curl openssh ...

  7. python学习之路(8)

    定义函数 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 我们以自定义一个求绝对值的my_ ...

  8. [CSP-S模拟测试]:小W的魔术(数学 or 找规律)

    题目传送门(内部题130) 输入格式 第一行一个整数$n$,表示字符串的长度. 第二行一个只包含小写字母的字符串$s$. 输出格式 一行一个整数表示答案对$998244353$取模后的结果. 样例 样 ...

  9. python3笔记三:运算符与表达式

    一:学习内容 算术运算符:+(加).-(减).*(乘)./(除).%(取模).**(求幂).//(取整) 赋值运算符:= 复合运算符:+=.-=.*=./=.%=.**=.//= 位运算符:& ...

  10. LeetCode 198. 打家劫舍(House Robber)LeetCode 213. 打家劫舍 II(House Robber II)

    打家劫舍 题目描述 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报 ...