如何基于jQuery.i18n實現(xiàn)web前端的國際化

這篇文章主要為大家展示了“如何基于jQuery.i18n實現(xiàn)web前端的國際化”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“如何基于jQuery.i18n實現(xiàn)web前端的國際化”這篇文章吧。

成都創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站制作、成都做網(wǎng)站與策劃設(shè)計,堯都網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:堯都等地區(qū)。堯都做網(wǎng)站價格咨詢:18982081108

在介紹 jQuery.i18n.properties 之前,我們先來看一下什么是國際化。國際化英文單詞為:Internationalization,又稱 i18n,“i”為單詞的第一個字母,“18”為“i”和“n”之間單詞的個數(shù),而“n”代表這個單詞的最后一個字母。在計算機領(lǐng)域,國際化是指設(shè)計能夠適應(yīng)各種區(qū)域和語言環(huán)境的軟件的過程。

jQuery.i18n.properties 是一款輕量級的 jQuery 國際化插件。與 Java 里的資源文件類似,jQuery.i18n.properties 采用.properties 文件對 JavaScript 進行國際化。jQuery.i18n.properties 插件根據(jù)用戶指定的(或瀏覽器提供的 )語言和國家編碼(符合 ISO-639 和 ISO-3166 標(biāo)準(zhǔn))來解析對應(yīng)的以“.properties”為后綴的資源文件。

利用資源文件實現(xiàn)國際化是一種比較流行的方式,例如 Android 應(yīng)用就可以采用以語言和國家編碼命名的資源文件來實現(xiàn)國際化。jQuery.i18n.properties 插件中的資源文件以“.properties”為后綴,包含了區(qū)域相關(guān)的鍵值對。我們知道,Java 程序也可以使用以 .properties 為后綴的資源文件來實現(xiàn)國際化,因此,當(dāng)我們要在 Java 程序和前端 JavaScript 程序中共享資源文件時,這種方式就顯得特別有用。jQuery.i18n.properties 插件首先加載默認(rèn)的資源文件(例如:strings.properties),然后加載針對特定語言環(huán)境的資源文件(例如:strings_zh.properties),這就保證了在未提供某種語言的翻譯時,默認(rèn)值始終有效。開發(fā)人員可以以 JavaScript 變量(或函數(shù))或 Map 的方式使用資源文件中的 key。

下面介紹一下如何在項目中如何使用i18n,說明一下,我這里與官網(wǎng)并不相同,i18n的一些方法我并沒有用,只是用到了很少的一部分,而且找出了比較適合我們項目使用的方式。

1.首先,建立資源文件:

如何基于jQuery.i18n實現(xiàn)web前端的國際化

locales/en-us/ns.jsp.json:

{ 
 "reSendMail": { 
  "emailSendFail": "Failed to send the email", 
  "emailHasSendToYourEmail": "The email has be sent to your email address. " 
 }, 
 "login": { 
  "pleaseWriteUserName": "Please input your username", 
  "pleaseWritePassword": "Please input your password " 
 }, 
 "activeRegist": { 
  "thisUserEmailHasUsed":"Email has already been used", 
  "thisUserNameHasUsed":"User Name has already been used", 
  "4to30Char":"Please enter 4-30 characters", 
  "1to50Char":"Please enter 1-50 characters", 
  "1to16Linkman":"Please enter 1-16 characters", 
  "loginPage":"Login Page", 
  "EmailMustNotEmpty": "Email can't be blank", 
  "PWDNotEmpty": "Password can't be blank", 
  "nameNotEmpty":"Name can't be blank", 
  "conpanyNotEmpty":"Company can't be blank", 
  "qqNotEmpty":"QQ can not be blank", 
  "phoneNotEmpty":"Mobile can not be blank", 
  "least50charEmailAddress":"No more than 50 characters for email address", 
  "enterEmailAddressLikeThis":"Email address format 'abc@abc.com'", 
  "enter6To32Character":"Please enter 6-32 characters", 
  "NameMost30Character":"No more than 30 characters for name", 
  "QQTypeIsWrong":"Incorrent QQ format", 
  "phoneTypeNotCorrect":"Incorrent mobile format", 
  "thisEmailHasRegistered":"Email address has already been registered", 
  "registerFail":"Registration failed!", 
   "TwoTimesPWDIsDifferent":"The passwords you entered do not match. Please try again." 
 } 
}

中文配置文件就不寫了,格式一樣,用了map的形式份模塊來寫。

2.在jsp頁面上引入i18n.js并初始化i18n

<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> 
<script type="text/javascript" src="js/i18next.js"></script> 
<script type="text/javascript"> 
i18n.init({ 
 lng:'${sessionScope.language }', 
 ns: { namespaces: ['ns.jsp'], defaultNs: 'ns.jsp'}, 
 useLocalStorage: false 
}); 
</script>

3.js引用

var emailflag = false; 
function checkemail() { 
 check('email', 'emailmessage'); 
 var email = $("#email").attr("value"); 
 if(email != null && email != "") { 
  if(email.length > 50) { 
   setDivInfo("emaildiv", i18n.t('activeRegist.least50charEmailAddress'), 1);//請輸入50字符內(nèi)的郵箱地址 
  } else { 
   if(isEmail(email, $("#email"))) { 
    checkemailForServer(email); 
   } else { 
    setDivInfo("emaildiv", i18n.t('activeRegist.enterEmailAddressLikeThis'), 1);//請輸入郵箱地址,格式為abc@abc.com 
   } 
  } 
 } 
}

4.測試

如何基于jQuery.i18n實現(xiàn)web前端的國際化

如何基于jQuery.i18n實現(xiàn)web前端的國際化

以上是“如何基于jQuery.i18n實現(xiàn)web前端的國際化”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

新聞名稱:如何基于jQuery.i18n實現(xiàn)web前端的國際化
標(biāo)題URL:http://www.muchs.cn/article10/gjjodo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站策劃、全網(wǎng)營銷推廣、品牌網(wǎng)站建設(shè)網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)