Spring支持国际化
来源:互联网 发布:碎屏幕手机壁纸软件 编辑:程序博客网 时间:2024/05/16 01:45
通过使用MessageSource接口,应用程序可以访问Spring资源,调用信息,并保存为不同的语言种类,对于每一种想支持的语言需要维护一个跟其他语言中消息一致的消息列表.
要使用ApplicationContext为MessageSource提供的支持,必须在配置中定义一个MessageSource类型的Bean并取名为messageSource.
MessageSource接口定义的三个国际化的方法:
1)getMessage(String,Object[],Locale)
Locale的作用是告诉ResourceBundleMessageSource当前正在查看哪个配置文件
2)getMessage(String , Object[] , String Locale)
与第一个使用方式一样,第二个参数允许在应用键的消息不可用时传入一个默认值
3)getMessage(messageSourceResolvable , Locale)
实现了Ness阿哥SOurceResolvable接口的对象,可以用来替代一组键值和一组参数
在Spring配置文件中需要做如下配置:
<bean id="messageSource" class="org.springframework,context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message</value>
</list>
</property>
</bean>
message是基础名称,查找特定的Locale的消息值时,会查找由基础名称和Locale名组合而成的属性文件,例如在查找一个en_US Locale的消息时,会去查找一个名为message_en_US.properties的文件,如果不存在,会查找message_en.properties 的属性文件,若也不存在,就会加载名为message.properties的文件.
创建两个属性文件,message_en_US.properties 和 message_zh_CN.properties
message_en_US.properties 文件写入如下内容
hello = welcome,{0}now = now is :{0}message_zh_CN.properties文件写入如下内容(eclipse会自动将不是英文的文字转换为二进制)
hello = 你好,{0}now = 现在时间是 : {0}
创建测试类,进行测试
public static void main(String[] args){ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); String[] a = {"用户"}; String hello = ctx.getMessage("hello",a, Locale.getDefault()); Object[] b = { new Date()}; String now = ctx.getMessage("now",b,Locale.getDefault()); System.out.println("\n本地化信息"); System.out.println(hello); System.out.println(now); hello = ctx.getMessage("hello",a,Locale.US);//参数中的hello对应属性文件中的属性名 now = ctx.getMessage("now",b,Locale.US); System.out.println("\nEnglish message"); System.out.println(hello); System.out.println(now); hello = ctx.getMessage("hello",a,Locale.CHINA); now = ctx.getMessage("now",b,Locale.CHINA); System.out.println("\n中文信息"); System.out.println(hello); System.out.println(now);
- Spring 国际化支持
- spring 国际化支持
- spring国际化支持
- Spring支持国际化
- Spring国际化支持
- Spring Boot国际化支持
- spring(11)------spring国际化支持
- Spring Applicationcontext的国际化支持
- Spring Applicationcontext的国际化支持
- Spring对国际化的支持
- Spring学习之国际化支持
- Spring ApplicationContext的国际化支持
- Spring 国际化的支持:Spring MVC Velocity
- Spring -- 国际化的支持(MessageResource用法)
- Spring framework(7):国际化支持 i18n
- Spring MVC完成指南(三)国际化的支持
- Spring使用拦截器支持国际化(转)
- Spring 国际化支持 消息传递 以及 AOP 面向方面编程
- 牛客网---2016---蘑菇街聊天
- 网络层数据包处理流程
- css之定位
- 基于vue的移动web app页面缓存解决方案
- 爬虫---requests库
- Spring支持国际化
- Activiti工作流引擎数据库表结构
- vue-router使用总结
- string 字符串连接
- 最短路uva11374
- GC垃圾回收
- SpringMVC上传图片并预览
- caffe中 对caffemodel进行结构修改并保存
- Solr集群