Question

I get problem when use Microsoft Bing translator for show output on 3 labels for different languages.

Here is my code :

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

Can anyone give some advice?

I just need to know how I can get translated text to 3 different label language: label 1 for dutch language, label 2 for english language, and label 3 for indonesian language.

Était-ce utile?

La solution

The final chunk of code in your example does the translation from English to Spanish. (The part that starts with Dim txtToTranslate...)

You will have to just use that 3 times (put it in a function) with once doing a translation from your target language to Dutch, once to English, once to Indonesian.

The part where the translation is specified is this:

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

From=en means 'from English' To=es means 'to Spanish'

So just amend that for the languages you need...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top