六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 30|回复: 0

jquery confirm alert 国际化

[复制链接]

升级  9.33%

18

主题

18

主题

18

主题

秀才

Rank: 2

积分
64
 楼主| 发表于 2013-2-7 17:16:47 | 显示全部楼层 |阅读模式
为了处理国际化alert与confirm中英提示框的问题,即如果当前语言为中文的话提示为中文信息,当前语言为英文提示为英文信息,我选择了Jquery自定义方法解决此问题。
一、 在Jquery1.3.2.js中增加如下代码

jQuery.extend({   
/**  
     * @author Jacky  
     * @param bathPath  
     * @param key  
     *            资源文件中的key  
     * @param resSimpleName  
     *            资源文件的简称 BookAirResources 取 BookAir  
     * @param values  
     *            参数传递是一个数组 new Object[]={...}  
     *   
     */  
    alertMessageResource:function(bathPath,key,resSimpleName,values){   
        if(null==bathPath||bathPath=='<%=bathPath%>'){   
            bathPath = (null==getURL()||''==getURL())?document.location.href:getURL();   
        }   
        jQuery.ajax({   
            type: "POST",   
            url: bathPath+"/loaclAction.do?method=getMessageResource",   
            data: {   
              key : key,   
              bundle : resSimpleName,   
              values : (null == values) ? null : values   
             },   
             success: function(msg){   
                if( null != msg && "" != msg){   
                        alert(msg);   
                }else{   
                        alert("Warning! No"+ key +"In "+resSimpleName+"Resources");   
                }   
            },   
            dataType: "html"  
        });   
    }   
    ,   
    /**  
     * @author Jacky  
     * @param bathPath  
     * @param key  
     *            资源文件中的key  
     * @param resSimpleName  
     *            资源文件的简称 BookAirResources 取 BookAir  
     * @param values  
     *            参数传递是一个数组 new Object[]={...}  
     * @callback 回调方法 点击确定时要做调用方法  
     *   
     */  
    confirmMessageResource:function(bathPath,key,resSimpleName,values,callback){   
        if(null==bathPath||bathPath=='<%=bathPath%>'){   
            bathPath = (null==getURL()||''==getURL())?document.location.href:getURL();   
        }   
          jQuery.ajax({   
            type: "POST",   
            url: bathPath+"/loaclAction.do?method=getMessageResource",   
            data: {   
              key : key,   
              bundle : resSimpleName,   
              values : (null == values) ? null : values   
             },   
             success: function(msg){   
                if( null != msg && "" != msg){   
                        if(confirm(msg)){   
                            jQuery(callback);      
                        }   
                }else{   
                        callback=confirm("Warning! No"+ key +"In "+resSimpleName+"Resources");   
                        jQuery(callback);   
                }   
            },   
            dataType: "html"  
        });   
    }   
});   
  
/**  
* 得到当前请求的URL  
* @author Jacky  
*/  
function getURL(){   
    var resultURL;   
    var urlStr = document.location.href;   
    urlStr=urlStr.substring(0,urlStr.indexOf('?'));   
    resultURL=urlStr.substring(0,urlStr.lastIndexOf('/'));   
    return resultURL;   
}  

jQuery.extend({
/**
* @author Jacky
* @param bathPath
* @param key
*            资源文件中的key
* @param resSimpleName
*            资源文件的简称 BookAirResources 取 BookAir
* @param values
*            参数传递是一个数组 new Object[]={...}
*
*/
alertMessageResource:function(bathPath,key,resSimpleName,values){
if(null==bathPath||bathPath=='<%=bathPath%>'){
bathPath = (null==getURL()||''==getURL())?document.location.href:getURL();
}
jQuery.ajax({
type: "POST",
url: bathPath+"/loaclAction.do?method=getMessageResource",
data: {
  key : key,
  bundle : resSimpleName,
  values : (null == values) ? null : values
},
success: function(msg){
if( null != msg && "" != msg){
alert(msg);
}else{
alert("Warning! No"+ key +"In "+resSimpleName+"Resources");
}
},
dataType: "html"
});
}
,
/**
* @author Jacky
* @param bathPath
* @param key
*            资源文件中的key
* @param resSimpleName
*            资源文件的简称 BookAirResources 取 BookAir
* @param values
*            参数传递是一个数组 new Object[]={...}
* @callback 回调方法 点击确定时要做调用方法
*
*/
confirmMessageResource:function(bathPath,key,resSimpleName,values,callback){
if(null==bathPath||bathPath=='<%=bathPath%>'){
bathPath = (null==getURL()||''==getURL())?document.location.href:getURL();
}
  jQuery.ajax({
type: "POST",
url: bathPath+"/loaclAction.do?method=getMessageResource",
data: {
  key : key,
  bundle : resSimpleName,
  values : (null == values) ? null : values
},
success: function(msg){
if( null != msg && "" != msg){
if(confirm(msg)){
jQuery(callback);
}
}else{
callback=confirm("Warning! No"+ key +"In "+resSimpleName+"Resources");
jQuery(callback);
}
},
dataType: "html"
});
}
});

/**
* 得到当前请求的URL
* @author Jacky
*/
function getURL(){
var resultURL;
var urlStr = document.location.href;
urlStr=urlStr.substring(0,urlStr.indexOf('?'));
resultURL=urlStr.substring(0,urlStr.lastIndexOf('/'));
return resultURL;
}


二、使用Jquery自定义方法
Javascript代码
//引入JS:   
<script language="javascript" src="<%=basePath%>/js/jquery/jquery.js"></script>   
//alert使用   
$.alertMessageResource('msg.c_domesticairquery.dateinfo','Message',null);   
//confirm的使用   
$.confirmMessageResource('msg.c_domesticairquery.dateinfo','Message',null,getConfirm);   
function getConfirm(){   
   alert("sssssssss");   
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表