1

我试图翻译“测试”这个词,
我的代码:

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

例外是:

Attempt by method 'Google_Translator.Form1.Button1_Click(System.Object, System.EventArgs)' to access method 'Google.API.Enumeration.op_Implicit(Google.API.Enumeration)' failed.
4

3 回答 3

1

此问题已在更高版本的 API 中得到修复(0.3.1 似乎可以工作):

http://code.google.com/p/google-api-for-dotnet/issues/detail?id=43

于 2012-08-07T04:12:57.520 回答
0

修复编码和更新代码

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

添加:step1.Encoding = Encoding.UTF8

于 2018-12-26T11:12:19.543 回答
0

这段代码效果很好:

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

dll 库“Newtonsoft.JSON”可从他们的网站获得,如果“HttpUtility”出现一些错误,请在添加参考窗口 -> .net 选项卡中添加参考“System.Web”

于 2016-11-23T18:19:38.190 回答