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);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章