Public Function LangTranslate(ByVal text As String, ByVal codeTo As String) As String
If codeTo.Trim() <> String.Empty Then
Dim APP_ID As String = "D52A3656551542D3BA22D2D8778B813A4EA4431F"
Dim URL_Detect As String = "http://api.microsofttranslator.com/V1/Http.svc/Detect?appId={0}"
Dim URL_Translate As String = "http://api.microsofttranslator.com/V1/Http.svc/Translate?appId={0}&from={1}&to={2}"
Dim URL_GetLanguages As String = "http://api.microsofttranslator.com/V1/Http.svc/GetLanguages?appId={0}"
Dim URL_GetLanguageNames As String = "http://api.microsofttranslator.com/V1/Http.svc/GetLanguageNames?appId={0}"
Dim codeFrom As String = "en"
Dim translatedText As String
Dim translateUri As String = String.Format(URL_Translate, APP_ID, codeFrom, codeTo)
Dim httpWebRequest As HttpWebRequest = CType(WebRequest.Create(translateUri), HttpWebRequest)
httpWebRequest.Method = "POST"
httpWebRequest.ContentType = "text/plain"
httpWebRequest.Timeout = 4000
httpWebRequest.ReadWriteTimeout = 2000
Dim bytes() As Byte = Encoding.UTF8.GetBytes(text)
Try
httpWebRequest.ContentLength = bytes.Length
Dim os As Stream = httpWebRequest.GetRequestStream
os.Write(bytes, 0, bytes.Length)
Dim response As WebResponse = httpWebRequest.GetResponse
Dim reader As StreamReader = New StreamReader(response.GetResponseStream)
translatedText = reader.ReadToEnd
Return translatedText
Catch ex As Exception
Return String.Empty
End Try
End If
End Function
Private Sub ChangeLanguageControls()
Dim lang As String = hdnLang.Value.ToString()
Dim stbuilder As New StringBuilder()
stbuilder.Clear()
stbuilder.Append(lblContainer.Text & ";" & lblLocation.Text & ";" & lblSupplier.Text & ";")
stbuilder.Append(btnSearch.Text & ";")
Dim cntClmns As Integer = 0
cntClmns = grdContainer.Columns.Count - 1
For i As Integer = 0 To cntClmns
stbuilder.Append(grdContainer.Columns(i).HeaderText & ";")
Next
Dim translated() As String
Dim trans As String
Dim source() As String
source = stbuilder.ToString().Split(";")
trans = lt.LangTranslate(stbuilder.ToString(), lang)
translated = trans.Split(";")
Dim ar As Integer = 0
lblContainer.Text = translated(ar)
lblLocation.Text = translated(ar + 1)
lblSupplier.Text = translated(ar + 2)
btnSearch.Text = translated(ar + 3)
Dim cntClmns1 As Integer = 0
cntClmns1 = grdContainer.Columns.Count - 1
For i As Integer = 0 To cntClmns1
grdContainer.Columns(i).HeaderText = translated(ar + 4 + i)
Next
End Sub
No comments:
Post a Comment