`
Wind_ZhongGang
  • 浏览: 259921 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论
阅读更多

  Spring提供了一个接口MessageSource用于获取国际化信息。有这么几个类值得注意,用于在项目中启用国际化信息功能,一个抽象类AbstractMessageSource,继承该抽象类的有四个类,分别为ResourceBundleMessageSource,ReloadableResourceBundleMessageSource,StaticMessageSource,

SpringSecurityMessageSource。各个类的功能相当,只是用处不同,查看Spring的javadoc发现,StaticMessageSource主要用于测试环境,编写测试类,并不用于生产环境,而SpringSecurityMessageSource用于Spring security的国际化信息。

 

  因为ApplicationContext继承并实现了MessagSource接口,所以ApplicationContext自身提供了国际化信息功能,想要在项目中实现国际化信息功能常用可以有两种做法。

 

  在介绍这两种做法之前,需要事先定义好国际化文件,如图:

 

  internationalization files

 

  在这里,我是在测试环境中来测试Spring提供的国际化信息功能,其中文件名为testInfo的Resource bundle

中有三个国际化文件文件,分别为英语,日语,中文,这里的国际化文件命名有一定的规范,如果不符合规范,那么Spring是无法解析国际化文件的,不能知道你哪个文件是哪个语言的国际化文件。国际化文件命名规范遵从 ${filename}_${languagename}_${countryname},其中${}是需要替代的内容,下划线是必需的分隔符,所以如果你想要定义一个法语国际化文件应该是这样的 testInfo_fr_FR,至于language和countryname的取名请参见java.util.Locale类,里面有详细说明。

 

  一。使用ApplicationContext提供的国际化信息功能

 

  启用ApplicationContext的国际化功能需要在ApplicationContext配置文件进行这样的定义

 

 

<?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>testError</value>
                   <value>testMessage</value>
                   <value>testInfo</value>
               </list>
           </property>
    </bean>

    <!--<bean id="messageSource" class="org.springframework.security.core.SpringSecurityMessageSource">-->
        <!--<property name="parentMessageSource" ref="resourceBundleMessageSource"/>-->
    <!--</bean>-->

    <!--<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">-->
           <!--<property name="parentMessageSource"  ref="resourceBundleMessageSource"/>-->
    <!--</bean>-->

    <!--<bean id="messageSource" class="org.springframework.context.support.StaticMessageSource">-->
           <!--<property name="parentMessageSource" ref="resourceBundleMessageSource"/>-->
    <!--</bean>-->
</beans>

 

  注释部分可以暂时不管,这里定义了一个MessageSource的实现类ResourceBundleMessageSource用于提供国际化功能,这里的id命名记得一定要以messageSource命名,因为研究AbstractApplicationContext会发现里面定义了一个messageSource属性,提供的set方法就是setMessageSource,Spring在初始化时会将ApplicationContext配置文件中id为messageSource的bean注入到ApplicationContext类中去,这样我们就可以使用ApplicationContext提供的国际化功能了。以下是测试类:

 

 


package com.dream.message;

import junit.framework.TestCase;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Locale;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-9-10
 * Time: 上午10:08
 */
public class MessageSourceTest extends TestCase {

    public void testResourceBundleMessageSource() throws Exception {
        MessageSource messageSource = new ClassPathXmlApplicationContext("testApplicationContext.xml");
        String message1 = messageSource.getMessage("info.one", null, Locale.CHINESE);
        String message2 = messageSource.getMessage("info.one", null, Locale.JAPAN);
        String message3 = messageSource.getMessage("info.one", null, Locale.US);
        assertEquals("This is the first message which language is Chinese.", message1);
        assertEquals("This is the first message which language is Japanese.", message2);
        assertEquals("This is the first message which language is English.", message3);
    }
}

 

  二。使用自定义的国际化信息功能

 

 

  想实现自定义的国际化信息功能,只需要自定义一个类,向该类中注入messageSource即可,这样就可以使用该接口中提供的方法来获取国际经信息了,其实与第一种方式并无二异。

  • 大小: 18.4 KB
1
6
分享到:
评论

相关推荐

    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