トップ 一覧 ping 検索 ヘルプ RSS ログイン

Spring MVC 各国語対応の変更点

  • 追加された行はこのように表示されます。
  • 削除された行はこのように表示されます。
!!!Spring MVC 各国語対応
[Spring][Spring MVC]
*http://d.hatena.ne.jp/GreenTea2010/20110529/1306692169
*https://jira.springsource.org/browse/SPR-6555
*http://viralpatel.net/blogs/2010/07/spring-3-mvc-internationalization-i18n-localization-tutorial-example.html
*http://static.springsource.org/spring/docs/1.1.5/taglib/tag/MessageTag.html
!!Eclipse での手順

!プロパティファイルを作成
*messages_en.properties
*messages_ja.properties
""messages_ja.properties は native2ascii をかける
::WEB-INF/i18n/messages_en.properties
 label.firstname=First Name
::WEB-INF/i18n/messages_ja.properties
 label.firstname=\u540d\u524d
!Spring設定ファイルにBeanの設定
 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
     <mvc:annotation-driven/> 
     <mvc:interceptors> 
         <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
             <property name="paramName" value="lang" />
         </bean>
     </mvc:interceptors>    
     
     <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
         <property name="basename" value="WEB-INF/i18n/messages"/>
         <property name="defaultEncoding" value="UTF-8" />        
     </bean>
     
     <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
         <property name="cookieName" value="locale" />
     </bean>
 </beans>
!JSPでtaglibの宣言
 <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
!JSPで利用
::出力
 <spring:message code="label.firstname"/>
::結果を変数に入れる
 <spring:message code="label.title.index" var="title" scope="page"/>
!タグライブラリが見つからないエラーの場合
*org.springframework.web.servlet の /META-INF/spring.tld を /WEB-INF へコピー
*web.xml に taglib 情報を与える
 <web-app>
      :
     <jsp-config>
         <taglib>
             <taglib-uri>http://www.springframework.org/tags</taglib-uri>
             <taglib-location>/WEB-INF/spring.tld</taglib-location>
         </taglib>
     </jsp-config>