js讀取properties配置文件,大多數的方法都是引入一個jQuery的插件jquery.i18n.properties,不能否認,確實可行,下面就讓咱們來看下具體的使用。javascript
涉及到的js可從該網址的示例代碼中下載得到:java
https://www.ibm.com/developerworks/cn/web/1305_hezj_jqueryi18n/jquery
jQuery.i18n.properties() 用法:web
jQuery.i18n.properties({ name:'strings',// 資源文件名稱 path:'bundle/',// 資源文件所在目錄路徑 mode:'both',// 模式:變量或 Map language:'pt_PT',// 對應的語言 cache:false, encoding: 'UTF-8', callback: function() {// 回調方法 } });
在上面的配置中,又尤爲要注意的是資源文件路徑,若是路徑出錯,讀取文件確定會失敗,從 jquery.i18n.properties-1.0.9.js中也能夠看出。除了幾個必要的配置,如:name、path、mode加上callback回調方法,其餘的能夠去掉,在callback回調方法中如無特別須要,可沒必要添加具體實現。.net
jQuery.i18n.prop(key) 使用:插件
$.i18n.prop('myKey')
在運行 jQuery.i18n.properties()後,可經過$.i18n.prop(key)的方式獲取配置文件中已配置的信息,如code
function loadProperties(){ jQuery.i18n.properties({ name:'strings', path:'static/js/jiajieJs/', mode:'map', callback: function() { } }); } function duquInfo(){ loadProperties(); $.i18n.prop('myKey'); }
配置文件若無其餘語言方面的須要,能夠只寫一個,如strings.properties,內容如ip
myKey=123jdksk456
親測可用。資源