spring國際化

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、運行結果
歡迎你,楠楠


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