ApplicationContext接口继承BeanFactory都是spring的容器。
ApplicationContext接口实现了MessageSource接口,因此具有国际化功能。下面是messageSource接口定义的两个用于国际化的两个方法:
String getMessage(String code,Object[] args,Locale loc)
String getMessage(String code,Object[] args,String default,Locale loc);
一、在spring配置文件中加入如下配置:
<
bean
id
=
"messageSource"
class
=
"org.springframework.context.support.ResourceBundleMessageSource"
>
<
property
name
=
"basenames"
>
<list>
<
value
>message</
value
>
</list>
</
property
>
</
bean
>
二、给出两份资源文件
1)英语资源文件message_en_US.properties
hello=welcom,{0}
now=now is :{0}
2)简体中文资源文件message.properties
hello=欢迎你,{0}
now=现在时间是 :{0}
三、使用native2ascii工具将这份资源文件国际化,命令如下:
native2ascii message.properties message_zh_CN.properties
执行上面命令后,生成message_zh_CN.properties文件,该文件是中文国际化资源文件。
四、主程序
public void main(String args[]){
ApplicationContext con = new
ClassPathXmlApplicationContext("beans.xml");
String hello = con.getMessage("hello",new String[]{"随悟空"},
Locale.getDefault(Locale.Category.Format");
String now= con.getMessage("now",new String[]{new Date()},
Locale.getDefault(Locale.Category.Format");
System.out.println(hello);
System.out.println(now);
}
程序输出结果会根据环境的不用而输出结果;
在中文环境下结果是:
欢迎你,随悟空
现在时间是,14-5-10 下午5:43
在英文环境下结果是:
welcom,随悟空
now is :14/5/10 5:43 PM
相关推荐
spring国际化简单案例
spring国际化实例 下下来就能用 捐点分吧(就1分) 谢谢
spring 国际化的实例,想学习spring的同学,可以参考看看。
Spring国际化Demo
采用spring技术 实现项目中英文切换 国际化
Spring国际化Demo.zip
封装Spring实现国际化 非常具有实战价值
spring显示国际化信息!!!!这是一个完整的范例!!!!
Spring和其它的框架一样,也提供了国际化功能,它是通过MessageSource接口来实现的 ApplicationContext接口继承了MessageSource 。 MessageSource接口方法
Spring实现国际化的一个小例子
NULL 博文链接:https://ganliang13.iteye.com/blog/1204319
NULL 博文链接:https://gaojiewyh.iteye.com/blog/1756655
Spring2.5的国际化配置 基于浏览器语言的国际化配置 使用Spring的MVC,并且配置中有配置Resource文件 <!-- 资源文件绑定器 --> <bean id="messageSource" class="org.springframework.context.support....
本篇文章主要介绍了解决Spring国际化文案占位符失效问题的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring MVC 国际化的配置方法及使用,希望能对大家的学习、工作有帮助。
spring2国际化的demo 用的是eclipse+myeclipse6.0GA 很简单 但是感觉不错 学习之后留着备用 里面加入用到了spring的泛型工厂 可以不用强制转换了
NULL 博文链接:https://20142014.iteye.com/blog/1999130
关于spring国际化的详细例子,很经典,一看便会!
SpringMVC入门很简单之国际化,具体参考博文:http://www.cnblogs.com/liukemng/tag/SpringMVC/