现在的位置: 首页 > 综合 > 正文

springMVC基于Session实现动态国际化

2013年03月04日 ⁄ 综合 ⁄ 共 1709字 ⁄ 字号 评论关闭

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

<bean id="messageSource"  class="org.springframework.context.support.ResourceBundleMessageSource">                    
  <property name="basenames">
   <list>
    <value>resources/message_info</value>
   </list>
  </property>                  
  <property name="useCodeAsDefaultMessage" value="true"/><!-- Set whether to use the message code as default message instead of throwing a NoSuchMessageException. Useful for development and debugging. -->           
 </bean>

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

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

 

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

<bean id="className"
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
  <property name="interceptors">
   <list>
    
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
   </list>

  </property>
 </bean>

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

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

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

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

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

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

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

原文地址:http://blog.sina.com.cn/s/blog_a1cc94c00100yndp.html

抱歉!评论已关闭.