Advertisement
struff

vsbc

Sep 25th, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function TranslateText(Text As String, SourceLanguage As String, TargetLanguage As String) As String
  2.     Dim url As String
  3.     Dim Http As Object
  4.     Dim Json As Object
  5.    
  6.     ' Crear la URL para la API de Google Translate
  7.    url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" & SourceLanguage & "&tl=" & TargetLanguage & "&dt=t&q=" & URLEncode(Text)
  8.    
  9.     ' Crear el objeto HTTP para hacer la solicitud
  10.    Set Http = CreateObject("MSXML2.XMLHTTP")
  11.     Http.Open "GET", url, False
  12.     Http.Send
  13.    
  14.     ' Procesar la respuesta JSON
  15.    TranslateText = Mid(Http.responseText, 5, InStr(Http.responseText, Chr(34)) - 5)
  16. End Function
  17.  
  18. ' Función para codificar la URL
  19. Function URLEncode(StringVal As String) As String
  20.     Dim i As Long
  21.     Dim CharCode As Integer
  22.     Dim Char As String
  23.     URLEncode = ""
  24.     For i = 1 To Len(StringVal)
  25.         Char = Mid(StringVal, i, 1)
  26.         CharCode = Asc(Char)
  27.         If (CharCode >= 48 And CharCode <= 57) Or _
  28.            (CharCode >= 65 And CharCode <= 90) Or _
  29.            (CharCode >= 97 And CharCode <= 122) Then
  30.             URLEncode = URLEncode & Char
  31.         Else
  32.             URLEncode = URLEncode & "%" & Hex(CharCode)
  33.         End If
  34.     Next i
  35. End Function
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement