關於ssh國際化的記錄

最近一點時間,公司項目上需要用到國際化。小小的研究了下,腦袋頭都大了。不是說國際化難,而是看合不合適自己。

接觸到國際化的技術分類有兩種

  1. 1.   前臺js國際化

  2. a)     jquery.i18n.properties

  3. b)    jquery.i18n

  4. c)     i18next

  5. 2.   後臺框架及java原生國際化方法

  6. a)     Java原生 ResourceBundle獲取en_US.properties文件,獲取值

  7. b)    Struts2配置相應配置文件 action繼承ActionSupport方法,用getText方法取值頁面引入Struts2標籤庫的方式取值。

  8. c)     Spring進行國際化. Struts2差不多。

因爲項目框架的原因,我不得不使用Struts2 + i18next來進行國際化。這兩種國際化的方式網上都有。我簡單列下要注意的點

  1. 1.    Struts2國際化會針對遊覽器語言進行自動切換語言,但常常我們自己希望控制語言的切換。

我的方法如下:

 

    /**
     * 語言切換
     * @return
     */
    publicvoid doChageLanguage(){
        String language =contextPvd.getRequestStr("language");
        Locale locale = null;
        if (language.equals("zh_CN")) {
            locale = Locale.CHINA;
        } else {
            locale = Locale.US;
        }
        //使用cookie 保存用戶語言信息
        Cookie cookie = new Cookie("language", language);
        contextPvd.addCookie(cookie);
        cookie.setMaxAge(60 * 60 * 24 * 14);
        cookie.setPath(contextPvd.getAppCxtPath());   
       
        //使用Session 保存用戶語言信息
        contextPvd.setLocale(locale);       
        contextPvd.setSessionAttr("WW_TRANS_I18N_LOCALE", locale);
}



但是發現語言切換還是有問題,查詢了下發下 Struts2bug~(不知道現在修復了沒,我用的版本比較老)

解決問題的方案見:

http://showtime520.iteye.com/blog/1042585(感謝分享)

我對上面代碼進行了簡單修改

見代碼

publicMyRequestWrapper(HttpServletRequest request) { 
        super(request); 
        
        Cookie  cookie =null;
        Cookie[] cookies =  request.getCookies();
        if (cookies != null) {
            for (Cookie c : cookies) {
                if (c.getName().equals("language")) {
                    cookie =c ;
                }
            }
        }
        
        if(null !=cookie){
        Stringlanguage = cookie.getValue();
        if(!StringUtils.isBlank(language) ){
              if (language.equals("zh_CN")) {
                      locale = Locale.CHINA;
                  } else {
                      locale = Locale.US;
                  }
            }else{
                HttpSession session =request.getSession();  
                locale = (Locale) session.getAttribute("WW_TRANS_I18N_LOCALE"); 
            }
        }
    }



主要使用Cookie 獲取語言~~Struts2注意點就這些。

然後講講 i18next,說實話如果不是框架的原因  單單使用i18next就可以完全實現國際化。

而且i18next非常小。但是i18next的資料很少,筆者的英語很爛,也只能咬着呀看官方文檔,但還是說同學們看文檔吧~~

 

關鍵初始化:

               var option = {
                     ns: 'i18n' , 
                              sendType: 'post',
                              resGetPath: bootPATH+'i18n/__lng__.json',
                              debug: true,
                              lng: language};
       i18n.init(option, function(err, t) {
$("body").i18n();
       }); 
       Json格式:
{  
"login":
    {    
        "msg":"land successfully,Get tothe system right away...", 
        "msg1": "landsuccessfully"
    } 
}



使用:

在頁面中用   data-i18n="login.msg" 直接使用。js彈出 時用 直接用i18n.t("ogin.msg")的方式直接獲取.

就寫到這裏。看看對你有沒有幫助~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章