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. hdu 2604 Queuing(推推推公式+矩阵快速幂)

    Description Queues and Priority Queues are data structures which are known to most computer scientis ...

  2. 原生Js_制作简易日历

    javascript制作简易日历,月份信息已经放在一个数组中,在<script>...</script>中编写代码实现其功能 实现步骤 a) 获取需要操作的dom对象 b) 在 ...

  3. uswgi

    1.安装uwsgi注意: 1)在系统环境安装,非虚拟环境 2)使用对应python版本安装 3)要先安装python开发包 ###sudo apt-get install python3.6-dev ...

  4. uni-app tabBar 踩坑

    { "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages { "pa ...

  5. 访问 Django 项目的静态资源

    from django.urls import path, re_path from django.conf import settingsfrom django.views.static impor ...

  6. Git-Runoob:Git 服务器搭建

    ylbtech-Git-Runoob:Git 服务器搭建 1.返回顶部 1. Git 服务器搭建 上一章节中我们远程仓库使用了 Github,Github 公开的项目是免费的,但是如果你不想让其他人看 ...

  7. 手动清空微信PC客户端数据

    微信PC客户端,用久了之后,会产生大量数据,包括聊天记录.聊天图片.视频等等,非常占存储空间,除非很重要的聊天记录或文件,建议额外保存,其他的可以手动删掉就好,可以节省存储空间. 1.找到[C:\Us ...

  8. hive跑mapreduce报java.lang.RuntimeException: Error in configuring object

    写于2016.7月 最近项目需要在hbase上做统计分析,在本机上装了hive,结果跑小批量数据sum时报错: hive> select count(*) from page_view; Tot ...

  9. iOS 图表工具charts之PieChartView

    关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...

  10. 前端必须掌握的 nginx 技能(2)

    概述 作为一个前端,我觉得必须要学会使用 nginx 干下面几件事: 代理静态资源 设置反向代理(添加https) 设置缓存 设置 log 部署 smtp 服务 设置 redis 缓存(选) 下面我按 ...