`

利用MessageSource实现国际化[I18N]

    博客分类:
  • Java
阅读更多

3.8.1. 利用MessageSource实现国际化

ApplicationContext接口扩展了MessageSource接口,因而提供了消息处理的功能(i18n或者国际化)。与HierarchicalMessageSource一起使用,它还能够处理嵌套的消息,这些是Spring提供的处理消息的基本接口。让我们快速浏览一下它所定义的方法:

  • String getMessage(String code, Object[] args, String default, Locale loc):用来从MessageSource获取消息的基本方法。如果在指定的locale中没有找到消息,则使用默认的消息。args中的参数将使用标准类库中的MessageFormat来作消息中替换值。

  • String getMessage(String code, Object[] args, Locale loc):本质上和上一个方法相同,其区别在:没有指定默认值,如果没找到消息,会抛出一个NoSuchMessageException异常。

  • String getMessage(MessageSourceResolvable resolvable, Locale locale):上面方法中所使用的属性都封装到一个MessageSourceResolvable实现中,而本方法可以指定MessageSourceResolvable实现。

当一个ApplicationContext被加载时,它会自动在context中查找已定义为MessageSource类型的bean。此bean的名称须为messageSource。如果找到,那么所有对上述方法的调用将被委托给该bean。否则ApplicationContext会在其父类中查找是否含有同名的bean。如果有,就把它作为MessageSource。如果它最终没有找到任何的消息源,一个空的StaticMessageSource将会被实例化,使它能够接受上述方法的调用。

Spring目前提供了两个MessageSource的实现:ResourceBundleMessageSourceStaticMessageSource。它们都继承NestingMessageSource以便能够处理嵌套的消息。StaticMessageSource很少被使用,但能以编程的方式向消息源添加消息。ResourceBundleMessageSource会用得更多一些,为此提供了一下示例:

<beans>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>format</value>
<value>exceptions</value>
<value>windows</value>
</list>
</property>
</bean>
</beans>

这段配置假定在你的classpath中有三个资源文件(resource bundle),它们是formatexceptionswindows。通过ResourceBundle,使用JDK中解析消息的标准方式,来处理任何解析消息的请求。出于示例的目的,假定上面的两个资源文件的内容为…

# in 'format.properties'
message=Alligators rock!
# in 'exceptions.properties'
argument.required=The '{0}' argument is required.

下面是测试代码。因为ApplicationContext实现也都实现了MessageSource接口,所以能被转型为MessageSource接口

public static void main(String[] args) {
MessageSource resources = new ClassPathXmlApplicationContext("beans.xml");
String message = resources.getMessage("message", null, "Default", null);
System.out.println(message);
}

上述程序的输出结果将会是...

Alligators rock!

总而言之,我们在'beans.xml'的文件中(在classpath根目录下)定义了一个messageSource bean,通过它的basenames属性引用多个资源文件;而basenames属性值由list元素所指定的三个值传入,它们以文件的形式存在并被放置在classpath的根目录下(分别为format.propertiesexceptions.propertieswindows.properties)。

再分析个例子,这次我们将着眼于传递参数给查找的消息,这些参数将被转换为字符串并插入到已查找到的消息中的占位符(译注:资源文件中花括号里的数字即为占位符)。

<beans>
<!-- this MessageSource is being used in a web application -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="baseName" value="WEB-INF/test-messages"/>
</bean>
<!-- let's inject the above MessageSource into this POJO -->
<bean id="example" class="com.foo.Example">
<property name="messages" ref="messageSource"/>
</bean>
</beans>
public class Example {
private MessageSource messages;
public void setMessages(MessageSource messages) {
this.messages = messages;
}
public void execute() {
String message = this.messages.getMessage("argument.required",
new Object [] {"userDao"}, "Required", null);
System.out.println(message);
}
}

调用execute()方法的输出结果是...

The 'userDao' argument is required.

对于国际化(i18n),Spring中不同的MessageResource实现与JDK标准ResourceBundle中的locale解析规则一样。比如在上面例子中定义的messageSource bean,如果你想解析British (en-GB) locale的消息,那么需要创建format_en_GB.propertiesexceptions_en_GB.propertieswindows_en_GB.properties三个资源文件。

Locale解析通常由应用程序根据运行环境来指定。出于示例的目的,我们对将要处理的(British)消息手工指定locale参数值。

# in 'exceptions_en_GB.properties'
argument.required=Ebagum lad, the '{0}' argument is required, I say, required.
public static void main(final String[] args) {
MessageSource resources = new ClassPathXmlApplicationContext("beans.xml");
String message = resources.getMessage("argument.required",
new Object [] {"userDao"}, "Required", Locale.UK);
System.out.println(message);
}

上述程序运行时的输出结果是...

Ebagum lad, the 'userDao' argument is required, I say, required.

MessageSourceAware接口还能用于获取任何已定义的MessageSource引用。任何实现了MessageSourceAware接口的bean将在创建和配置的时候与MessageSource一同被注入。

分享到:
评论

相关推荐

    springMVC实现国际化

    看了好多资料后整理的springMVC国际化,内容为亲测可用的eclipse项目,直接import即可。 页面点击后可以切换中文和英文两种国际化信息,可以再追加其他语言。

    Spring国际化

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

    Spring2.5的国际化配置

    只要有了这个配置,然后配置JSP渲染器为JSTL支持的,那么在你的JSP文件中使用fmt标记就可以实现客户浏览器语言国际化了。 如:&lt;fmt:message key="info.login.title" /&gt; 其中的info.login.title和你的资源文件对应

    spring messageSource结合ehcache demo

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

    Grails开源框架 - 使用指南

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

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

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

    base-api-server-v1.0.1.zip

    基于spring boot 2.x 搭建基础版 api服务框架 技术架构: 开发环境: ·语言 Java8 ·IDE(JAVA): IDEA / Eclipse安装lombok插件 ·依赖管理:Maven ...5、MessageSource/i18n 消息国际化 6、Ureport2 报表功能

    Grails实战教程-整合版

    现如今,基于Java的网站开发明显过于复杂,远超实际工作需要。在Java领域中,大部分的网站开发框架也没有严格的...使用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),以上那些特性变...

    java开发的餐饮管理系统源码-cypher-messagesource:图数据库的基于密码的MessageSource实现

    这意味着确保您的软件国际化和本地化非常重要。 国际化和本地化 是开发产品的过程,以便可以轻松地针对语言和文化进行本地化。 是调整产品以使其在特定文化或语言市场中可用的过程。 这实际上意味着什么? 这意味着...

    Grails 教程

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

    java中的Grails开源框架 - 使用指南(chm)

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

    grails-开源框架使用指南

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

    Grails1.1中文文档

    基于 Spring 的 MessageSource 核心概念的国际化 (i18n) 支持 基于 Spring 的抽象事务概念的事务服务层 所有这些都非常易于使用,这得益于 Groovy 语言的强大以及 Domain Specific Languages (DSLs) 的广泛使用。 ...

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

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

    spring-proj-template:如何配置Spring项目的参考-Maven,Spring,MVC,Hibernate,EJB,DI,国际化

    基于数据库MessageSource的国际化 依赖注入 EJB启动Bean 没有测试,可耻的是我:) 注意:此示例仅包含基于xml的配置。 是相同项目模板的链接,但具有基于注释的配置。 要运行此应用程序,请安装Jboss AS 7.1.1,并...

    spring-proj-template-no-xml:如何通过注解配置 Spring 项目的参考 - maven、spring、mvc、hibernate、ejb、di、国际化

    Spring 项目模板(基于注解) 此项目模板与完全相同,但使用注释代替 XML ...基于数据库MessageSource的国际化 依赖注入 EJB 启动 Bean 要运行此应用程序,请安装 Jboss AS 7.1.1 并在必要时修改 deploy-script.bat。

    JavaTranslator:用于 Java 程序国际化的实用程序-开源

    一个有用的应用程序来帮助 Java 应用程序翻译。

    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. 简介 ...

Global site tag (gtag.js) - Google Analytics