Option Explicit
'************************************************************
'formule BASE *
'=Translate2(A1;"fr";"en";2)'conversion hexa *
'***********************************************************
Sub test()
MsgBox Translate2("bonjour tout le monde", "fr", "en", 2)
End Sub
Public Function Translate2(Optional SendText As String, Optional From As String = "en", Optional ToLang As String = "fr", Optional Convert = 0)
Dim RQ As Object, URL$, elem As Object, X&
Set RQ = CreateObject("microsoft.xmlhttp") '"MSXML2.ServerXMLHTTP"
If Convert <> 0 Then If Convert = 1 Then SendText = EncodeText1(SendText) Else SendText = EncodeText2(SendText)
URL = "https://translate.google.pl/m?hl=" & From & "&sl=" & From & "&tl=" & ToLang & "&ie=UTF-8&prev=_m&q=" & SendText
RQ.Open "POST", URL, False
RQ.SetRequestheader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
RQ.send
With CreateObject("htmlfile")
.body.innerhtml = RQ.responsetext
For Each elem In .ALL
If elem.Tagname = "DIV" And elem.classname = "t0" Then Translate2 = elem.innerhtml: Exit For
Next
End With
Debug.Print URL
End Function
Function EncodeText2(ByVal Texte As String) As String
Dim P&, C$, A&
For P = 1 To Len(Texte)
C = Mid$(Texte, P, 1): A = AscW(C)
Select Case A
'Case 32: C = "+"'pour ceux qui préfèrent "+" au lieu de "%20"
Case Is > 127: C = "\u" & Right$("000" & Hex$(A), 4)
Case 0 To 44, 47, 58 To 64, 91 To 94, 96, 123 To 127: C = "%" & Right$("0" & Hex$(A), 2)
End Select
EncodeText2 = EncodeText2 & C
Next P
End Function