jquery-i18n-properties國際化(最簡單案例)
阿新 • • 發佈:2019-01-01
概述:
最近需要需要整國際化,網上有很多例子,但是本人資質愚鈍,看不懂。。。記錄一下怎麼實現最簡單的國際化,(通了就好說,不通感覺懷疑人生了),關於國際化的概念就免了(不同國家看到不同語言),實戰為主,GitHub最新國際化js不會用,報錯。
1、準備
工具:Eclipse,Tomcat
下載對應js檔案:
2、檔案放置位置
檔案清單:
- 1、strings_zh.properties (表示中文屬性檔案)
- 2、strings.properties (表示預設情況下屬性檔案)
- 3、jquery.i18n.properties-min-1.0.9.js(國際化需要檔案)
- 4、jquery.js (jquery.js檔案,把版本號給刪掉)
- 5、test.html (展示html頁面)
檔案結構:
3、檔案內容
3.1、string_zh.properties (注意屬性檔案建立字元編碼是ISO-8859-1格式,可以修改成UTF-8,否則漢字顯示不出來)
右鍵string_zh.properties->Properties(最下面的選項)->Text file encoding
username=使用者名稱:
password=密碼:
3.2、strings.properties
username=User Name:
password=Password:
3.3、test.html
language的還可以根據瀏覽器來確定選擇屬性檔案,把上面language:"zh"改為:language:$.i18n.browserLang()<html lang="en"> <head> <meta charset="UTF-8"> <title>國際化</title> </head> <body> <label data-locale="username">使用者名稱:</label><input type="text"> <label data-locale="password">密碼:</label><input type="password"> <script src="../lib/jquery.js"></script> <!-- 載入語言包檔案 --> <script src="../lib/jquery.i18n.properties-min-1.0.9.js"></script> <script type="text/javascript"> loadProperties(); function loadProperties() { $.i18n.properties({ name:'strings', //屬性檔名 命名格式: 檔名_國家代號.properties path:'../i18n/', //注意這裡路徑是你屬性檔案的所在資料夾 mode:'map', language:"zh", //這就是國家代號 name+language剛好組成屬性檔名:strings+zh -> strings_zh.properties callback:function(){ $("[data-locale]").each(function(){ console.log($(this).data("locale")); $(this).html($.i18n.prop($(this).data("locale"))); }); } }); } </script> </body> </html>
4、將應用新增Tomcat啟動
---------------------------謝謝你寶貴的時間