<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    孤燈野火
    暢想的天空
    posts - 2,comments - 4,trackbacks - 0

    1.       根據瀏覽器語言配置國際化

    1.1 例如國際化配置文件internationalization.xml,放置在spring/appServlet/文件夾下,可根據需要具體放置


     

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>

        
    <!-- Internationalization start -->

        
    <bean id="messageSource" class="com.util.ResourceBundleMessageSourceExtend">

            
    <property name="basename">

                
    <value>config/account/messages-account  </value>

            
    </property>

            
    <property name="useCodeAsDefaultMessage" value="true" />




    </beans>




    <!-- Internationalization end -->


    </bean>

     

     

     

    注:此配置文件加載國家化語言配置文件,value中值表示在當前src下的config/core文件件下的語言配置文件,例如:
    可根據模塊配置多個不同的配置文件以逗號放置在value中,例如:

      1.2 加載internationalization.xml

                    web,xml需要配置,加載國際化配置文件

    例如: 

     

    <context-param>   
    <param-name>contextConfigLocation</param-name>

    <param-value>

     classpath:spring/appServlet/internationalization.xml

    </param-value>

    </context-param>

     

      1.3 國際化頁面的展現

              引入spring或者jstl的標簽庫,例如

    <%@ taglib uri="/spring"
    prefix="spring"%>
    ,也可手動引入spring.tld,那么在uri中寫入spring.tld所在位置,例如:/WEB-INF/tag/spring.tld

       在需要國際化的jsp中引入標簽:

             <spring:message code="CM01S01_Label_Password " /> 
    code
    中可根據需要寫入國際化語言配置的key

      1.4 啟動加載國際化語言的類以及后臺國際化

             1.4.1 ResourceBundleMessageSourceExtend.java
       

    package com.core.util;

    import org.springframework.context.support.ResourceBundleMessageSource;

    /**
     * ResourceBundleMessageSourceExtend
     * 
     * 
    @author liu_dawei
     * 
     
    */

    public class ResourceBundleMessageSourceExtend extends ResourceBundleMessageSource {

        
    private String[] extendBaseNames;

        
    /**
         * 
         * <p>
         * setBasenames
         * </p>
         * 
         * 
    @param basenames
         * 
    @see org.springframework.context.support.ResourceBundleMessageSource#setBasenames(java.lang.String[])
         
    */

        @Override
        
    public void setBasenames(String[] basenames) {
            
    // TODO Auto-generated method stub
            String tempBaseName = basenames[0].toString();
            
    int length = tempBaseName.split(",").length;
            extendBaseNames 
    = new String[length];

            
    for (int i = 0; i < length; i++{
                extendBaseNames[i] 
    = tempBaseName.split(",")[i];
            }


            
    super.setBasenames(extendBaseNames);
        }


    }

     

        1.4.2 MessageManager類

    /**
     * MessageManager.java
     * 
     * @screen core
     * 
    @author liu_dawei
     
    */

    package com.core.util;

    import java.util.Locale;

    import javax.servlet.http.HttpServletRequest;

    import org.springframework.context.ApplicationContext;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    import org.springframework.web.servlet.LocaleResolver;
    import org.springframework.web.servlet.support.RequestContextUtils;

    /**
     * The internationalization message manager class
     * 
     * 
    @author liu_dawei
     * 
     
    */

    public class MessageManager {

        
    /** the single instance */
        
    private static MessageManager instance;

        
    /**
         * <p>
         * The Constructors Method.
         * </p>
         * 
         
    */

        
    private MessageManager() {}

        
    /**
         * <p>
         * get the single instance of MessageManager.
         * </p>
         * 
         * 
    @return the single instance
         
    */

        
    public static MessageManager getInstance() {

            
    if (null == instance) {
                instance 
    = new MessageManager();
            }


            
    return instance;
        }


        
    /**
         * get Message
         * 
         * 
    @param key properties key
         * 
    @return the message
         
    */

        
    public String getMessage(String key) {
            
    return this.getMessage(key, null);
        }


        
    /**
         * get Message
         * 
         * 
    @param key properties key
         * 
    @param obj the parameters
         * 
    @return the message
         
    */

        
    public String getMessage(String key, Object[] obj) {
            
    // TODO
            ApplicationContext actx = null;
            actx 
    = WebApplicationContextUtils.getWebApplicationContext(getHttpServletRequest().getSession()
                    .getServletContext());

            String value 
    = "";
            value 
    = actx.getMessage(key, obj, getLocale());

            
    if (StringUtil.isEmpty(value)) {
                value 
    = key;
            }

            
    return value;
        }


        
    /**
         * get Message
         * 
         * 
    @param key properties key
         * 
    @param obj the parameters
         * 
    @return the message
         
    */

        
    public String getMessage(String key, Object[] obj, Locale locale) {
            
    // TODO
            ApplicationContext actx = ApplicationContextManager.getInstance().getApplicationContext();

            String value 
    = actx.getMessage(key, obj, locale);

            
    if (StringUtil.isEmpty(value)) {
                value 
    = key;
            }

            
    return value;
        }


        
    /**
         * get the Locale info
         * 
         * 
    @return the Locale info
         
    */

        
    public Locale getLocale() {

            HttpServletRequest request 
    = getHttpServletRequest();

            LocaleResolver localeResolver 
    = RequestContextUtils.getLocaleResolver(request);

            
    return localeResolver.resolveLocale(request);

        }


        
    /**
         * 
         * <p>
         * getIELanguage
         * </p>
         * 
         * 
    @return
         
    */

        
    public String getIELanguage() {

            
    return getHttpServletRequest().getLocale().getLanguage();
        }


        
    /**
         * <p>
         * get the request.
         * </p>
         * 
         * 
    @return the request
         
    */

        
    public HttpServletRequest getHttpServletRequest() {
            ServletRequestAttributes servletRequestAttributes 
    = (ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes();
            
    if (servletRequestAttributes != null{
                
    return servletRequestAttributes.getRequest();
            }
     else {
                
    return null;
            }

        }


    }

    注:getMessage(String key, Object[] obj) obj的參數傳的國際化語言中附帶的參數,例如:

    Common_E0014=Input value of {0} is too short. Minimum {1} character

    Obj[0]=”a”,obj[1]=”b”,如果調用getMessage(“Common_E0014”,obj),那么形成的值為:Input value of a is too short. Minimum b character

       getLocale()方法用于spring Session國際化的,只要session中有Locale,就會使用session中的locale,以下會詳解

    2.       Spring Session國際化

    1.1  與瀏覽器國際化相比需要在internationalization.xml中加入以下配置

     

    <bean id="localeResolver"
            class
    ="org.springframework.web.servlet.i18n.SessionLocaleResolver">
            
    <property name="defaultLocale" value="en"></property>
        
    </bean>


        
    <bean id="controllerClassNameHandler"
            class
    ="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
            
    <property name="interceptors">
                
    <list>
                    
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
                
    </list>

            
    </property>
        
    </bean>



    1.2   調用jsp中改變語言的按鈕時,需要把變化的語言傳到后臺,并放置在session中,例如:

      // language 頁面傳入的語言
            String newLanguage = language;
            
    if (!StringUtil.isEmpty(newLanguage)) {
                
    // get the locale resolver
                LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
                
    if ("zh".equals(newLanguage)) {
                    localeResolver.setLocale(request, response, Locale.CHINA);
                }
     else if ("en".equals(newLanguage)) {
                    localeResolver.setLocale(request, response, Locale.ENGLISH);
                }
     else if ("jp".equals(newLanguage)) {
                    localeResolver.setLocale(request, response, Locale.JAPAN);
                }
     else {
                    localeResolver.setLocale(request, response, Locale.ENGLISH);
                }

            }
     else {
                
    ////此處使用的外界傳入locale,如果默認進入主頁面想使用瀏覽器的,可調用request.getLocale().getLanguage();
                newLanguage = locale.getLanguage();
            }

     

    其他使用方法類似------------------------------end



    posted on 2012-11-01 11:00 孤飛燕 閱讀(2621) 評論(0)  編輯  收藏 所屬分類: Spring
    主站蜘蛛池模板: 香港一级毛片免费看| 精品免费tv久久久久久久| 亚洲人成无码www久久久| 国产麻豆一精品一AV一免费| 亚洲国产美女精品久久| 国产一区二区三区在线免费| 成全视频免费观看在线看| 亚洲精品中文字幕乱码| 又爽又黄无遮挡高清免费视频| 成人无码a级毛片免费| 亚洲天然素人无码专区| 亚洲夜夜欢A∨一区二区三区| 免费在线看v网址| 国产激情久久久久影院老熟女免费| 亚洲最新在线视频| 亚洲精品无码久久毛片| 成年人视频免费在线观看| 一区二区三区精品高清视频免费在线播放 | 亚洲精品日韩专区silk| 免费能直接在线观看黄的视频 | 午夜精品射精入后重之免费观看| 亚洲色大情网站www| 亚洲AV无码乱码在线观看裸奔 | 91嫩草亚洲精品| 不卡一卡二卡三亚洲| 成年丰满熟妇午夜免费视频| 国产在线观看无码免费视频| 亚洲欧美第一成人网站7777| 亚洲一区二区中文| 亚洲中文字幕伊人久久无码| 成年人性生活免费视频| 在线观看的免费网站无遮挡| 男女拍拍拍免费视频网站| 国产精品亚洲片在线花蝴蝶| 亚洲国产成人综合| 午夜亚洲国产理论秋霞| 日日噜噜噜噜夜夜爽亚洲精品| 日韩高清在线免费看| 一二三四免费观看在线视频中文版 | 国产在线观看免费av站| 久久精品国产亚洲AV天海翼|