《Pro Spring》学习笔记之ApplicationContext国际化

本文介绍Spring框架如何通过MessageSource接口提供强大的国际化支持。开发者可以通过配置ApplicationContext或直接使用MessageSource的实现来轻松实现多语言应用。文章提供了示例代码及配置文件说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  

Spring确实表现优异的一个方面是对国际化的支持,借助MessageSource接口,你的应用程序能够访问各种不同语言存储的String资源和被调消息

尽管你无须为了使用MessageSource而去用ApplicationContext,实际上ApplicationContext已经继承了MessageSource,并针对载入消息及令消息在你的环境里可用可供了专门支持

除了ApplicationContext,spring 还提供了三个MessageSource实现:

ResourceBundleMessageSource                使用java ResourceBundle
ReloadableResourceMessageSouce         可以定期中心载入资源文件
StaticMessageSource

利用ApplicationContext对MessageSource的支持,你必须在配置文件中顶一个MessageSource类型,名称为messageSouce的bean,并配置beannames或者beanname属性

MessageSoureTest.java

 

package ch5.applicationContext.MessageResource;

import java.io.File;
import java.util.Locale;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;

public class MessageResourceTest {
  
public static void main(String args[]){
      String realpath
="";
       
       
//加载配置项
       realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/applicationContext/MessageResource"+File.separator+"applicationContext.xml";

      ApplicationContext applicationContext
=new FileSystemXmlApplicationContext("classpath:ch5/applicationContext/MessageResource/applicationContext.xml");
       System.out.println(applicationContext.getMessage(
"msg"null,Locale.ENGLISH));
       System.out.println(applicationContext.getMessage(
"msg"null,Locale.US));
       System.out.println(applicationContext.getMessage(
"msg"null,Locale.UK));
       System.out.println(applicationContext.getMessage(
"msg"null,Locale.CHINA));
       System.out.println(applicationContext.getMessage(
"msg"null,Locale.CHINESE));
       
//带参数的例子
       System.out.println(applicationContext.getMessage("msg"new Object[]{"gao","xiang"},Locale.CHINESE));
    
  }

}

 

配置文件:

 

<?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-2.0.xsd">


<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  
<property name="basenames">
  
<list>
     
<value>ch5.applicationContext.MessageResource.labels</value>
  
</list>

  
</property>
</bean>


</beans>

 

资源文件5个:

labels_en_GB.properties:msg = british english (en_GB)
labels_en.properties:msg = english (en)
labels_en_US.properties:msg = american(us)
labels_zh_CN.properties:msg = 中国(zn_CN)
labels_zh.properties:msg = 中国(zh) {0},{1}

 

结果:

english (en)
american (US)
british english (en_GB)
中国(zn_CN)
中国(zh) {0},{1}
中国(zh) gao,xiang

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值