Created
March 4, 2026 04:48
-
-
Save whatsmate/6346ea47a163b4cc502bc306e09721cf to your computer and use it in GitHub Desktop.
Translating natural languages in C# using VS2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Net; | |
| using System.Text.Json; | |
| using System.IO; | |
| using System.Text; | |
| class Translator | |
| { | |
| // TODO: If you have your own Premium account credentials, put them down here: | |
| private static string CLIENT_ID = "FREE_TRIAL_ACCOUNT"; | |
| private static string CLIENT_SECRET = "PUBLIC_SECRET"; | |
| private static string API_URL = "https://api.whatsmate.net/v1/translation/translate"; | |
| static void Main(string[] args) | |
| { | |
| Translator textTranslator = new Translator(); | |
| // TODO: Specify your translation requirements here: | |
| string fromLang = "en"; | |
| string toLang = "es"; | |
| string text = "Let's have fun!"; | |
| textTranslator.translate(fromLang, toLang, text); | |
| Console.WriteLine("Press Enter to exit."); | |
| Console.ReadLine(); | |
| } | |
| public bool translate(string fromLang, string toLang, string text) | |
| { | |
| bool success = true; | |
| try | |
| { | |
| HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(API_URL); | |
| 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; | |
| Payload payloadObj = new Payload() { fromLang = fromLang, toLang = toLang, text = text }; | |
| string postData = JsonSerializer.Serialize(payloadObj); | |
| using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream())) | |
| { | |
| streamWriter.Write(postData); | |
| } | |
| var httpResponse = (HttpWebResponse)httpRequest.GetResponse(); | |
| using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) | |
| { | |
| var result = streamReader.ReadToEnd(); | |
| Console.WriteLine(result); | |
| } | |
| } | |
| catch (WebException webExcp) | |
| { | |
| Console.WriteLine("A WebException has been caught."); | |
| Console.WriteLine(webExcp.ToString()); | |
| WebExceptionStatus status = webExcp.Status; | |
| if (status == WebExceptionStatus.ProtocolError) | |
| { | |
| Console.Write("The REST API server returned a protocol error: "); | |
| HttpWebResponse? httpResponse = webExcp.Response as HttpWebResponse; | |
| Stream stream = httpResponse.GetResponseStream(); | |
| StreamReader reader = new StreamReader(stream); | |
| String body = reader.ReadToEnd(); | |
| Console.WriteLine((int)httpResponse.StatusCode + " - " + body); | |
| success = false; | |
| } | |
| } | |
| catch (Exception e) | |
| { | |
| Console.WriteLine("A general exception has been caught."); | |
| Console.WriteLine(e.ToString()); | |
| success = false; | |
| } | |
| return success; | |
| } | |
| public class Payload | |
| { | |
| public string fromLang { get; set; } | |
| public string toLang { get; set; } | |
| public string text { get; set; } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://whatsmate.github.io/2026-03-03-translate-text-csharp-vs2022/