java struts2 国际化配置

1.struts.properties
struts.custom.i18n.resources=language.message,language.bizMessage,language.format,language.messages,language.language
struts.objectFactory =spring
struts.objectFactory.spring.autoWire =type
struts.ui.theme=simple
struts.locale=zh_CN
struts.i18n.encoding=UTF-8
struts.configuration.xml.reload=false
struts.multipart.parser=jakarta
struts.multipart.maxSize=100000000
struts.devMode=true
2.配置文件名称必须以language.*  ------*.properties格式开头;



3.页面添加切换按钮

function changeLocalKey(localKey){
$.ajaxSettings.async = false;
$.getJSON(getContextPath()
+ '/common/changeLocalKey.action?random=' + Math.random(),
{
        "localKey" :localKey  //参数设定
}, function(data) {
window.location.reload();
});
}

4.java中action
/**
* 国际化语言选择
*
* @param email
* @return
*/
public String changeLocalKey() throws Exception {
    Locale currentLocable=null;
if(StringUtils.isNotEmpty(localKey)){
if(localKey.equals("chinese")){
currentLocable=new Locale("zh", "CN");
}
            if(localKey.equals("english")){
            currentLocable=new Locale("en", "US");
}
if(localKey.equals("japanese")){
currentLocable=new Locale("ja", "JP");
}
}
else{
currentLocable=Locale.getDefault();
}
ActionContext.getContext().setLocale(currentLocable);
Envir.getSession().setAttribute("WW_TRANS_I18N_LOCALE", currentLocable);
return SUCCESS;
}
5.如果初始化需要默认自定义语言添加如下配置 web.xml
<filter-mapping> 
        <filter-name>i18nFilter</filter-name> 
        <url-pattern>*.action</url-pattern> 
</filter-mapping>
<filter> 
       <filter-name>i18nFilter</filter-name> 
        <filter-class>com.canon.shwg.core.interceptor.I18nFilter</filter-class> 
</filter>
********************************************************自己添加拦截器
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class I18nFilter implements Filter {

@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest r = (HttpServletRequest) req; 
MyRequestWrapper request = new MyRequestWrapper(r); 
filterChain.doFilter(request, resp); 

    } 



@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}

}
***************************************************************MyRequestWrapper()
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;

import com.canon.shwg.core.base.action.BaseAction;
import com.canon.shwg.core.util.framework.Envir;
import com.opensymphony.xwork2.ActionContext;

public class MyRequestWrapper extends HttpServletRequestWrapper{

private Locale locale = null; 
 
    public MyRequestWrapper(HttpServletRequest request) { 
       super(request); 
        HttpSession session = request.getSession();
        locale = (Locale) session.getAttribute("WW_TRANS_I18N_LOCALE"); 
        if(locale==null){
        Locale currentLocable=null;
     currentLocable=new Locale("zh", "CN");
     //ActionContext.getContext().setLocale(currentLocable);
     request.getSession().setAttribute("WW_TRANS_I18N_LOCALE", currentLocable);
     //Envir.getSession().setAttribute("WW_TRANS_I18N_LOCALE", currentLocable);
        }
    } 
 
   /**
     * struts2的BUG,如果重定向的话,国际化默认取HTTP请求头中的参数 替换HTTP请求参数
     **/ 
   /* @Override 
    public String getHeader(String name) { 
        String value = super.getHeader(name); 
        if ("Accept-Language".equals(name) && locale != null) { 
           value = locale.getLanguage() + "_" + locale.getCountry() 
                    + value.substring(6, value.length()); 
        } 
        return value; 
   } 
 
    @Override 
   public Locale getLocale() { 
        if (locale != null) { 
           return locale; 
       } 
      return super.getLocale(); 
    }  */


}

你可能感兴趣的:(struts2)