用于多个文本框的Microsoft Translator?

时间:2012-06-18 15:39:02

标签: asp.net vb.net localization language-translation microsoft-translator

使用Microsoft Bing转换器在不同语言的3个标签上显示输出时出现问题。

这是我的代码:

Imports System
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml.Linq

Public Class AdmAccessToken
    Public Property access_token() As String
        Get
            Return m_access_token
        End Get
        Set(ByVal value As String)
            m_access_token = value
        End Set
    End Property
    Private m_access_token As String
    Public Property token_type() As String
        Get
            Return m_token_type
        End Get
        Set(ByVal value As String)
            m_token_type = value
        End Set
    End Property
    Private m_token_type As String
    Public Property expires_in() As String
        Get
            Return m_expires_in
        End Get
        Set(ByVal value As String)
            m_expires_in = value
        End Set
    End Property
    Private m_expires_in As String
    Public Property scope() As String
        Get
            Return m_scope
        End Get
        Set(ByVal value As String)
            m_scope = value
        End Set
    End Property
    Private m_scope As String
End Class

Partial Class translated
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        'Button1.Click += New EventHandler(Button1_Click1)
    End Sub

    Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim clientID As String = "*******"
        Dim clientSecret As String = "************"

        Dim strTranslatorAccessURI As String = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
        Dim strRequestDetails As String = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientID), HttpUtility.UrlEncode(clientSecret))

        Dim webRequest As System.Net.WebRequest = System.Net.WebRequest.Create(strTranslatorAccessURI)
        webRequest.ContentType = "application/x-www-form-urlencoded"
        webRequest.Method = "POST"

        Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes(strRequestDetails)
        webRequest.ContentLength = bytes.Length
        Using outputStream As System.IO.Stream = webRequest.GetRequestStream()
            outputStream.Write(bytes, 0, bytes.Length)
        End Using
        Dim webResponse As System.Net.WebResponse = webRequest.GetResponse()

        Dim serializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(AdmAccessToken))
        Dim token As AdmAccessToken = DirectCast(serializer.ReadObject(webResponse.GetResponseStream()), AdmAccessToken)
        Dim headerValue As String = "Bearer " + token.access_token

        Dim txtToTranslate As String = TextBox1.Text
        Dim uri As String = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(txtToTranslate) + "&from=en&to=es"
        Dim translationWebRequest As System.Net.WebRequest = System.Net.WebRequest.Create(uri)
        translationWebRequest.Headers.Add("Authorization", headerValue)
        Dim response As System.Net.WebResponse = Nothing
        response = translationWebRequest.GetResponse()
        Dim stream As System.IO.Stream = response.GetResponseStream()
        Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")
        Dim translatedStream As New System.IO.StreamReader(stream, encode)
        Dim xTranslation As New System.Xml.XmlDocument()
        xTranslation.LoadXml(translatedStream.ReadToEnd())
        Label1.Text = "Your Translation is: " + xTranslation.InnerText
    End Sub
End Class

有人可以提供一些建议吗?

我只需要知道如何将翻译文本翻译成3种不同的标签语言:标签1表示荷兰语,标签2表示英语,标签3表示印尼语。

1 个答案:

答案 0 :(得分:2)

您示例中的最后一段代码执行从英语到西班牙语的翻译。 (以Dim txtToTranslate开头的部分......)

你必须使用它3次(把它放在一个函数中),一次从目标语言翻译成荷兰语,一次翻译成英语,一次翻译成印度尼西亚语。

指定翻译的部分是:

Dim uri As String = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(txtToTranslate) + "&from=en&to=es"

From = en表示'来自英语' To = es表示'到西班牙语'

所以只需修改你需要的语言......