Cognitive Services

14.08.2018 von robert@riwa4.de
/blog/artikel/translator

Translator Text API

This web page certainly doesn't have an international audience, but nevertheless I took a look at the Translator Text API of Azure Cognitive Services and incorporated it here.

Deutsch Français Espagnol Portuguese
Cognitive Services

You can now have the text translated into different languages via a new button in the blog and when displaying tours (i.e. bike rides, trips, hikes). To do this, I call an Azure Function to be able to use this in general. The function then finally calls the actual Text API. 

The calls are all REST-based and expect and return JSON objects. To do this, I use the excellent Flurl library So a typical call looks like this:

System.Object[] body = new System.Object[] { new { Text = textToTranslate } };

var response = await $"https://api.cognitive.microsofttranslator.com/translate?api-version=3.0"
                     .WithHeader("Ocp-Apim-Subscription-Key", GetEnvironmentVariable("TranslatorApiKey"))
                     .SetQueryParam("to", to)
                     .SetQueryParam("textType", "html")
                     .SetQueryParam("from", "de")
                     .PostJsonAsync(body)
                     .ReceiveJsonList();
return new JsonResult(response);

As far as I can tell, the translations are quite good. The automatic recognition of the source language did not work so well. But ok, I'm only translating from German into another language here. What is very convenient: You can translate HTML text, whereby the HTML structure of the text is then preserved. 

By the way, I use the free plan, which allows you to translate texts with a total of up to 2,000,000 letters per month. That's enough to try it out.