0

使用 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 用于印度尼西亚语。

4

1 回答 1

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 表示“来自西班牙语”

因此,只需针对您需要的语言进行修改...

于 2013-04-19T01:04:47.847 回答