Use the Translator Text API to Translate text

Spread the love
In this tutorial we are going to see how to use the Translator Text API to translate Text from English to Portuguese.

Prerequisites

  1. To run the sample code you must have an edition of  Visual Studio installed.
  2. You will need the Json.NET NuGet package.
  3. You will need the .NET SDK installed in your machine
  4. You will need an Azure Cognitive Services account with a Translator Text resource. If you don’t have an account, you can use the free trial to get a subscription key.

Create your Project

To create an application to translate your text follow the steps below:

  1. Create a .NET Core Console Application in Visual Studio 2017
  2. Add the JSON.net nuget package
    Install-Package Newtonsoft.Json
    
  3. Add the following code under Program
    static void Translate()
           {
               string SourceLanguage = "en";
               string DestinationLanguage = "pt";
               string host = "https://api.cognitive.microsofttranslator.com";
               string route = "/translate?api-version=3.0&to="+ SourceLanguage + "&to="+ DestinationLanguage;
               string subscriptionKey = "enter your subscription key";
    
               System.Object[] body = new System.Object[] { new { Text = @"Hello this is a CodeStories post." } };
               var requestBody = JsonConvert.SerializeObject(body);
    
               using (var client = new HttpClient())
               using (var request = new HttpRequestMessage())
               {
                   request.Method = HttpMethod.Post;
                   request.RequestUri = new Uri(host + route);
                   request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                   request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
                   var response = client.SendAsync(request).Result;
                   var jsonResponse = response.Content.ReadAsStringAsync().Result;
                   Console.WriteLine(jsonResponse);
                   Console.WriteLine("Press any key to continue.");
               }
           }
           static void Main(string[] args)
           {
               Translate();
               Console.ReadLine();
           }
  4. Replace the parameters Source Language and Destination Language. To get supported languages use the Get Languages API. See the tutorial here.
  5. Replace your subscription key here: string subscriptionKey = “enter your subscription key”;
  6. Add here the text you want to be translated System.Object[] body = new System.Object[] { new { Text = @”Hello this is a CodeStories post.” } };
  7. Run the Program

Get Results

The result is in the following format. That’s it we have translated the english text to portuguese.

[{"detectedLanguage":{"language":"en","score":1.0},"translations":[{"text":"Hello this is a CodeStories post.","to":"en"},{"text":"Olá este é um post CodeStories.","to":"pt"}]}]

You can find the complete source code in my Github in this repository in the TextTranslate Project.

Leave a Reply

Your email address will not be published. Required fields are marked *