spring国际化

1 篇文章 0 订阅

spring的国际化:

ApplicationContext接口因为继承了MessageSource接口,因此具备国际化的功能。

spring的国际化是建立在java程序国际化的基础上,都是将程序中需要国际化的信息写入资源文件,而代码中仅仅使用各资源文件的key。


1、bean.xml
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  	<property name="basenames">
  		<list>
  			<value>message</value>
  		</list>
  	</property>
  </bean>

2、资源文件message.properties
hello=欢迎你,{0}
和message_en_US.properties
hello=welcome,{0}

3、该资源文件包含了非西欧文字,所有使用native2ascii使该资源文件国际化,命令如下(载cmd中切换到资源文件所在的目录下):
native2ascii message.properties message_en_US.properties
运行该命令后,会生成message_zh_CN.properties,该文件将作为简体中文实际的国际化资源文件,此时,可自适应简体中文和英文的语言环境

4、在main方法中测试
public static void main(String[] args) {
		ApplicationContext ac = new FileSystemXmlApplicationContext("config/bean_*.xml");
		String[] a = {"楠楠"};
		String bendi = ac.getMessage("hello", a, Locale.getDefault());
		System.out.println(bendi);
		
	}

5、运行结果
欢迎你,楠楠


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值