Get Translator Text API Supported Languages

Spread the love
In this tutorial we are going to see how to get the Translator Text API supported languages

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 Languages()
            {
                string host = "https://api.cognitive.microsofttranslator.com";
                string route = "/languages?api-version=3.0";
                string subscriptionKey = "enter your subscription key";
    
                using (var client = new HttpClient())
                using (var request = new HttpRequestMessage())
                {
                    request.Method = HttpMethod.Get;
                    request.RequestUri = new Uri(host + route);
                    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)
            {
                Languages();
                Console.ReadLine();
            }
  4. Replace your subscription key here  string subscriptionKey = “enter your subscription key”;
  5. Run the program

Get the Results

The results are a json string in the following format. The first node is the API and inside you can find all the supported languages for this API.

{
  "translation": {
    "af": {
      "name": "Afrikaans",
      "nativeName": "Afrikaans",
      "dir": "ltr"
    },
    "ar": {
      "name": "Arabic",
      "nativeName": "العربية",
      "dir": "rtl"
    },
    ...
  },
  "transliteration": {
    "ar": {
      "name": "Arabic",
      "nativeName": "العربية",
      "scripts": [{
          "code": "Arab",
          "name": "Arabic",
          "nativeName": "العربية",
          "dir": "rtl",
          "toScripts": [{
            "code": "Latn",
            "name": "Latin",
            "nativeName": "اللاتينية",
            "dir": "ltr"
          }]
        },
        {
          "code": "Latn",
          "name": "Latin",
          "nativeName": "اللاتينية",
          "dir": "ltr",
          "toScripts": [{
            "code": "Arab",
            "name": "Arabic",
            "nativeName": "العربية",
            "dir": "rtl"
          }]
        }
      ]
    },
    ...
  },
  "dictionary": {
    "af": {
      "name": "Afrikaans",
      "nativeName": "Afrikaans",
      "dir": "ltr",
      "translations": [{
        "name": "English",
        "nativeName": "English",
        "dir": "ltr",
        "code": "en"
      }]
    },
    "ar": {
      "name": "Arabic",
      "nativeName": "العربية",
      "dir": "rtl",
      "translations": [{
        "name": "English",
        "nativeName": "English",
        "dir": "ltr",
        "code": "en"
      }]
    },
    ...
  }
}

 

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

Leave a Reply

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