Reputation: 21
i tried to translate the word "test",
my code:
Imports Google.API.Translate
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "test"
Dim gtras As New TranslateClient("MY API KEY")
Label1.Text = gtras.Translate(TextBox1.Text, Language.English, Language.Hebrew, TranslateFormat.Text)
End Sub
End Class
the exception is:
Attempt by method 'Google_Translator.Form1.Button1_Click(System.Object, System.EventArgs)' to access method 'Google.API.Enumeration.op_Implicit(Google.API.Enumeration)' failed.
Upvotes: 1
Views: 10739
Reputation: 1
Fix Encoding And update code
Public Shared Function gtranslate(ByVal inputtext As String, ByVal fromlangid As String, ByVal tolangid As String) As String
inputtext = HttpUtility.HtmlAttributeEncode(inputtext)
Dim step1 As New WebClient
step1.Encoding = Encoding.UTF8
Dim step2 As String = step1.DownloadString("https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=" & tolangid & "&hl=" & fromlangid & "&dt=t&dt=bd&dj=1&source=icon&q=" & inputtext)
Dim step3 As Newtonsoft.Json.Linq.JObject = JObject.Parse(step2)
Dim step4 As String = step3.SelectToken("sentences[0]").SelectToken("trans").ToString()
Return step4
End Function
Add : step1.Encoding = Encoding.UTF8
Upvotes: 0
Reputation: 127
This code works very well:
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System
Imports System.Net
Imports System.Text
Imports System.Web
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(gtranslate(TextBox1.Text, "it", "en"))'From italian to english, of course you can change it
End Sub
Public Shared Function gtranslate(ByVal inputtext As String, ByVal fromlangid As String, ByVal tolangid As String) As String
inputtext = HttpUtility.HtmlAttributeEncode(inputtext)
Dim step1 As New WebClient
Dim step2 As String = step1.DownloadString("https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=" & tolangid & "&hl=" & fromlangid & "&dt=t&dt=bd&dj=1&source=icon&q=" & inputtext)
Dim step3 As Newtonsoft.Json.Linq.JObject = JObject.Parse(step2)
Dim step4 As String = step3.SelectToken("sentences[0]").SelectToken("trans").ToString()
Return step4
End Function
End Class
The dll library "Newtonsoft.JSON" is available from their website and, if you get some errors with "HttpUtility", add the reference "System.Web" in the add reference window -> .net tab
Upvotes: 0
Reputation: 65554
This issue has been fixed in a later version of the API (0.3.1 seems to work):
http://code.google.com/p/google-api-for-dotnet/issues/detail?id=43
Upvotes: 1