java spring mvc jstl國際化

spring 添加攔截 若是參數獲取到設定國際化參數 進行動態獲取java

 

 

package com.ewhl.interceptor;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class I18nInterceptor extends HandlerInterceptorAdapter {

   private String paramName;
   /**
    * Set the name of the parameter that contains a locale specification
    * in a locale change request. Default is "locale".
    */
   public void setParamName(String paramName) {
      this.paramName = paramName;
   }

   /**
    * Return the name of the parameter that contains a locale specification
    * in a locale change request.
    */
   public String getParamName() {
      return this.paramName;
   }
   
   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
         throws Exception {
      String lang = request.getParameter("lang");

      if (StringUtils.isNotBlank(lang)) {
         LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);

         if (localeResolver == null) {
            throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
         }

         localeResolver.setLocale(request, response, getLocal(lang));
         request.getSession().setAttribute("localeLang",localeResolver.resolveLocale(request));
         return true;
      }
      if(request.getSession().getAttribute("localeLang")!=null){
         LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
         if (localeResolver == null) {
            throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
         }
         localeResolver.setLocale(request, response, (Locale) request.getSession().getAttribute("localeLang"));
         request.getSession().setAttribute("localeLang",localeResolver.resolveLocale(request));
         return true;
      }
      Cookie langCookie = this.existLang(request);
      if (langCookie != null) {
         LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);

         if (localeResolver == null) {
            throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
         }
         request.getSession().setAttribute("localeLang",localeResolver.resolveLocale(request));
         localeResolver.setLocale(request, response, getLocal(langCookie.getValue()));
         return true;
      }
      request.getSession().setAttribute("localeLang",Locale.SIMPLIFIED_CHINESE);
      return true;
   }

   private Cookie existLang(HttpServletRequest request) {
      Cookie[] cookies = request.getCookies();
      if (cookies != null) {
         for (Cookie cookie : cookies) {
            if ("lang".equalsIgnoreCase(cookie.getName())) {
               return cookie;
            }
         }
      }
      return null;
   }

   private Locale getLocal(String lang) {
      if ("zh".equalsIgnoreCase(lang)) {
         return Locale.SIMPLIFIED_CHINESE;
      } else if ("en".equalsIgnoreCase(lang)) {
         return Locale.US;
      } else {
         return Locale.SIMPLIFIED_CHINESE;
      }

   }

}

resouces目錄下放上web

message_en_US.propertiesspring

login.loginButton=Login

message_zh_CN.propertiesapache

login.loginButton=登陸

若是是jsp須要cookie

<context-param>
   <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
   <param-value>message</param-value>
</context-param>

支持jstl方式app

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<% LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
   if (localeResolver == null || localeResolver.resolveLocale(request).toString().equals("")) {
%>
<c:set var="localeResolver" value="zh_CN" />
<fmt:setLocale value="zh_CN" scope="application"/>
<%} else if ("cn".equalsIgnoreCase(localeResolver.resolveLocale(request).toString())) {%>
<c:set var="localeResolver" value="zh_CN" />

<fmt:setLocale value="zh_CN" scope="application"/>
<% } else {%>
<c:set var="localeResolver" value="en_US" />
<fmt:setLocale value="en_US" scope="application"/>
<% }%>

 

<fmt:message key="login.loginButton" /> 動態獲取

 

若是java代碼獲取國際化jsp

根據application獲取message ide

public String getLocale(String key){
   return SpringMvcBeanProxy.getApplicationContext().getMessage(key,null,(Locale)((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession().getAttribute("localeLang"));
}
getLocale(key) 直接獲取

相關文章
相關標籤/搜索