三、在ASP中使用
在ASP中使用bingTransLate
在asp中比在vb6中更方便,我们制作2个文件,1.html和1.asp
1.html是前端通过ajax与1.asp通讯,1.asp则负责处理翻译过程,
1. html代码如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>bingTranslate</title>
</head>
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<body>
<textarea name="text1" cols="50" rows="10" id="text1"></textarea><br>
翻译从<select id='lngfrom'>
<option value='en' selected>英语</option>
<option value='zh-CHS'>中文</option></select>
到<select id='lngto'>
<option value='zh-CHS' selected>中文</option>
<option value='en'>英语</option></select>
<input type='button' name='trans' id='trans' value='翻译'>
<br>
<textarea name="text2" cols="50" rows="10" id="text2"></textarea>
<script language="javascript">
$(document).ready(function(){
$("#trans").click(abc);
});
function abc(){
$.post('1.asp',
{Content:escape($("#text1").val()),lngfrom:escape($("#lngfrom").val()),lngto:escape($("#lngto").val())},function (data){ //回传函数
$('#text2').html(data);
});
}
</script>
</body>
</html>
1. asp代码如下
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Response.Expires=0
response.charset="GB2312"
dim ResponseInfo
call TransLate
response.Write(ResponseInfo)
Sub TransLate()
dim c_WSDL_URL,c_SERVICE,c_PORT,c_SERVICE_NAMESPACE,appID
c_WSDL_URL = "http://api.microsofttranslator.com/V2/Soap.svc"
c_SERVICE = "SoapService"
c_PORT = "BasicHttpBinding_LanguageService"
c_SERVICE_NAMESPACE = "http://tempuri.org/"
appID = "你的appid"
Dim str_WSML
'str_WSML = "<servicemapping>"
'str_WSML = str_WSML & "<service name='SoapService'>"
'str_WSML = str_WSML & "<using PROGID='MSOSOAP.GenericCustomTypeMapper30' cachable='0' ID='GCTM'/>"
'str_WSML = str_WSML & "<types>"
'str_WSML = str_WSML & "<type name='GetTranslationsResponse' targetNamespace='http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2' uses='GCTM' targetClassName='struct_GetTranslationsRespo'/>"
'str_WSML = str_WSML & "<type name='TranslateArray2Response' targetNamespace='http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2' uses='GCTM' targetClassName='struct_TranslateArray2Respo'/>"
'str_WSML = str_WSML & "<type name='TranslateArrayResponse' targetNamespace='http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2' uses='GCTM' targetClassName='struct_TranslateArrayRespon'/>"
' str_WSML = str_WSML & "<type name='TranslateOptions' targetNamespace='http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2' uses='GCTM' targetClassName='struct_TranslateOptions'/>"
' str_WSML = str_WSML & "<type name='Translation' targetNamespace='http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2' uses='GCTM' targetClassName='struct_Translation'/>"
' str_WSML = str_WSML & "<type name='TranslationMatch' targetNamespace='http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2' uses='GCTM' targetClassName='struct_TranslationMatch'/>"
'str_WSML = str_WSML & "</types>"
'str_WSML = str_WSML & "</service>"
'str_WSML = str_WSML & "</servicemapping>"
Dim sc_SoapService
set sc_SoapService = server.CreateObject("MSSOAP.SoapClient30")
sc_SoapService.ClientProperty("ServerHTTPRequest") = True
'sc_SoapService.MSSoapInit2 c_WSDL_URL, str_WSML, c_SERVICE, c_PORT, c_SERVICE_NAMESPACE ', str_WSML, c_SERVICE, c_PORT, c_SERVICE_NAMESPACE
sc_SoapService.MSSoapInit c_WSDL_URL, str_WSM'注意这里不能用MSSoapInit2 了
'response.write( sc_SoapService.Detect(appID, "你好"))'可以自动检测原来语言
Content=vbsunescape(request("Content"))
lngfrom=vbsunescape(request("lngfrom"))
lngto=vbsunescape(request("lngto"))
ResponseInfo=sc_SoapService.Translate(appID, Content, lngfrom, lngto, "text/plain", "general")
set sc_SoapService=nothing
end sub
Function VbsUnEscape(str)
x=InStr(str,"%")
Do While x>0
VbsUnEscape=VbsUnEscape&Mid(str,1,x-1)
If LCase(Mid(str,x+1,1))="u" Then
VbsUnEscape=VbsUnEscape&ChrW(CLng("&H"&Mid(str,x+2,4)))
str=Mid(str,x+6)
Else
VbsUnEscape=VbsUnEscape&Chr(CLng("&H"&Mid(str,x+1,2)))
str=Mid(str,x+3)
End If
x=InStr(str,"%")
Loop
VbsUnEscape=VbsUnEscape&str
End Function
%>
运行效果如下