「Spring MVC」MessageSourceを使ってページの国際化を実装する

1.properties の設定
Messages_ja.properties(日本語ロケール)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
username=山田
profile=大学:{0} 出身:{1}
username=山田 profile=大学:{0} 出身:{1}
username=山田
profile=大学:{0} 出身:{1}

Messages_en.properties(英語ロケール)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
username=yamada
profile= school:{0} hometown:{1}
username=yamada profile= school:{0} hometown:{1}
username=yamada
profile= school:{0} hometown:{1}

2.applicationContext.xmlの設定

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/Messages" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/WEB-INF/Messages" /> </bean>
<bean id="messageSource"
            class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/Messages" />
</bean>

「WEB-INF」フォルダの「MessageRes.properties」を指定します
「ReloadableResourceBundleMessageSource」クラス指定されたベース名を使用してリソースバンドルにアクセスし、Spring ApplicationContext のリソースロードに参加する、
Spring 固有の MessageSource 実装。

3.コントローラ側(CftController.java)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class CftController {
@Autowired
MessageSource mgs; //DIの設定
@RequestMapping(value = "/", method = GET)
public String info(Locale locale) {
String[] strArr = { "東京大学", "大阪" };
//メッセージファイルのキーを指定
System.out.println(mgs.getMessage("username", null, locale));
//パラメータを指定
System.out.println(mgs.getMessage("profile", strArr, locale));
return "cft";
}
}
package com.arkgame.study; import static org.springframework.web.bind.annotation.RequestMethod.GET; import java.util.Locale; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class CftController { @Autowired MessageSource mgs; //DIの設定 @RequestMapping(value = "/", method = GET) public String info(Locale locale) { String[] strArr = { "東京大学", "大阪" }; //メッセージファイルのキーを指定 System.out.println(mgs.getMessage("username", null, locale)); //パラメータを指定 System.out.println(mgs.getMessage("profile", strArr, locale)); return "cft"; } }
package com.arkgame.study;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CftController {

      @Autowired
      MessageSource mgs; //DIの設定

      @RequestMapping(value = "/", method = GET)
      public String info(Locale locale) {
            String[] strArr = { "東京大学", "大阪" };
            
             //メッセージファイルのキーを指定
            System.out.println(mgs.getMessage("username", null, locale));
            
            //パラメータを指定
            System.out.println(mgs.getMessage("profile", strArr, locale));
            return "cft";
      }
}

メッセージを取得したい Java クラスで、MessageSource をプロパティとして定義します。そして @Autowired を付けて、Spring に設定(DI)します。

4.JSP側cft.jsp

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<div>
<spring:message code="username" />
<br />
<spring:message code="profile" arguments="東京大学,大阪" />
</div>
<div> <spring:message code="username" /> <br /> <spring:message code="profile" arguments="東京大学,大阪" /> </div>
<div>
<spring:message code="username" />
      <br />
<spring:message code="profile" arguments="東京大学,大阪" />
</div>

 

SpringMVC

Posted by arkgame