`
xp84012493
  • 浏览: 58543 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

spring的messageSource

阅读更多

*******************************************************

作者:陈刚,程序员,广西省桂林人,广西师范大学数学系97届毕业。

blog:http://blog.csdn.net/glchengang

Email:glchengang@yeah.net 

******************************************************* 

第三章  IoC中的国际化(CVS版本:V002

从这一章开始,我将把实例的项目打开一个CVS版本,不知谁能提供一个FTP空间?

3.1        前言

标题准确来说应该是“使用Spring中的IoC功能来实现我们所开发项目系统的国际化”,国际化不是针对IoC的,而是针对你开发的整个系统。

如果你使用过Eclipse的国际化,或者用过Eclipse的“外部化字符串”向导(Eclipse主菜单:源代码->外部化字符串),那么对Spring提供的国际化功能应该是非常容易理解,两者基本一样,或者说各种Java程序的国际化方式都基本一样。

先谈谈Eclipse国际化的两个组成部分:*.properties的资源文件、获取资源文件内容的Message类。

Spring则和Eclipse的处理类似:资源文件两者是一样的,不同语言的翻译放在不同的资源文件里,连起名规则都一样;EclipseMessage类要自己写(代码通用,复制以前项目的即可,或用Eclipse的向导生成一个也行),Spring则已经有写好的Message类,我们在IoCxml文件里注册一下即可使用(也可以实现SpringMessageSource接口,自己来写一个Message类,代码并不复杂,不过这没什么必要,用Spring提供的就行了)。

无论是EclipseMessage类,还是Spring的自带的Message类,或是我们自己写一个Message类,都是使用JDKjava.util.ResourceBundle类来实现*.properties文件的读取。

3.2  简单实例

假设我们有如下程序,程序的作用是打印出一个字符串

package cn.com.chengang.spring;

public class MessageTest {

    public static void main(String[] args) {

        String str = "ChenGang";

        System.out.println(str);

    }

}

现在,我们要让这个程序能够根据使用者的语言情况输出不同的字符,比如:对英文使用者输出“ChenGang”,对中文使用者输出“陈刚”,对台湾使用输出“陳剛”等等。这个需求的实现方法如下:

1、创建一系列的资源文件

cn.com.chengang.spring包下创建以下文件:

1messages.properties(默认:英文),内容仅一句,如下

chengang=Giles

chengang”是键值,Giles是要输出的英文字符串

2messages_zh_CN.properties(简体中文)

chengang=\u9648\u521A

\u9648\u521A”是UNICODE码,对应的中文是“陈刚”

3messages_ zh_TW.properties(繁体中文)

chengang=\u9673\u525B

\u9673\u525B”对应的中文是“陳剛”

 

附注:由于中文是要转换成UNICODE码,在编辑和阅读上有诸多不便,如果是用EclipseIDE,则有一个编辑资源文件的插件jinto,下载网址是http://www.guh-software.de/,用它打开的资源文件如下图所示,可以看到三个资源在一个界面反映了出来。

如果你不用Eclipse,而是用EditplugsJDK的方式来编程(现在还有这样的原始人吗?),你也可以用JDK自带的native2ascii.exe程序来将中文字串转成UNICODE码。Ant中还提供了一个相应的任务:<native2ascii encoding="GBK" src="${src}" dest="${build}"/>,其中GBK是一个中国的字符集。

 

2、修改bean.xml

Spring自带的org.springframework.context.support.ResourceBundleMessageSource类注册到bean.xml中,这个类的作用是获取资源文件的内容,注册到IoCbean.xml文件中是为了自动获得此类的对象(Spring做了一些简化编程的处理)。

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

       <bean id="Chinese" class="cn.com.chengang.spring.Chinese"/>

       <bean id="American" class="cn.com.chengang.spring.American"/>

       <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

              <property name="basenames">

                     <list>

                            <value>cn.com.chengang.spring.messages</value>

                     </list>

              </property>

       </bean>

</beans>

代码说明:

l           id="messageSource" 的设置是不变的、必须的。

l           ResourceBundleMessageSourceSpring的一个Message类。这里还有一个选择,用ReloadableResourceBundleMessageSource类,此类可以提供不用重启即可重新加载资源文件的特性(前者对资源文件只加载一次)。对于那种有热修改资源文件的需求,后者比较合适,只是后者在效率上有可能有损耗,因为至少要多一些检查资源文件是否改变的代码(这只是我的猜测,我没有仔佃去读这段的源码)。

l           basenames”是不变的、必须的。它是ResourceBundleMessageSource的一个属性,在源代码中的定义是“private String[] basenames;”,可见它是一个字符串数组。

l           cn.com.chengang.spring.messages”是把资源文件的位置传入到basenames属性中。注意:三个资源文件只需要将共同的主名(红色字体)传入:messages.propertiesmessages_zh_CN.propertiesmessages_zh_TW.properties

 

3、使用。修改MessageTest类,如下

package cn.com.chengang.spring;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MessageTest {

    public static void main(String[] args) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");

        String str = ctx.getMessage("chengang", null, null);

        System.out.println(str);

    }

}

代码说明:

1main方法里

l           第一句取得bean.xml文件的配置信息。

l           第二句从资源文件里得到键值chengang对应的字符串。

l           第三句将字符串打印出来,结果是打印的是“陈刚”,说明读取的是messages_zh_CN.properties资源文件。

2ctx.getMessage("chengang", null, null);有三个参数:

l           第一个是资源文件的键值;

l           第二个是资源文件字符串的参数,由于本字符串没有参数,所以用一个null(后面给出了一个用到字符串参数的实例);

l           第三个是一个java.util. Locale类型的参数。参数为null,则表示根据使用者的语言环境来选择Locale,因为我用的是中文版的windows,所以在取字符串时它自动选择了messages_zh_CN.properties资源文件。
   
这其中还有一个控制点在JVMJVM会根据当前操作系统的语言环境进行相应处理,我们可以通过在JVM启动参数中追加“-Duser.language=zh_TW”来设定当前JVM语言类型,通过JVM级的设定,也可以实现自动切换所使用的资源文件类型。
   
所以这里面的控制语言的方式有三种:从最低层的操作系统的Locale设定,到更上一层的JVMLocale设定,再到程序一级的Locale设定。

3.3  资源文件的其他使用方式:

package cn.com.chengang.spring;

import java.util.Locale;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

import org.springframework.context.support.ResourceBundleMessageSource;

public class MessageTest {

    public static void main(String[] args) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");

        String str = ctx.getMessage("chengang", null, null);

        System.out.println(str); //输出“陈刚”

        /*

         * 使用了messages.properties

         */

        str = ctx.getMessage("chengang", null, new Locale(""));

        System.out.println(str);//输出“Giles

        /*

         * 使用了messages_zh_CN.properties

         */

        str = ctx.getMessage("chengang", null, new Locale("zh", "CN"));

        System.out.println(str);//输出“陈刚”

        /*

         * 使用了messages_zh_TW.properties

         */

        str = ctx.getMessage("chengang", null, new Locale("zh", "TW"));

        System.out.println(str);//输出“陳剛”

        /*

         * 使用了messages_zh_TW.properties,从这里可见资源文件的起名可以很随意,

         * 比如我们建立一个messages_123.properties,在传参数时候就可以这样:

         * new Locale("123"),一样也可以取出messages_123.properties中的值

         */

        str = ctx.getMessage("chengang", null, new Locale("zh_TW"));

        System.out.println(str);//输出“陳剛”

        /*

         * 当找不到相应的资源文件时,使用了messages_zh_CN.properties

         */

        str = ctx.getMessage("chengang", null, new Locale("abcd"));

        System.out.println(str);//输出“陈刚”

         /**

         * 不通过IoC注册,直接使用ResourceBundleMessageSource类的写法。

         */

        ResourceBundleMessageSource s = new ResourceBundleMessageSource();

        s.setBasename("cn.com.chengang.spring.messages");

        str = s.getMessage("chengang", null, null);

        System.out.println(str);//输出“陈刚”

    }

}

代码说明:

前面说过控制语言的方式有三种:从最低层的操作系统的Locale设定,到更上一层的JVMLocale设定,再到程序一级的Locale设定。我认为最佳的方法是在程序一级进行控制:定义一个统一的Locale静态变量,然后整个系统中只使用这一个变量,以后就可以通过界面操作设置此Locale变量的值,让用户来选择他所需的软件语言。而且我们也可以将此静态变量设成null值,来自动选择资源文件。

另外,Locale里也定义了一些常量,我们可以直接使用而不必去new一个Locale对象,如:“Locale.ENGLISH”。

 

3.4  再一个实例

这个实例演示了如何使用多个资源文件,以及如何使用字符串参数

1)在cn.com.chengang.spring包下再创建一个资源文件messagesOther_zh_CN.properties

chengang.info=\u9648\u521A\uFF0C\u7F51\u540D\uFF1A{0}\uFF0C\u82F1\u6587\u540D\uFF1A{1}\uFF0CBlog\uFF1A{2}

其中UNICODE字符串对应的中文是:“陈刚,网名:{0},英文名:{1}Blog{2}”,这个字符串一共有三个参数。

2)修改 bean.xml文件

因为basenames属性是一个数组,当然也就可以接收多个资源文件设定。具体修改如下面的红字部份

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

       <bean id="Chinese" class="cn.com.chengang.spring.Chinese"/>

       <bean id="American" class="cn.com.chengang.spring.American"/>

       <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

              <property name="basenames">

                     <list>

                            <value>cn.com.chengang.spring.messages</value>

                            <value>cn.com.chengang.spring.messagesOther</value>

                     </list>

              </property>

       </bean>

</beans>

 

3)修改MessageTest类,加入几行使用的代码

        String[] strArgs = new String[3];

        strArgs[0]="混北民工";

        strArgs[1]="Giles";

        strArgs[2]="http://blog.csdn.net/glchengang";

        str = ctx.getMessage("chengang.info", strArgs, null);

        System.out.println(str);

打印出来的结果就是:“陈刚,网名:混北民工,英文名:GilesBloghttp://blog.csdn.net/glchengang

 

3.5  国际化的实践建议

l           建议一个包对应一个资源文件。不要整个系统都使用一个资源文件来翻译,这样单个文档的体积就太大了,不利于维护;当然,也不必一个类对应一个资源文件,这样资源文件又太多了。

l           建议资源文件和其翻译类/包在同一目录下。不过,如果是要将软件打成一外JAR包或WAR包,建议把资源文件分离出来,这样可以修改资源文件,而不必再次打包。

l           建议字符串项的键值上加上其所在的类名。比如:上面的chengangchengang.info最好是取名成MessageTest.chengangMessageTest.chengang.info。这样查找使用此键值的类会方便很多。

 

 

参考文献

l           夏昕的<<Spring开发指南>>  http://www.xiaxin.net/Spring_Dev_Guide.rar 

分享到:
评论
4 楼 Ouvidia 2012-02-09  
受益匪浅啊  谢谢
3 楼 lostyue 2009-04-10  
还要谢谢楼主的好书~~~呵呵
2 楼 lostyue 2009-04-10  
请教下:
我看了api里对setBasename()的描述:
setBasename
public void setBasename(String basename)Set a single basename, following ResourceBundle conventions: essentially, a fully-qualified classpath location. If it doesn't contain a package qualifier (such as org.mypackage), it will be resolved from the classpath root.
Messages will normally be held in the "/lib" or "/classes" directory of a web application's WAR structure. They can also be held in jar files on the class path.

Note that ResourceBundle names are effectively classpath locations: As a consequence, the JDK's standard ResourceBundle treats dots as package separators. This means that "test.theme" is effectively equivalent to "test/theme", just like it is for programmatic java.util.ResourceBundle usage.

其中红色的部分似乎在源码中没有体现呢?
1 楼 xo_tobacoo 2009-02-23  
Thank you!

相关推荐

    spring messageSource结合ehcache demo

    spring messageSource功能结合ehcache实现提示语句从数据库读取demo,注:sql脚本都在代码中,如有问题及时沟通指点。

    Spring国际化

    Spring和其它的框架一样,也提供了国际化功能,它是通过MessageSource接口来实现的 ApplicationContext接口继承了MessageSource 。 MessageSource接口方法

    pebble-spring-translate:Pebble 模板引擎的翻译扩展,使用 Spring 消息接口

    为模板引擎提供t函数,它从 Spring MessageSource 检索消息。 {{ t('users.show.title') }} “懒惰”查找 函数支持,因此您可以在users/show.html查找users.show.title消息,例如: {{ t('.title') }} 没有模板...

    Spring-Reference_zh_CN(Spring中文参考手册)

    3.8.1. 利用MessageSource实现国际化 3.8.2. 事件 3.8.3. 底层资源的访问 3.8.4. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 4.2. ...

    Spring2.5的国际化配置

    Spring2.5的国际化配置 基于浏览器语言的国际化...&lt;bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"&gt; &lt;property name="basename" value="message-info" /&gt; ...

    Spring 2.0 开发参考手册

    3.8.1. 利用MessageSource实现国际化 3.8.2. 事件 3.8.3. 底层资源的访问 3.8.4. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 ...

    Spring.3.x企业应用开发实战(完整版).part2

    5.5.2 MessageSource 5.5.3 容器级的国际化信息资源 5.6 容器事件 5.6.1 Spring事件类结构 5.6.2 解构Spring事件体系的具体实现 5.6.3 一个实例 5.7 小结 第6章 Spring AOP基础 6.1 AOP概述 6.1.1 AOP到底是什么 ...

    Spring中文帮助文档

    3.8.2. 利用MessageSource实现国际化 3.8.3. 事件 3.8.4. 底层资源的访问 3.8.5. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.10. 以J2EE RAR文件的形式部署Spring ...

    spring chm文档

    3.8.1. 利用MessageSource实现国际化 3.8.2. 事件 3.8.3. 底层资源的访问 3.8.4. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 ...

    Spring API

    3.8.2. 利用MessageSource实现国际化 3.8.3. 事件 3.8.4. 底层资源的访问 3.8.5. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.10. 以J2EE RAR文件的形式部署Spring ...

    Spring3.x企业应用开发实战(完整版) part1

    5.5.2 MessageSource 5.5.3 容器级的国际化信息资源 5.6 容器事件 5.6.1 Spring事件类结构 5.6.2 解构Spring事件体系的具体实现 5.6.3 一个实例 5.7 小结 第6章 Spring AOP基础 6.1 AOP概述 6.1.1 AOP到底是什么 ...

    spring-mvc-params:Spring RequestHandler方法参数注入

    例子@Controllerpublic class AutowiredParamController {/** * Handles A requests * @param param an example url parameter * @param messageSource the MessageSource from the application context, * injected...

    SPRING API 2.0.CHM

    MessageSource MessageSourceAccessor MessageSourceAware MessageSourceResolvable MessageSourceResourceBundle MessageTag MetaDataAccessException MetadataAwareAspectInstanceFactory ...

    errors-spring-boot-starter:Spring Boot的优雅错误处理

    使用普通的旧MessageSource简单的错误消息插值。 可自定义的HTTP错误表示形式。 将参数从异常公开到错误消息。 同时支持传统和React堆。 可自定义的异常日志记录。 支持错误指纹识别。 入门 下载 下载或通过...

    解决Spring国际化文案占位符失效问题的方法

    本篇文章主要介绍了解决Spring国际化文案占位符失效问题的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    Grails开源框架 - 使用指南

    不用重新启动服务器就可以进行重新加载利用内置的Spring 容器实现依赖注入基于Spring的MessageSource核心概念,提供了对国际化(i18n)的支持基于Spring事务抽象概念,实现事务服务层  借助于功能强大的Groovy动态...

    使用Grails快速开发Web应用.rar

    不用重新启动服务器就可以进行重新加载利用内置的Spring 容器实现依赖注入基于Spring的MessageSource核心概念,提供了对国际化(i18n)的支持基于Spring事务抽象概念,实现事务服务层  借助于功能强大的Groovy动态...

    GRails 中文帮助文档(参考手册)和安装开发介绍帮助

    基于Spring的MessageSource核心概念,提供了对国际化(i18n)的支持 基于Spring事务抽象概念,实现事务服务层 借助于功能强大的Groovy动态语言和领域特定语言(Domain Specific Language,DSL),以上那些特性变得...

    grails-docs-2.0.3.zip

    基于Spring的MessageSource核心概念,提供了对国际化(i18n)的支持 基于Spring事务抽象概念,实现事务服务层 借助于功能强大的Groovy动态语言和领域特定语言(Domain Specific Language,DSL),以上那些特性变...

Global site tag (gtag.js) - Google Analytics