js讀取properties配置文件,大多數的方法都是引入一個jQuery的插件jquery.i18n.properties,不可否認,確實可行,下面就讓我們來看下具體的使用。
涉及到的js可從該網址的示例代碼中下載獲得:
https://www.ibm.com/developerworks/cn/web/1305_hezj_jqueryi18n/
jQuery.i18n.properties() 用法:
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回調方法中如無特別需要,可不必添加具體實現。
jQuery.i18n.prop(key) 使用:
$.i18n.prop('myKey')
在運行 jQuery.i18n.properties()后,可通過$.i18n.prop(key)的方式獲取配置文件中已配置的信息,如
function loadProperties(){ jQuery.i18n.properties({ name:'strings', path:'static/js/jiajieJs/', mode:'map', callback: function() { } }); } function duquInfo(){ loadProperties(); $.i18n.prop('myKey'); }
配置文件若無其他語言方面的需要,可以只寫一個,如strings.properties,內容如
myKey=123jdksk456
---------------------------------------------------------------------------------------------
文章出處:https://my.oschina.net/u/3747963/blog/1589360