How to call SDL API in .Net in order to do language translation?

I want to convert language (Arabic->English) . My application is developed in .Net. I need to incorporate SDL via the API to the same and there by to do the translation.

Parents
  • Hi Alex,

    API documentation for SDL Language Cloud can be found at https://languagecloud.sdl.com/translation-toolkit/api-documentation

     

    To get started, it’s best to sign up and an SDL Language Cloud account at https://languagecloud.sdl.com/translation-toolkit/sign-up

     

    Once you’re signed up and logged in, navigate to the profile page (here) and check the “Developer Capabilities” box which will give access to the Sandbox environment which can be turned on and off from the user menu (top right). Sandbox allows you to freely develop your application/integration with SDL Language Cloud without having an active subscription. Integrations generally use an “API Key” which can be generated from the Integrations menu on the left. Note that API keys are either Sandbox or Production keys depending on whether you are in Sandbox mode when you create them.

     

    Here’s a .NET example of a text translation call:

     

    namespace LCTestApp

    {

        using System;

        using System.Net.Http;

        using System.Net.Http.Headers;

        using System.Runtime.Serialization.Json;

        using System.Text;

        using System.Xml;

        using System.Xml.Linq;

        using System.Xml.XPath;

     

        class Program

        {

            static void Main(string[] args)

            {

                string apiKey = "[Your API Key Here]";

                string sourceText = "مرحبا بالعالم";

                string sourceLanguage = "ara";

                string targetLanguage = "eng";

                string json = string.Format(@"{{""text"":""{0}"",""from"":""{1}"",""to"":""{2}""}}", sourceText, sourceLanguage, targetLanguage);

                var client = new HttpClient();

                var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://lc-api.sdl.com/translate");

                requestMessage.Headers.Add("Accept", "application/json");

                requestMessage.Headers.Add("Authorization", string.Format("LC apiKey={0}", apiKey));

                requestMessage.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(json));

                requestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=UTF-8");

                HttpResponseMessage response = client.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead).Result;

                if (response.IsSuccessStatusCode)

                {

                    byte[] responseBytes = response.Content.ReadAsByteArrayAsync().Result;

                    XmlReader reader = JsonReaderWriterFactory.CreateJsonReader(responseBytes, new XmlDictionaryReaderQuotas());

                    XElement root = XElement.Load(reader);

                    XElement translation = root.XPathSelectElement("//translation");

                    Console.WriteLine(translation.Value);

                }

                else

                {

                    Console.WriteLine("#ERROR: " + Encoding.UTF8.GetString(response.Content.ReadAsByteArrayAsync().Result));

                }

     

                Console.ReadKey();

            }

        }

    }

Reply
  • Hi Alex,

    API documentation for SDL Language Cloud can be found at https://languagecloud.sdl.com/translation-toolkit/api-documentation

     

    To get started, it’s best to sign up and an SDL Language Cloud account at https://languagecloud.sdl.com/translation-toolkit/sign-up

     

    Once you’re signed up and logged in, navigate to the profile page (here) and check the “Developer Capabilities” box which will give access to the Sandbox environment which can be turned on and off from the user menu (top right). Sandbox allows you to freely develop your application/integration with SDL Language Cloud without having an active subscription. Integrations generally use an “API Key” which can be generated from the Integrations menu on the left. Note that API keys are either Sandbox or Production keys depending on whether you are in Sandbox mode when you create them.

     

    Here’s a .NET example of a text translation call:

     

    namespace LCTestApp

    {

        using System;

        using System.Net.Http;

        using System.Net.Http.Headers;

        using System.Runtime.Serialization.Json;

        using System.Text;

        using System.Xml;

        using System.Xml.Linq;

        using System.Xml.XPath;

     

        class Program

        {

            static void Main(string[] args)

            {

                string apiKey = "[Your API Key Here]";

                string sourceText = "مرحبا بالعالم";

                string sourceLanguage = "ara";

                string targetLanguage = "eng";

                string json = string.Format(@"{{""text"":""{0}"",""from"":""{1}"",""to"":""{2}""}}", sourceText, sourceLanguage, targetLanguage);

                var client = new HttpClient();

                var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://lc-api.sdl.com/translate");

                requestMessage.Headers.Add("Accept", "application/json");

                requestMessage.Headers.Add("Authorization", string.Format("LC apiKey={0}", apiKey));

                requestMessage.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(json));

                requestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=UTF-8");

                HttpResponseMessage response = client.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead).Result;

                if (response.IsSuccessStatusCode)

                {

                    byte[] responseBytes = response.Content.ReadAsByteArrayAsync().Result;

                    XmlReader reader = JsonReaderWriterFactory.CreateJsonReader(responseBytes, new XmlDictionaryReaderQuotas());

                    XElement root = XElement.Load(reader);

                    XElement translation = root.XPathSelectElement("//translation");

                    Console.WriteLine(translation.Value);

                }

                else

                {

                    Console.WriteLine("#ERROR: " + Encoding.UTF8.GetString(response.Content.ReadAsByteArrayAsync().Result));

                }

     

                Console.ReadKey();

            }

        }

    }

Children
No Data