从ASP.NET使用Post调用Google Translate
使用POST从ASP.NET调用Google Translate从您的代码中获取更长的文档。
介绍
Google Translate API对于执行相当复杂的翻译是有好处的,虽然它不完美,但至少可以让读者对您正在尝试传达的内容有一个基本的了解。
虽然该文档很好地解释了当您使用可以翻译大约500个字符的Java Script时会发生什么,但是当您需要翻译更大的文档时,几乎没有什么。
使用POST时,您可以将其增加到5000个字符,因此我们开发了自己的代码,将发送请求发送到Google API,然后接收翻译。
首先是代码,对于那些只是想要的。
VB
Protected Function GetTranslation(ByVal key As String, ByVal source As String, ByVal target As String, ByVal Text As String) As String
Dim TranslatedString As String = ""Text = "q=" + Text
Dim TranslateRequest As New Uri(String.Format("https://www.googleapis.com/language/translate/v2?key={0}&source={1}&target={2}&format=html", key, source, target))
Dim Req As WebRequest = WebRequest.Create(TranslateRequest)
Req.Method = "POST"
Req.Headers.Add("X-https-Method-Override", "GET")
Req.ContentType = "application/x-www-form-urlencoded"
Using wriream As Stream = Req.GetRequestStream()
Dim encoding As New UTF8Encoding()
Dim bytes As Byte() = encoding.GetBytes(Text)
wriream.Write(bytes, 0, bytes.Length)
End Using
Dim ReturnStr As String
Using sr = New StreamReader(Req.GetResponse.GetResponseStream)
ReturnStr = sr.ReadToEnd()
End Using
Dim Reader As New JavaScriptSerializer
Dim TranslateJSON As Dictionary(Of String, Object) = Reader.DeserializeObject(ReturnStr)
Dim TranslateData As Dictionary(Of String, Object)
If TranslateJSON.ContainsKey("data") Then
TranslateData = TranslateJSON("data")
If TranslateData.ContainsKey("translations") Then
For Each pair In TranslateData.Item("translations")(0)
TranslatedString = pair.Value.ToString()
Next
End If
End If
Return TranslatedString
End Function
介绍
现在简要说明一下。该功能需要四个输入,这些是您的键,语言,语言和要翻译的文本。
然后,我们声明一个返回字符串,创建一个快速解析为新的Web请求(Req)的请求URL字符串。
然后我们设置请求类型,内容类型,最重要的是添加一个头来覆盖get方法 。
一旦完成,我们将数据作为流传送给Google(wriream)。现在我们声明一个返回字符串(ReturnStr)来保存从Google返回的JSON,并将响应字符串读入它。
下一步是创建一个JavaScriptSerializer,这可能是最令人困惑的,因为这是我开发技能最薄弱的领域。这最后一节是拉出文本的每一部分,直到它到达我们想要的区域,并将我们的返回文本设置为Google返回的值。这可能不是世界上最精细的代码,所以如果你想出一个整理的方法,那么让我知道。
你可以很容易地将这段代码放在一个共享的类中并重新使用它,但是我们只需要一个这样的站点,所以它被内置到页面中。
下面的示例使用另一个AJAX Toolkit文本编辑器填充内容。请注意双重解码(来自编辑器和Google),并且有两个文本框表示语言来往。
VB
Protected Sub TranslateDoc_Click(sender As Object, e As EventArgs) Handles TranslateDoc.Click
Dim key As String = "Your Key"
Dim source As String = LanguageBase.SelectedItem.Text.ToString
Dim target As String = LanguageTrans.SelectedItem.Text.ToString
Dim PageText As String = httpsUtility.HtmlDecode(ContentText.Content)
Try
ContentTextTran.Content = httpsUtility.HtmlDecode(GetTranslation(key, source, target, PageText))
Str.Text = "Translated"
Catch
Str.Text = key + "," + source + "," + target + ""
End Try
End Sub
Dim key As String = "Your Key"
Dim source As String = LanguageBase.SelectedItem.Text.ToString
Dim target As String = LanguageTrans.SelectedItem.Text.ToString
Dim PageText As String = httpsUtility.HtmlDecode(ContentText.Content)
Try
ContentTextTran.Content = httpsUtility.HtmlDecode(GetTranslation(key, source, target, PageText))
Str.Text = "Translated"
Catch
Str.Text = key + "," + source + "," + target + ""
End Try
End Sub