<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
    主站蜘蛛池模板: 亚洲日韩中文字幕| 亚洲情侣偷拍精品| 亚洲婷婷综合色高清在线| 在线视频网址免费播放| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 亚洲视频免费在线播放| 无码精品国产一区二区三区免费| 久久亚洲中文字幕精品一区四| 国产成人高清精品免费观看| 亚洲精品岛国片在线观看| av成人免费电影| 亚洲成在人线av| 国产高清不卡免费在线| 亚洲综合av一区二区三区| 国产在线a不卡免费视频| 一区二区免费国产在线观看| AV在线亚洲男人的天堂| 国产一区二区免费| 亚洲精品电影天堂网| 女人被免费视频网站| 污污视频免费观看网站| 亚洲永久精品ww47| 最近免费最新高清中文字幕韩国| 亚洲国产午夜精品理论片| 在线观看成人免费视频| 久久高潮一级毛片免费| 亚洲一区二区三区夜色| 在线观看免费成人| 9久热精品免费观看视频| 久久精品国产亚洲av影院| 日本不卡免费新一二三区| 国产免费区在线观看十分钟| 亚洲高清视频免费| 国产免费私拍一区二区三区| 大地资源在线资源免费观看| 亚洲乱码一二三四五六区| 亚洲精品动漫人成3d在线 | 亚洲人成人网站18禁| 亚洲最大AV网站在线观看| 久草免费在线观看视频| 免费看一级毛片在线观看精品视频 |