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、运行结果
欢迎你,楠楠