Most of the global teams are spread out in many countries. That’s adds a very interesting aspect to all our bots/app

Most of the global teams are spread out in many countries. That’s adds a very interesting aspect to all our bots/app implementations which need multilingual support

In this blog, we will look how to use Azure AI which can provide multilingual support for apps and bots. This will be useful in many scenarios, some of which are:

  1. Language agnostic bots that works across multiple regions
  2. Language agnostic app that works based on the user locale
  3. Language independent resource support on web app
  4. Business requirement which require support for real time language translation

Architecture

The architecture for this implementation is simple. Here is one of the example architectures but could be derived to any preferred ones.

Since it is beneficial to make it as real time as possible, create an Azure Function or like host the multilingual conversion using a HTTP call. And we will need the Azure AI Text Analytics endpoint for the conversion.

Implementation

For this example, we will use an Azure Function bot service which provides us with the input from the user. Here is link to the Git repo with the Bot Service code. More details about the code and snippets are provided below.

1. Firstly, we will need to intialize the text conversion class with the API key of the Text Analytics service.

public TextAnalyticsProcessing(TraceWriter Log)
{
log = Log;
log.Info("Text Processing Section Called");
// Create a client.
client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials())
{
Endpoint = "https://australiaeast.api.cognitive.microsoft.com"
}; //Replace 'australiaeast' with the correct region for your Text Analytics subscription
log.Info("Text Processing section initialized");
}

2. Before the conversion, we need to first determine the locale of the text provided. In this case, the bot is hosted across many regions, so we mayn’t know where the request is coming from.

public string DetectLanguageFromText(string text)
{
log.Info($"Got Text {text}");
string detectedLanguage = "";
var result = client.DetectLanguageAsync(new BatchInput(
new List<Input>()
{
new Input("1", text)
})).Result;
// Printing language results.
foreach (var document in result.Documents)
{
detectedLanguage = document.DetectedLanguages[0].Iso6391Name;
log.Info($"Document ID: {document.Id} , Language: {document.DetectedLanguages[0].Name}");
}
return detectedLanguage;
}

3. After the locale is determined, we can call the Text Conversion endpoint to convert the text into the required language. In this case, it is going to be English.

public string getTranslatedText(string text)
{
string host = "https://api.cognitive.microsofttranslator.com";
string route = "/translate?api-version=3.0&to=en";
string translatedText = "";
System.Object[] body = new System.Object[] { new { Text = text } };
var requestBody = JsonConvert.SerializeObject(body);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
// Set the method to POST
request.Method = HttpMethod.Post;
// Construct the full URI
request.RequestUri = new Uri(host + route);
// Add the serialized JSON object to your request
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
// Add the authorization header
request.Headers.Add("Ocp-Apim-Subscription-Key", ConfigurationManager.AppSettings["TranslatorTextSubKey"]);
// Send request, get response
var response = client.SendAsync(request).Result;
var jsonResponse = response.Content.ReadAsStringAsync().Result;
// Print the response
log.Info(jsonResponse);
JArray result = JArray.Parse(jsonResponse);
log.Info(result[0].ToString());
TranslatedResult translatorObj = JsonConvert.DeserializeObject<TranslatedResult>(result[0].ToString());
foreach(var translate in translatorObj.Translations)
{
log.Info($"Translate text {translate.Text}");
translatedText = translate.Text;
}
}
return translatedText;
}

4. This text can then be used by the bots to determine the intents for further processing or the app for its business logic

Conclusion

In this blog we saw we could use the Azure AI to get the text converted in the destination language type. For this blog, I am converting them to English but could be converted to any language of choice. After the target language is acquired we could use it as needed.

Happy Coding!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s