1 基本介紹
個人建議使用jquery.i18n.properties-1.0.x版本,高版本可能會出現意想不到的問題。
我在git上傳了jquery.i18n.properties-1.0.x、jquery.i18n.properties-1.2.7(最新版本)和jquery-1.9.0.js源文件,有需要的可以下載: https://github.com/MasonYyp/jquery.i18n.git
在jquery.i18n.properties設置properties對應的結構如下圖:
1.1 在jquery.i18n.properties-1.0.x中
(1) 如果沒報錯,並且出現key的原始信息(如下圖),則是properties的路徑出問題了,檢查name、language和path,注意path中路徑最後最好加上”/”,在新的版本中可能不需要。
1.2 在jquery.i18n.properties-1.2.7中
(1) 如果出現“Cannot read property 'length' of undefined”的錯誤,則是properties的路徑出現了問題,檢查name、language和path是否正確。
(2) 如果出現“settings is not defined”的錯誤,請將jquery.i18n.properties1.7.2.min.js更換爲jquery.i18n.properties1.7.2.js,如果還有問題,請更換爲其他版本的jquery.i18n.properties。
2 源程序和運結果
本文使用兩種方法,設置國際化
2.1 在回調函數中調用
// 源代碼
function set_properties_callback(local) {
$.i18n.properties({
name: 'messages', // 資源文件名稱
path: './js/language/', // 資源文件所在目錄路徑
mode: 'map', // Map的方式使用資源文件中的Key
language: local, // 設置的語言
cache: false,
encoding: 'UTF-8',
callback: function () { // 回調方法
alert("回調函數中:"+$.i18n.prop('msg_hello'));
}
});
}
2.2 不在回調函數中調用
// 源代碼
function set_properties(local) {
$.i18n.properties({
name: 'messages', // 資源文件名稱
path: './js/language/', // 資源文件所在目錄路徑
mode: 'map', // Map的方式使用資源文件中的Key
language: local, // 設置的語言
cache: false,
encoding: 'UTF-8'
});
alert("不在回調函數中:"+$.i18n.prop('msg_hello'));
}