一、国际化在实际代码中是非常常见的一中方式。为了结合web做一下语言上面的切换,而达到展示的目的。

  二、这里呢,主要是介绍spring中对于国际化做了哪些处理。

  三、实现方式

  1)xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message.test</value>
</list>
</property>
</bean>
</beans>

  备注:<!--这里id必须是messageSource,后续会说为什么-->

  2)语言文件

   

  写法:

testtest=\u6d4b\u8bd5{0}
testtest=test{0}

  3)应用过程:

     ApplicationContext context = new ClassPathXmlApplicationContext("spring-message.xml");
ResourceBundleMessageSource messageSource = context.getBean("messageSource", ResourceBundleMessageSource.class);
String messageZh = messageSource.getMessage("testtest", new Object[]{"数据"}, Locale.CHINESE);
String messageEn = messageSource.getMessage("testtest", new Object[]{"data"}, Locale.ENGLISH);
System.out.println(messageZh);
System.out.println(messageEn);

  4)效果

  

  四、当然这里只是后台的实现方式,因为是国际化吗。肯定是和web端密切相关的。传递方式

  1)request:设置消息头等。通过拦截请求设置获取当前语言。然后在后端处理国际化。

  2)cookie:这种方式通过请求的方式修改语言。

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieMaxAge" value="1800000"/>
<property name="defaultLocale" value="zh_CN"/>
<property name="cookieName" value="Language"/>
</bean>

  3)session:这个方式的好处是在于会话处理。相对于cookie,会话可以根据session的时间而保持。

  <bean id="sessionLocaleResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="zh"/>
</bean>

  五、spring部分

  1)refresh()方法中的this.initMessageSource();

  2)实现:

protected voidinitMessageSource() {
//获取beanfactory
ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
//判断是否包含messageSource的bean
if (beanFactory.containsLocalBean("messageSource")) {
this.messageSource = (MessageSource)beanFactory.getBean("messageSource", MessageSource.class);
//如果父类存在,设置父类
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
HierarchicalMessageSource hms = (HierarchicalMessageSource)this.messageSource;
if (hms.getParentMessageSource() == null) {
hms.setParentMessageSource(this.getInternalParentMessageSource());
}
}
if (this.logger.isDebugEnabled()) {
this.logger.debug("Using MessageSource [" + this.messageSource + "]");
}
} else {
//spring默认的国际化使用方式
DelegatingMessageSource dms = new DelegatingMessageSource();
dms.setParentMessageSource(this.getInternalParentMessageSource());
this.messageSource = dms;
beanFactory.registerSingleton("messageSource", this.messageSource);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Unable to locate MessageSource with name 'messageSource': using default [" + this.messageSource + "]");
}
}
}

  3)从代码中可以看出:这里默认访问的是messageSource,如果配置成其他名称。在使用的时候会抛类型异常。

  六、简易版国际化实现过程

     ResourceBundle resourceBundle = ResourceBundle.getBundle("message.test", Locale.US);
String testest = resourceBundle.getString("testtest");
String format = MessageFormat.format(testest, new Object[]{"data"});
System.out.println(format);

  

spring源码-国际化-3.5的更多相关文章

  1. spring源码-bean之增强初始化-3

    一.ApplicationContext的中文意思是“应用上下文”,它继承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在国际化支持.资源访问(如URL和文件).事件传播 ...

  2. Spring 源码(8)Spring BeanPostProcessor的注册、国际化及事件发布机制

    上一篇文章https://www.cnblogs.com/redwinter/p/16198942.html介绍了Spring的注解的解析过程以及Spring Boot自动装配的原理,大概回顾下:Sp ...

  3. spring源码:学习线索(li)

    一.spring xml配置(不包括AOP,主要了解在初始化及实例化过程中spring配置文件中每项内容的具体实现过程,从根本上掌握spring) <bean>的名字 &,alia ...

  4. spring源码浅析——IOC

    =========================================== 原文链接: spring源码浅析--IOC   转载请注明出处! ======================= ...

  5. 【Spring源码分析】Bean加载流程概览

    代码入口 之前写文章都会啰啰嗦嗦一大堆再开始,进入[Spring源码分析]这个板块就直接切入正题了. 很多朋友可能想看Spring源码,但是不知道应当如何入手去看,这个可以理解:Java开发者通常从事 ...

  6. 【Spring源码分析】非懒加载的单例Bean初始化前后的一些操作

    前言 之前两篇文章[Spring源码分析]非懒加载的单例Bean初始化过程(上篇)和[Spring源码分析]非懒加载的单例Bean初始化过程(下篇)比较详细地分析了非懒加载的单例Bean的初始化过程, ...

  7. 【spring源码分析】IOC容器初始化(一)

    前言:spring主要就是对bean进行管理,因此IOC容器的初始化过程非常重要,搞清楚其原理不管在实际生产或面试过程中都十分的有用.在[spring源码分析]准备工作中已经搭建好spring的环境, ...

  8. spring源码:学习线索

    一.spring xml配置(不包括AOP,主要了解在初始化及实例化过程中spring配置文件中每项内容的具体实现过程,从根本上掌握spring) <bean>的名字 &,alia ...

  9. [spring源码] 小白级别的源码解析(一)

    一直都在用spring,但是每次一遇到spring深入的问题,就是比较懵的状态.最近花了段时间学习了一下spring源码. 1,spring版本介绍 虽然工作中,一直在用到spring,可能有时候,并 ...

随机推荐

  1. bzoj1434 [ZJOI2009]染色游戏

    Description 一共n × m 个硬币,摆成n × m 的长方形.dongdong 和xixi 玩一个游戏, 每次可以选择一个连通块,并把其中的硬币全部翻转,但是需要满足存在一个 硬币属于这个 ...

  2. CF839D Winter is here

    题目分析 显然我们不可能直接计算每一个子序列的贡献,而应该计算对于每一个gcd对答案的贡献. 考虑容斥.按照套路: 设\(q(i)\)表示序列\(gcd\)为\(i\)的倍数的序列长度和. 设\(g( ...

  3. python 中if-else的多种简洁的写法

    因写多了判断语句,看着短短的代码却占据来好几行,于是便搜下if-else简洁的写法,结果也是发现新大陆 4种: 第1种:__就是普通写法 a, b, c = 1, 2, 3 if a>b: c ...

  4. 10、SpringBoot-CRUD登陆拦截

    1.前端页面的设置 index.html <input type="text" class="form-control" name="usern ...

  5. DataTables.Queryable Sample

    1.DataTables.Queryable的例子项目使用了SQL Server CE数据库,花了几分钟时间转为使用LocalDB. 完整Web.config文件如下: <?xml versio ...

  6. __future__模块

    Python提供了__future__模块,把下一个新版本的特性导入到当前版本,于是我们就可以在当前版本中使用一些新版本的特性,比如除法: 在Python 2.x中,对于除法有两种情况,如果是整数相除 ...

  7. 在eclipse中查看HttpServlet源码失败的解决方法

    在初次建立java EE 项目时,想要查看HttpServlet源码时会提示失败, 按照网上的方式,将Tomcat中lib中的servlet-api.jar的包导进去,发现并不管用.并且提示里面并不包 ...

  8. java soa接口测试,可以使用http协议调用

    post调用url:“接口url”+/rpc post调用参数body: { "ver": "接口版本号", "soa":{"re ...

  9. java文件系统中的的NIO与IO

    java从jdk1.4后就引入了java NIO机制: NIO的显著特点就是通道(channel).缓冲(buffer).选择器(selector),NIO机制中添加了传统I/O机制中没有的非阻塞调用 ...

  10. Spring Boot集成Hazelcast实现集群与分布式内存缓存

    Hazelcast是Hazelcast公司开源的一款分布式内存数据库产品,提供弹性可扩展.高性能的分布式内存计算.并通过提供诸如Map,Queue,ExecutorService,Lock和JCach ...