Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function TranslateText(Text As String, SourceLanguage As String, TargetLanguage As String) As String
- Dim url As String
- Dim Http As Object
- Dim Json As Object
- ' Crear la URL para la API de Google Translate
- url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" & SourceLanguage & "&tl=" & TargetLanguage & "&dt=t&q=" & URLEncode(Text)
- ' Crear el objeto HTTP para hacer la solicitud
- Set Http = CreateObject("MSXML2.XMLHTTP")
- Http.Open "GET", url, False
- Http.Send
- ' Procesar la respuesta JSON
- TranslateText = Mid(Http.responseText, 5, InStr(Http.responseText, Chr(34)) - 5)
- End Function
- ' Función para codificar la URL
- Function URLEncode(StringVal As String) As String
- Dim i As Long
- Dim CharCode As Integer
- Dim Char As String
- URLEncode = ""
- For i = 1 To Len(StringVal)
- Char = Mid(StringVal, i, 1)
- CharCode = Asc(Char)
- If (CharCode >= 48 And CharCode <= 57) Or _
- (CharCode >= 65 And CharCode <= 90) Or _
- (CharCode >= 97 And CharCode <= 122) Then
- URLEncode = URLEncode & Char
- Else
- URLEncode = URLEncode & "%" & Hex(CharCode)
- End If
- Next i
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement