Spring ApplicationContext的国际化支持
来源:互联网 发布:淘宝网店 不进货 编辑:程序博客网 时间:2024/05/09 08:47
ApplicationContext接口继承MessageSource接口,因此具备国际化功能。下面是MessageSource接口定义的三个国际化方法。
》String getMessage(String code, Object[] args, Locale loc)
》String getMessage(String code, Object[] args, String default, Locale loc)
》String getMessage(MessageSourceResolvable resolvable, Locale locale)
ApplicationContext正式通过这三个方法来完成国际化的,当程序创建ApplicationContext容器时,Spring自动查找在配置文件中名为 messageSource 的 Bean 实例,一旦找到这个Bean实例,上诉3个方法的调用被委托给该MessageSource Bean。如果没有改Bean,ApplicationContext会查找其父定义中的messageSource Bean;如果找到,他将作为 messageSource Bean使用。
如果无法找到 messageSource Bean.系统会创建一个空的 StaticMessageSource Bean,该Bean能接受上诉三个方法的调用。
在Spring中配置 messageSource Bean通常使用 ResourceBundleMessageSource 类。看下面的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 7 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 8 <property name="basenames"> 9 <list>10 <value>message</value>11 <!-- 如果有多个资源文件,全部列在此处 -->12 </list>13 </property>14 </bean>15 </beans>
上面的配置定义了一个 messageSource Bean,该Bean实例只指定了一份国际化资源文件,其baseName 是message
给出如下两份资源文件
第一份为美式英语的资源文件,文件名 message_en_US.properties
1 hello=welcome,{0}2 now=now is \:{0}
第二份为简体中文的资源文件,文件名 message_zh_CN.properties
1 hello=欢迎你{0}2 now=现在时间是:{0}
因为该资源文件包含了非西欧文字,因此,应使用 native2ascii 工具将这份资源文件国际化,命令如下:
1 native2ascii -encoding UTF-8 message.properties message_zh_CN.properties
执行上面命令后将会看到系统会多出一个message_zh_CN.properties文件,该文件将作为简体中文实际的国家化资源文件。此时,程序拥有了两份资源文件,可以自适应美式英语和简体中文的环境,主程序如下部分:
1 ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml"); 2 //创建参数数组 3 String[] a ={"张三"}; 4 //使用getMessage方法获取本地化信息。Locale的getDefault方法返回计算机环境默认Locale 5 String hello = ac.getMessage("hello", a, Locale.getDefault()); 6 Object[] b = {new Date()}; 7 //使用Locale.US获取美国英语环境 8 String now = ac.getMessage("now", b, Locale.US); 9 System.out.println(hello);10 System.out.println(now);
结果输出
1 欢迎你张三2 now is :9/12/13 7:34 PM
Spring的国际化支持,其实是建立在Java程序国际化的基础之上。其核心思路都是将程序需要实现国际化的信息写入资源文件,而代码中仅仅使用相应的个信息的Key。
- Spring Applicationcontext的国际化支持
- Spring Applicationcontext的国际化支持
- Spring ApplicationContext的国际化支持
- ApplicationContext的国际化支持
- ApplicationContext的国际化支持
- Spring对国际化的支持
- spring ApplicationContext.xml之基于Session的国际化配置初探
- spring的ApplicationContext,事件监听器与国际化消息
- spring ApplicationContext生成和获取国际化信息的方法
- Spring 国际化的支持:Spring MVC Velocity
- Spring -- 国际化的支持(MessageResource用法)
- Spring 国际化支持
- spring 国际化支持
- spring国际化支持
- Spring支持国际化
- Spring国际化支持
- Spring Boot国际化支持
- 《Pro Spring》学习笔记之ApplicationContext国际化
- OpenCV代码提取:remap函数的实现
- 一个位域问题
- 程序员面试金典题解 二叉树平衡检查
- 剑指offer 17题 【代码的鲁棒性】合并两个排序的链表
- Java隐式类型转换
- Spring ApplicationContext的国际化支持
- 【Leetcode】Word Ladder
- 【Unity Shader】案例之地球云图(二)
- 122. Best Time to Buy and Sell Stock II
- Separating Pebbles数学,暴力
- 【Android基础】 Launch Mode
- 华为机试题--16.坐标移动
- Android坐标系
- leetcode60. Permutation Sequence