Skip to content

Instantly share code, notes, and snippets.

@whatsmate
Created March 4, 2026 07:46
Show Gist options
  • Select an option

  • Save whatsmate/a6e04ab66180d7283cc2c24e006b7b08 to your computer and use it in GitHub Desktop.

Select an option

Save whatsmate/a6e04ab66180d7283cc2c24e006b7b08 to your computer and use it in GitHub Desktop.
Translating natural languages in VB.NET using VS2022
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Text.Json
Public Class Translator
''' TODO: If you have your own Premium account credentials, put them down here:
Private Const CLIENT_ID As String = "FREE_TRIAL_ACCOUNT"
Private Const CLIENT_SECRET As String = "PUBLIC_SECRET"
Private Const API_URL As String = "http://api.whatsmate.net/v1/translation/translate"
Public Function translate(ByVal fromLang As String, ByVal toLang As String, ByVal text As String) As Boolean
Dim success As Boolean = True
Try
Dim httpRequest As HttpWebRequest = DirectCast(WebRequest.Create(API_URL), HttpWebRequest)
httpRequest.Method = "POST"
httpRequest.Accept = "application/json"
httpRequest.ContentType = "application/json"
httpRequest.Headers("X-WM-CLIENT-ID") = CLIENT_ID
httpRequest.Headers("X-WM-CLIENT-SECRET") = CLIENT_SECRET
Dim payloadObj As New Payload(fromLang, toLang, text)
Dim postData As String = JsonSerializer.Serialize(payloadObj)
Using streamWriter As New StreamWriter(httpRequest.GetRequestStream())
streamWriter.Write(postData)
End Using
Dim httpResponse As HttpWebResponse = DirectCast(httpRequest.GetResponse(), HttpWebResponse)
Using streamReader As New StreamReader(httpResponse.GetResponseStream())
Dim result As String = streamReader.ReadToEnd()
Console.WriteLine(result)
End Using
Catch webExcp As WebException
Console.WriteLine("A WebException has been caught.")
Console.WriteLine(webExcp.ToString())
Dim status As WebExceptionStatus = webExcp.Status
If status = WebExceptionStatus.ProtocolError Then
Console.Write("The REST API server returned a protocol error: ")
Dim httpResponse As HttpWebResponse = TryCast(webExcp.Response, HttpWebResponse)
Dim stream As Stream = httpResponse.GetResponseStream()
Dim reader As New StreamReader(stream)
Dim body As String = reader.ReadToEnd()
Console.WriteLine(CInt(httpResponse.StatusCode) & " - " & body)
success = False
End If
Catch e As Exception
Console.WriteLine("A general exception has been caught.")
Console.WriteLine(e.ToString())
success = False
End Try
Return success
End Function
Private Class Payload
Public Property fromLang As String
Public Property toLang As String
Public Property text As String
Sub New(ByVal fromLangCode As String, ByVal toLangCode As String, ByVal originalText As String)
fromLang = fromLangCode
toLang = toLangCode
text = originalText
End Sub
End Class
End Class
Module Module1
Sub Main()
' TODO: Specify your translation requirements here:
Dim fromLang As String = "en"
Dim toLang As String = "es"
Dim text As String = "Let's have fun!"
Dim textTranslator As New Translator()
textTranslator.translate(fromLang, toLang, text)
Console.WriteLine("Press Enter to exit")
Console.ReadLine()
End Sub
End Module
@whatsmate
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment