トップ 差分 一覧 ping ソース 検索 ヘルプ PDF RSS ログイン

Spring MVC 各国語対応



目次



記事一覧

キーワード

Spring MVC 各国語対応

[Spring][Spring MVC]

 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>



YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto

Copyright© 矢木 浩人 All Rights Reserved.