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. layer 回调

    目前使用的layer版本为1.8.5 在调用layer的JS中,使用end来进行处理 $.layer({ type : 2, shadeClose : true, title : '选择人员', cl ...

  2. jquery type属性 语法

    jquery type属性 语法 作用:type 属性描述触发哪种事件类型.大理石直角尺 语法:event.typ 参数: 参数 描述 event     必需.规定要检查的事件.这个 event 参 ...

  3. 简单加密 DEncrypt

    /// <summary> /// Encrypt 的摘要说明. /// </summary> public class DEncrypt { /// <summary& ...

  4. codevs 2597 团伙x

                         题目描述 Description 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的 ...

  5. maven打包加时间戳方法总结

    基于Maven的项目,发布时需要打包,如tar.gz.web项目打成war格式包.每次打包时希望自己加上时间戳,假如我的项目名是myproject,默认打包后名为myproject.war.而我希望的 ...

  6. mybatis plus table doesn't exists

    使用@TableName 注解即可 实际上就是建立bean与表名的连接

  7. 如何在外部获取当前A标签的ID值

    <div class="diskmain"> <ul id="folder"> <li><span class='do ...

  8. centos6 升级php版本

    配置yum源 追加CentOS 6.8的epel及remi源. # rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel- ...

  9. IDEA项目添加第三方依赖

    1.在项目WEB-INF文件夹下面新建一个lib文件夹并将我们需要的第三方jar包拷贝进去: Ctrl+Shift+Alt+S打开项目配置页面: 选择我们存放jar包的文件夹: 选择我们需要引入目标依 ...

  10. 自定义ListView实现下拉刷新,下拉加载的功能

    package com.loaderman.myrefreshlistviewdemo; import android.content.Context; import android.util.Att ...