spring的國際化:
ApplicationContext接口因爲繼承了MessageSource接口,因此具備國際化的功能。
spring的國際化是建立在java程序國際化的基礎上,都是將程序中需要國際化的信息寫入資源文件,而代碼中僅僅使用各資源文件的key。
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message</value>
</list>
</property>
</bean>
2、資源文件message.properties
hello=歡迎你,{0}
hello=welcome,{0}
3、該資源文件包含了非西歐文字,所有使用native2ascii使該資源文件國際化,命令如下(載cmd中切換到資源文件所在的目錄下):
native2ascii message.properties message_en_US.properties
運行該命令後,會生成message_zh_CN.properties,該文件將作爲簡體中文實際的國際化資源文件,此時,可自適應簡體中文和英文的語言環境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、運行結果
歡迎你,楠楠