Spring MVCの国際化とFreemarkerの統合の国際化


Springバージョン4.1.2 
国際文書を書く
messages_en_US.properties英語
loginFrom.username=login username
loginFrom.password=login password
locale.selection = Language Selection
messages_zh_CN.properties中国語簡体字
loginFrom.username=\u767B\u9646\u7528\u6237\u540D
loginFrom.password=\u767B\u9646\u5BC6\u7801
locale.selection = \u8BED\u8A00\u9009\u62E9
messages_zh.properties中国語
loginFrom.username=\u767B\u9646\u7528\u6237\u540D zh
loginFrom.password=\u767B\u9646\u5BC6\u7801 zh
locale.selection = \u8BED\u8A00\u9009\u62E9 zh

Spring MVC+jsp国際化
Spring MVCプロファイルにプロファイルを追加

 	
	
	
 	
       	
			
				
				messages/messages
			
		
		
        
    

JspページにSpring MVCタグを追加

Spring MVC + Freemarker

spring.ftl   spring-webmvc-4.1.2.RELEASE.jar   \org\springframework\web\servlet\view\freemarker\spring.ftl
 
           
Freemarker ( LocaleController )



login.ftl






$(function(){ $("#selection").change(function(){ var locale = $(this).val(); $.post("/18n/locale/change",{locale:locale},function(data){ if(data == "success"){ window.location.href="/18n/locale/login" }else{ alert(" "); } },"html") }); })

LocaleController
package com.lwp.controller;
import java.io.IOException;
import java.util.Locale;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.support.RequestContext;

@Controller
@RequestMapping("/locale")
public class LocaleController {
	
	@RequestMapping("/login")
	public String login(){
		return "/login";
	}
	
	
	
	@Autowired
    private LocaleResolver localeResolver; 
	
	@RequestMapping("/change")
	@ResponseBody
	public String setSession(HttpServletRequest request,HttpServletResponse response,String locale){
		try {
			//         ,    if               session  cookie          
			//
			if(localeResolver instanceof org.springframework.web.servlet.i18n.SessionLocaleResolver){
				String[] localeArr = locale.split("_");
				
				Locale l = null;
				if(localeArr.length == 1){
					l = new Locale(localeArr[0]);  
				}else{
					l = new Locale(localeArr[0],localeArr[1]);
				}
				localeResolver.setLocale(request, response, l); 
				//request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, l); //    
				System.out.println("SessionLocaleResolver");
			}
			//
			if(localeResolver instanceof org.springframework.web.servlet.i18n.CookieLocaleResolver){
					Cookie cookie = new Cookie(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME, new Locale(locale).toString());
					cookie.setMaxAge(5);
					response.addCookie(cookie);
					response.flushBuffer();
					System.out.println("CookieLocaleResolver");
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
		
	}
	
	/**
	 *              
	 * @param response
	 * @param request
	 */
	@RequestMapping("returnMessage")
	public void returnMessage(HttpServletResponse response,HttpServletRequest request){
		RequestContext rc = new RequestContext(request);
		String w = rc.getMessage("loginFrom.username"); //  locale        
		try {
			response.getWriter().write(w);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}