Spring中的国际化的处理

在applicationContext.xml文件或是其他名字的spring配置文件中进行设置,

基本的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

	<!-- 配置国际化的操作,这是固定的方式 -->
	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<!-- 设置读取的配置文件message.properties -->
				<value>message</value>
			</list>
		</property>
	</bean>
</beans>


message.properties国际化配置文件的书写:
##其中{num}是占位符,从0开始记录
hello=welcom,{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}-{8}-{9}-{10}-{11}
now=new is\:{0}


测试类中的调用:
// 加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml");

// 国际化
// 查找到message.properties中的key为hello的进行返回,如果有占位符就采用数组的方式进行赋值
String hello = applicationContext.getMessage("hello", new Object[] {"spring", "1", "2", "3", "4", "5", "6", "7", "ba", "九", "十",
		
		"十一" }, Locale.getDefault());

System.out.println(hello);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值