我一直在寻找一个Ext中的I18N的解决方案,事实上我在官方并没有找到。在Google搜索之后,发现Mr.Max 写了一个Ext的插件解决了这个问题,他的文章地址是:
http://elmasse.blogspot.com/2008/04/i18n-with-extjs-resource-bundle.html
以下是翻译:
引用内容上周我主要工作在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'));
});
});
除 了文章中Extjs论坛外,这里也可以下载:
http://www.uusam.com/uu/blog/attachments/month_0812/k20081229174452.rar
现在对他做个小的扩展, 以支持参数功能。
http://elmasse.blogspot.com/2008/04/i18n-with-extjs-resource-bundle.html
以下是翻译:
![引用内容](https://i-blog.csdnimg.cn/blog_migrate/d74b423f5baabb19722e568bdc821940.jpeg)
然 后你就能用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'));
});
});
除 了文章中Extjs论坛外,这里也可以下载:
http://www.uusam.com/uu/blog/attachments/month_0812/k20081229174452.rar
现在对他做个小的扩展, 以支持参数功能。
打开Bundle.js文件,找到getMsg函数。修改为以下代码:
getMsg : function(key, str1, str2) {
if (this.getById(key)) {
if (!str1) {
str1 = '';
}
if (!str2) {
str2 = '';
}
return String.format(Ext.util.Format.htmlDecode(this
.getById(key).data), str1, str2);
} else {
return key + 'undefined';
}
},
你也可以修改为支持更多参数的,以上我仅仅改成了两个的。
以上转自:http://www.uusam.com/uu/blog/article.asp?id=269