1.Java本身框架国际化
(1)本地化相关的类
Locale
NumberFormat
DateFormat
MessageFormat
(2)使用ResourceBoundle
国际化资源命名规范:
<资源名>_<语言代码>_<国家/地区代码>.properties
如:
resource.properties
resource_zh_CN.properties
resource_en_US.properties
使用例子
package com.baobaotao.i18n; import java.text.DateFormat; import java.text.MessageFormat; import java.text.NumberFormat; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import java.util.ResourceBundle; public class LocaleSample { public static void numberFormat() { Locale locale = new Locale("zh", "CN"); NumberFormat currFmt = NumberFormat.getCurrencyInstance(locale); double amt = 123456.78; System.out.println(currFmt.format(amt)); } public static void dateFormat() { Locale locale = new Locale("en", "US"); Date date = new Date(); DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); System.out.println(df.format(date)); } public static void messageFormat1() { Object[] params = {"John", new GregorianCalendar().getTime(),1.0E3}; String pattern1 = "{0},你好!你于{1}在工商银行存入{2} 元。"; String pattern2 = "At {1,time,short} On{1,date,long},{0} paid {2,number,currency}."; String msg1 = MessageFormat.format(pattern1,params); MessageFormat mf = new MessageFormat(pattern2,Locale.US); String msg2 = mf.format(params); System.out.println(msg1); System.out.println(msg2); } public static void resourceBoundle(){ ResourceBundle rb1 = ResourceBundle.getBundle("com/baobaotao/i18n/resource",Locale.US); ResourceBundle rb2 = ResourceBundle.getBundle("com/baobaotao/i18n/resource",Locale.CANADA); System.out.println("us:"+rb1.getString("greeting.common")); System.out.println("cn:"+rb2.getString("greeting.common")); } public static void resourceBoundleFmt(){ ResourceBundle rb1 = ResourceBundle.getBundle("com/baobaotao/i18n/fmt_resource",Locale.US); ResourceBundle rb2 = ResourceBundle.getBundle("com/baobaotao/i18n/fmt_resource",Locale.CHINA); Object[] params = {"John", new GregorianCalendar().getTime()}; String str1 = new MessageFormat(rb1.getString("greeting.common"),Locale.US).format(params); String str2 =new MessageFormat(rb2.getString("greeting.morning"),Locale.CHINA).format(params); String str3 =new MessageFormat(rb2.getString("greeting.afternoon"),Locale.CHINA).format(params); System.out.println(str1); System.out.println(str2); System.out.println(str3); } public static void main(String[] args) { numberFormat(); dateFormat(); messageFormat1(); resourceBoundle(); resourceBoundleFmt(); } }
resource.properties
greeting.common=How are you! greeting.morning = Good morning! greeting.afternoon =Good Afternoon\!
fmt_resource.properties
greeting.common=How are you!{0},today is {1} greeting.morning = Good morning!{0},now is {1,time,short} greeting.afternoon =Good Afternoon\!{0} now is {1,date,long}
2.Spring国际化
相关的接口及类:
(1)接口:MessageSource
(2)类:ResourceBundleMessageSource
通过beanName指定一个资源名
(3)类:ReloadableResourceBundleMessageSource
可以定时刷新资源文件
(4)容器级的国际化
Application实现了MessageSource接口,要配置文件中的Bean的名称必须为"messageSource".
Spring xml配置 bean.xml
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="myResource1" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>com/baobaotao/i18n/fmt_resource</value> </list> </property> </bean> <bean id="myResource2" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames"> <list> <value>com/baobaotao/i18n/fmt_resource</value> </list> </property> <property name="cacheSeconds" value="2"/> </bean> <bean id="messageSource12" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>com/baobaotao/i18n/fmt_resource</value> </list> </property> </bean> <!-- 容器级国际化,名称只能为messageSource --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>com/baobaotao/i18n/fmt_resource</value> </list> </property> </bean> </beans>
Java code:
package com.baobaotao.i18n; import java.text.MessageFormat; import java.util.GregorianCalendar; import java.util.Locale; import org.springframework.context.ApplicationContext; import org.springframework.context.MessageSource; import org.springframework.context.support.ClassPathXmlApplicationContext; public class I18nGreeting { private static void rsrBdlMessageResource(){ String[] configs = {"com/baobaotao/i18n/beans.xml"}; ApplicationContext ctx = new ClassPathXmlApplicationContext(configs); MessageSource ms = (MessageSource)ctx.getBean("myResource1"); Object[] params = {"John", new GregorianCalendar().getTime()}; String str1 = ms.getMessage("greeting.common",params,Locale.US); String str2 = ms.getMessage("greeting.morning",params,Locale.CHINA); String str3 = ms.getMessage("greeting.afternoon",params,Locale.CHINA); System.out.println(str1); System.out.println(str2); System.out.println(str3); } private static void rrsrBdlMessageResource() throws Exception{ String[] configs = {"com/baobaotao/i18n/beans.xml"}; ApplicationContext ctx = new ClassPathXmlApplicationContext(configs); MessageSource ms = (MessageSource)ctx.getBean("myResource2"); Object[] params = {"John", new GregorianCalendar().getTime()}; for (int i = 0; i < 2; i++) { String str1 = ms.getMessage("greeting.common",params,Locale.US); System.out.println(str1); Thread.currentThread().sleep(20000); } } private static void ctxMessageResource() throws Exception{ String[] configs = {"com/baobaotao/i18n/beans.xml"}; ApplicationContext ctx = new ClassPathXmlApplicationContext(configs); Object[] params = {"John", new GregorianCalendar().getTime()}; String str1 = ctx.getMessage("greeting.common",params,Locale.US); String str2 = ctx.getMessage("greeting.morning",params,Locale.CHINA); System.out.println(str1); System.out.println(str2); } public static void main(String[] args) throws Exception{ // rsrBdlMessageResource(); // rrsrBdlMessageResource(); ctxMessageResource(); } }
相关推荐
spring国际化实例 下下来就能用 捐点分吧(就1分) 谢谢
spring 国际化的实例,想学习spring的同学,可以参考看看。
基于struts2-hibernate-spring的Java Web系统国际化设计与实现.pdf
spring2国际化的demo 用的是eclipse+myeclipse6.0GA 很简单 但是感觉不错 学习之后留着备用 里面加入用到了spring的泛型工厂 可以不用强制转换了
自己整理的sping boot国际化,切换中英文页面,切换时,将当前语言类型保存到session中,保证跳转页面时,语言类型沿用上个页面的语言类型
它介绍了JAVAspring的国际化的概念、原理和作用,以及如何使用JAVAspring的国际化来实现多语言支持,包括资源文件、区域设置、消息解析、日期格式、货币格式等内容,以及一些配置文件和注解的用法。
Spring国际化Demo
spring boot+JPA+easyUI 实现基于浏览器语言的国际化配置
Spring和其它的框架一样,也提供了国际化功能,它是通过MessageSource接口来实现的 ApplicationContext接口继承了MessageSource 。 MessageSource接口方法
java国际化验证
用springmvc做的一个国际化小例子,包含如何在页面和java代码中获取国际化资源
基于struts2-hibernate-spring的Java Web系统国际化设计与实现
主要介绍了Java Spring项目国际化详细方法与实例,需要的朋友可以参考下
第3篇为高级篇,内容包括JSTL标签库的使用、如何自定义标签、资源国际化、数据库概述、JDBC详解、XML概述等;第4篇为Struts框架篇,内容不仅包括Struts 1.x的内容,也包括Struts 2.x中的内容;第5篇为Hibernate框架...
- Spring 业务应用,包括 AOP、过滤器、定时任务、上传文件、国际化 等。 - spring-dubbo - Spring 集成 Dubbo - spring-mq - Spring 集成 消息队列,包括 kafka、rabbitmq、activemq、rocketmq、redismq -...
背景Spring MVC 的国际化是建立在Java国际化的基础之上的,其一样也是通过提供不同国家语言环境的消息资源,然后通过 Resource Bundle 加
SpringJDBCStruts实现产品列表;增加产品(上传图片),java国际化(拦截器)
template-thymeleaf|[thymeleaf实现应用国际化示例](https://github.com/smltq/spring-boot-demo/blob/master/template-thymeleaf) mq-redis|[redis之mq实现,发布订阅模式]...
2. 内容包括 Spring ioc, aop, 切面,生命周期,拓展点,和SpringBoot, SpringCloud的区别于联系,Spring 对象作用域 ,Spring各个context的区别,Spring的国际化,Spring事务实效的种类原因等等内容,建议详细阅读...
包含国际化、标签、Theme、视图展现的FreeMarker、JasperReports、Tiles、Velocity、XSLT相关类。当然,如果你的应用使用了独立的MVC框架,则无需这个JAR文件里的任何类。 19. spring-webmvc-portlet-4.1.1.RELEASE....