实施 google 翻译

Implement google translation

我已经尝试使用 google translate 将名称从语言翻译成另一种语言,使用 c# 并且它工作正常但现在我尝试使用 ms access vba 做同样的事情我已经尝试了很多方法但没有运气! 这是用 C# 编写的代码,运行良好

public string trans_arabic_to_english(string word)
        {
            var toLanguage = "en";//English
            var fromLanguage = "ar";//Deutsch
            var url = $"https://translate.googleapis.com/translate_a/single?client=gtx&sl={fromLanguage}&tl={toLanguage}&dt=t&q={HttpUtility.UrlEncode(word)}";
            var webClient = new WebClient
            {
                Encoding = System.Text.Encoding.UTF8
            };
            var result = webClient.DownloadString(url);
            try
            {
                result = result.Substring(4, result.IndexOf("\"", 4, StringComparison.Ordinal) - 4);
                return result;
            }
            catch
            {
                return "Error";
            }
        }

这是vba

中的代码
Private Sub Command0_Click()
Dim toLan As String, fromLan As String, resp As String, s As String, a_name As String, url As String

toLang = "ar"
fromlang = "en"
a_name = "omar khalil"
url = "https://translate.googleapis.com/translate_a/single?"
url = url & "client=gtx&sl={""" & toLang & """}&tl={""" & fromlang & """}&dt=t&q={""" & a_name & """}"

url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl="
url = url & fromlang & "&tl=" & to_lang & "&dt=t&q=" & a_name
'==
Dim ob As Object
Set ob = CreateObject("WinHttp.WinHttpRequest.5.1")
ob.Open "POST", url, False
ob.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
ob.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
ob.Send
If ob.Status <> 200 Then
resp = ob.ResponseText
MsgBox resp
End Sub

我也尝试过使用 WinHttp,但一点运气都没有! 任何人都可以帮助我解决这个问题谢谢。

请尝试下一个功能:

Private Function GTranslate(strInput As String, strFromLang As String, strToLang As String) As String
    Dim strURL As String, objHTTP As Object, objHTML As Object, objDivs As Object, objDiv As Variant
    
    strURL = "https://translate.google.com/m?hl=" & strFromLang & _
        "&sl=" & strFromLang & _
        "&tl=" & strToLang & _
        "&ie=UTF-8&prev=_m&q=" & strInput
        
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    objHTTP.Open "GET", strURL, False
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.Send ""
    
    Set objHTML = CreateObject("htmlfile")
    With objHTML
        .Open
        .Write objHTTP.responseText
        .Close
    End With
    
    Set objDivs = objHTML.getElementsByTagName("div")
    For Each objDiv In objDivs
        'If objDiv.className = "t0" Then              'it does not work, anymore
        If objDiv.className = "result-container" Then 'adapted on December 28th
            GTranslate = objDiv.innerText: Exit For
        End If
    Next objDiv
    
    Set objHTML = Nothing: Set objHTTP = Nothing
End Function

可以通过以下方式调用:

Sub testTranslateG()
  MsgBox GTranslate("Este es un libro", "auto", "en")
End Sub

第二个参数可以是“auto”(就像在测试 Sub 中一样),或者如果需要更准确的翻译,则可以是特定语言的缩写(“es” - 西班牙语,“ru” - 俄语,“ro” - 罗马尼亚语等) .).

为了找到正确的缩写,您可以打开Google翻译页面,右击并选择'View page source'。然后尝试寻找一些语言。让我们说西班牙语。在该区域中,您将看到一个脚本,其中包含如下字符串:“code:'it',name:'Italian'”。通俗易懂,“it”是意大利语的缩写...