springMVC基于Session实现动态国际化

来源:互联网 发布:开源商城系统 php 编辑:程序博客网 时间:2024/06/02 03:46

springMVC基于Session实现动态国际化

1.在spring配置文件中配置资源文件properties的位置及公共名,下列配置指定的properties文件处于src目录下的resources文件夹中,名字为message_info_*.properties。

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">                    
  <propertyname="basenames">
   <list>
    <value>resources/message_info</value>
   </list>
  </property>                  
  <propertyname="useCodeAsDefaultMessage"value="true"/><!-- Set whether to usethe message code as default message instead of throwing aNoSuchMessageException. Useful for development anddebugging. -->           
 </bean>

2.在spring配置文件中配置基于session的处理,将提交上来的locale参数进行处理,下列代码默认加载的语言是中文简体。

<bean id="localeResolver"class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
   <propertyname="defaultLocale"value="zh_CN"></property>
 </bean>

 

3.在spring配置文件中的controller内配置相应的拦截器。

<beanid="className"
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
  <propertyname="interceptors">
   <list>
    
    <beanclass="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
   </list>

  </property>
 </bean>

4.相应的properties文件内写入对应的语言,配置文件的语言信息以keyvalue的形式进行存储。

5.利用jstl的fmt标签库进行相应数据的国际化。

  1)导入相应的fmt标签库<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

  2)需要国际化处写入<fmt:messagekey="title"></fmt:message>标签,此处将显示相应properties文件中名为title的信息。

  3)页面上写成三个连接用于控制国际化的转换

   <ahref="/CloudPortal/staff/goindex.do?locale=zh_CN">Chinese</a>//message_info_zh_CN.properties
   <ahref="/CloudPortal/staff/goindex.do?locale=en_US">English</a>//message_info_en_US.properties
   <ahref="/CloudPortal/staff/goindex.do?locale=zh_TW">Chinese(TW)</a>//message_info_zh_TW.properties

 locale内部固定的参数用于判断读取请求的配置文件。

 


-------------------------------------------------------------分割线--------------2014-11-11 by zhang------------------------------------------------------------------------------------------------------

springmvc如何实现国际化

1.springmvc实现国际化有多种方式(自行百度)。

几种方式无非就是配置国际化方式和读取国际化资源文件从而实现国际化,下面本文介绍基于session的国际化配置,感觉配置比较方便快捷一些。

(1).首先配置配置国际化在spring-servlet.xml,即springmvc的配置文件中(xxx-servlet.xml)。

<!-- springmvc 国际化配置 (基于Session的国际化配置 方式)--> <bean id="localeResolver"          class="org.springframework.web.servlet.i18n.SessionLocaleResolver">   </bean> <bean id="messageSource"class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><property name="fallbackToSystemLocale"><value>false</value></property><property name="basename" value="/WEB-INF/i18n/resources/messages" /></bean>

(2)在WEB-INF文件夹下按照
/WEB-INF/i18n/resources/messages
配置新建文件夹,在文件夹里面新建配置文件

messages_zh_CN.properties(中文国际化资源文件),messages.properties(英文国际化资源文件)国际化资源文件。

到此配置完毕。

注:springmvc的国际化必须要经过控制器Controller才能生效。


2.如何在jsp页面读取国际化

(1)引入<spring:message/>标签如:<spring:message key=''user.manage.role/>

或者

(2)引入<fmt:message/>标签例如<fmt:message key='user.manage.role'/>,其中user.manage,role在messages_zh_CN.properties中配置为:user.manage.role=\u89D2\u8272,在messages.properties中配置为user.manage.role=Role,即可。



0 0
原创粉丝点击