Spring ApplicationContext的国际化支持
2013-09-12 19:39
274 查看
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 类。看下面的配置文件
上面的配置定义了一个 messageSource Bean,该Bean实例只指定了一份国际化资源文件,其baseName 是message
给出如下两份资源文件
第一份为美式英语的资源文件,文件名 message_en_US.properties
第二份为简体中文的资源文件,文件名 message_zh_CN.properties
因为该资源文件包含了非西欧文字,因此,应使用 native2ascii 工具将这份资源文件国际化,命令如下:
执行上面命令后将会看到系统会多出一个message_zh_CN.properties文件,该文件将作为简体中文实际的国家化资源文件。此时,程序拥有了两份资源文件,可以自适应美式英语和简体中文的环境,主程序如下部分:
结果输出
Spring的国际化支持,其实是建立在Java程序国际化的基础之上。其核心思路都是将程序需要实现国际化的信息写入资源文件,而代码中仅仅使用相应的个信息的Key。
》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 类。看下面的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>message</value> <!-- 如果有多个资源文件,全部列在此处 --> </list> </property> </bean> </beans>
上面的配置定义了一个 messageSource Bean,该Bean实例只指定了一份国际化资源文件,其baseName 是message
给出如下两份资源文件
第一份为美式英语的资源文件,文件名 message_en_US.properties
hello=welcome,{0} now=now is \:{0}
第二份为简体中文的资源文件,文件名 message_zh_CN.properties
hello=欢迎你{0} now=现在时间是:{0}
因为该资源文件包含了非西欧文字,因此,应使用 native2ascii 工具将这份资源文件国际化,命令如下:
native2ascii -encoding UTF-8 message.properties message_zh_CN.properties
执行上面命令后将会看到系统会多出一个message_zh_CN.properties文件,该文件将作为简体中文实际的国家化资源文件。此时,程序拥有了两份资源文件,可以自适应美式英语和简体中文的环境,主程序如下部分:
ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml"); //创建参数数组 String[] a ={"张三"}; //使用getMessage方法获取本地化信息。Locale的getDefault方法返回计算机环境默认Locale String hello = ac.getMessage("hello", a, Locale.getDefault()); Object[] b = {new Date()}; //使用Locale.US获取美国英语环境 String now = ac.getMessage("now", b, Locale.US); System.out.println(hello); System.out.println(now);
结果输出
欢迎你张三 now is :9/12/13 7:34 PM
Spring的国际化支持,其实是建立在Java程序国际化的基础之上。其核心思路都是将程序需要实现国际化的信息写入资源文件,而代码中仅仅使用相应的个信息的Key。
相关文章推荐
- Spring Applicationcontext的国际化支持
- Spring -- 国际化的支持(MessageResource用法)
- Spring 国际化支持 消息传递 以及 AOP 面向方面编程
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
- Spring使用拦截器支持国际化(转)
- Spring ApplicationContext的国际化支持
- Spring对国际化的支持
- Spring 国际化的支持:Spring MVC Velocity
- spring_02国际化支持
- spring国际化支持
- Spring 国际化支持
- 7.4.3: 使用spring容器---ApplicationContext的国际化支持
- Spring学习之国际化支持
- JSP Spring ApplicationContext的国际化支持
- Spring使用拦截器支持国际化(转)
- 【Spring学习笔记-3】国际化支持
- spring 国际化支持
- Spring Applicationcontext的国际化支持
- Spring的数据库支持 推荐
- Spring MVC+Freemarker+Javascript的多语言(国际化i18n/本地化)和主题(Theme)实现