| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「Spring MVC 各国語対応」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
(ページの作成:「==Spring MVC 各国語対応== [Spring][Spring MVC] *http://d.hatena.ne.jp/GreenTea2010/20110529/1306692169 *https://jira.springsource.org/browse/SPR-6555 *http://vir…」)
 
 
(同じ利用者による、間の3版が非表示)
1行目: 1行目:
==Spring MVC 各国語対応==
+
==[[Spring MVC 各国語対応]]==
[Spring][Spring MVC]
+
[[Spring]] | [[Spring MVC]] |
 
*http://d.hatena.ne.jp/GreenTea2010/20110529/1306692169
 
*http://d.hatena.ne.jp/GreenTea2010/20110529/1306692169
*https://jira.springsource.org/browse/SPR-6555
+
*https://jira.springsource.org/browse/SP[[R]]-6555
*http://viralpatel.net/blogs/2010/07/spring-3-mvc-internationalization-i18n-localization-tutorial-example.html
+
*http://[[vi]]ralpatel.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
 
*http://static.springsource.org/spring/docs/1.1.5/taglib/tag/MessageTag.html
===Eclipse での手順===
+
===[[Eclipse]] での手順===
  
 
====プロパティファイルを作成====
 
====プロパティファイルを作成====
 
*messages_en.properties
 
*messages_en.properties
 
*messages_ja.properties
 
*messages_ja.properties
<blockquote>messages_ja.properties は native2ascii をかける</blockquote>
+
&lt;blockquote&gt;messages_ja.properties は native2ascii をかける&lt;/blockquote&gt;
 
=====WEB-INF/i18n/messages_en.properties=====
 
=====WEB-INF/i18n/messages_en.properties=====
 
  label.firstname=First Name
 
  label.firstname=First Name
 
=====WEB-INF/i18n/messages_ja.properties=====
 
=====WEB-INF/i18n/messages_ja.properties=====
 
  label.firstname=\u540d\u524d
 
  label.firstname=\u540d\u524d
====Spring設定ファイルにBeanの設定====
+
====[[Spring]]設定ファイルにBeanの設定====
  <?xml version="1.0" encoding="UTF-8"?>
+
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  <beans xmlns="http://www.springframework.org/schema/beans"
+
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
     xmlns:xsi="http://www.w3.org/2001/[[XML]]Schema-instance"
 
     xmlns:mvc="http://www.springframework.org/schema/mvc"
 
     xmlns:mvc="http://www.springframework.org/schema/mvc"
 
     xmlns:context="http://www.springframework.org/schema/context"
 
     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
 
     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/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">
+
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
 
   
 
   
     <mvc:annotation-driven/>
+
     &lt;mvc:annotation-driven/&gt;
     <mvc:interceptors>
+
     &lt;mvc:interceptors&gt;
         <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
+
         &lt;bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt;
             <property name="paramName" value="lang" />
+
             &lt;property name="paramName" value="lang" /&gt;
         </bean>
+
         &lt;/bean&gt;
     </mvc:interceptors>    
+
     &lt;/mvc:interceptors&gt;    
 
      
 
      
     <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
+
     &lt;bean id="messageSource" class="org.springframework.context.support.[[R]]eloadable[[R]]esourceBundleMessageSource"&gt;
         <property name="basename" value="WEB-INF/i18n/messages"/>
+
         &lt;property name="basename" value="WEB-INF/i18n/messages"/&gt;
         <property name="defaultEncoding" value="UTF-8" />        
+
         &lt;property name="defaultEncoding" value="UTF-8" /&gt;        
     </bean>
+
     &lt;/bean&gt;
 
      
 
      
     <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
+
     &lt;bean id="localeResolver" class="org.springframework.web.servlet.i18n.[[Cookie]]LocaleResolver"&gt;
         <property name="cookieName" value="locale" />
+
         &lt;property name="cookieName" value="locale" /&gt;
     </bean>
+
     &lt;/bean&gt;
  </beans>
+
  &lt;/beans&gt;
====JSPでtaglibの宣言====
+
====[[JSP]]でtaglibの宣言====
  <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
+
  &lt;%@taglib uri="http://www.springframework.org/tags" prefix="spring"%&gt;
====JSPで利用====
+
====[[JSP]]で利用====
 
=====出力=====
 
=====出力=====
  <spring:message code="label.firstname"/>
+
  &lt;spring:message code="label.firstname"/&gt;
 
=====結果を変数に入れる=====
 
=====結果を変数に入れる=====
  <spring:message code="label.title.index" var="title" scope="page"/>
+
  &lt;spring:message code="label.title.index" var="title" scope="page"/&gt;
 
====タグライブラリが見つからないエラーの場合====
 
====タグライブラリが見つからないエラーの場合====
 
*org.springframework.web.servlet の /META-INF/spring.tld を /WEB-INF へコピー
 
*org.springframework.web.servlet の /META-INF/spring.tld を /WEB-INF へコピー
 
*web.xml に taglib 情報を与える
 
*web.xml に taglib 情報を与える
  <web-app>
+
  &lt;web-app&gt;
 
       :
 
       :
     <jsp-config>
+
     &lt;jsp-config&gt;
         <taglib>
+
         &lt;taglib&gt;
             <taglib-uri>http://www.springframework.org/tags</taglib-uri>
+
             &lt;taglib-uri&gt;http://www.springframework.org/tags&lt;/taglib-uri&gt;
             <taglib-location>/WEB-INF/spring.tld</taglib-location>
+
             &lt;taglib-location&gt;/WEB-INF/spring.tld&lt;/taglib-location&gt;
         </taglib>
+
         &lt;/taglib&gt;
     </jsp-config>
+
     &lt;/jsp-config&gt;

2020年2月16日 (日) 04:32時点における最新版

Spring MVC 各国語対応

Spring | Spring MVC |

Eclipse での手順

プロパティファイルを作成

  • messages_en.properties
  • messages_ja.properties

<blockquote>messages_ja.properties は native2ascii をかける</blockquote>

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>