上周我主要工作在Ext的I18處理上, 有一個在JSP捆綁資源的功能我沒有想到。所以我就實現了這個功能-可以簡單的得到資源從頁面中,這個類的名字是Ext.i18n.ResourceBundle.主要的想法是建立一個對象并得到一個資源包的類,它意味著通過這個資源類,IE拿到資源的名稱和語言,然后試圖找一個.properties文件。 如果這語言是es-ES, 它將試圖尋找這[bundle]_es-ES。properties文件。如果不存在就讀取[bundle].properties文件。
然后你就能用getMsg(key)方法,得到一個字符串通過這個key屬性。
這是整個代碼和一點例子,享受它吧:)
Bundle.js and Test.js可以在這里找到: Ext.forum
(
http://extjs.com/forum/showthread.php?t=32456)
使用方法:
var bundle = new Ext.i18n.Bundle({bundle='Application'});
bundle.onReady(
alert('example'+ bundle.getMsg('key1'));
);
如果語言是es-ES,它將會讀取一個 Application_es-ES.properties 文件,文件內容像這樣:
key1 "Mensaje para la propiedad key1"
如果Application_es-ES.properties 文件不存在,它將讀取Application.properties文件:
#This is a simple comment
key1 "this is the message for key1"
類的構造函數是Bundle(config),參數config的格式是: {bundle: , patch:}
bundle: properties文件的名字.
{bundle: 'mybundle'}
它將查找這樣的一個文件在:
http:/yourdomain/yourApp/mybundle_[language].properties.
所以如果你不想做I18n,但又要保證以后可以擴展,至少建立一個mybundle.properties文件.
patch: (可選) 這個properties文件的地址.
{bundle: 'mybundle, path: 'resources'}
它將查找這個文件:
http:/yourdomain/yourApp/resources/mybundle_[language].properties.
Take into account that you need to write your application in the bundle.onReady() method in order to be able to access to your bundle.
你需要考慮到寫一個bundle。onReady方法來保證資源已經加載完成后在使用。
注意:
這個資源已經存儲到一個 Ext.data.Store 對象緩存里面,不會重復讀取。
完整demo:
//Test for Bundle
Ext.onReady(function(){
var bundle = new Ext.i18n.Bundle({bundle:'Application', path: 'resources'});
bundle.onReady(function(){
alert("culo"+bundle.getMsg('key1'));
});
});