DEV Community

Cover image for Breaking Language Barriers with Microsoft Azure AI Translator
Erick Badillo
Erick Badillo

Posted on

Breaking Language Barriers with Microsoft Azure AI Translator

“Artificial intelligence is the new electricity.” - Andrew Ng

Hello!. Today, we embark on an exciting journey as we kickstart a series of short posts that will shed light on the remarkable capabilities of Azure AI.

This post serves as a demonstration of how you could abstract and implement Azure's AI services for referencing in your applications. Within this post, you will find a link to the code showcasing a lightweight version of Azure resources that you can utilize to deploy an AI-powered application in production environments. Hopefully, the provided designs and abstractions will prove useful as they are, and in any other case, I hope this work serves as a foundation for you to build upon in your own implementation.

What Not to Expect:
This post is not a step-by-step guide on how to use Azure's artificial intelligence services. It is also not a duplicate of Microsoft's official documentation, nor is it a copy of the code that can already be found in the examples provided by Microsoft for developers to implement their Azure AI services. If you wish to access Microsoft's resources, please take a look at the references section.

Let's have fun

In our interconnected world, effective communication transcends borders and languages. Microsoft Azure Translator, an AI-powered service, empowers developers to seamlessly bridge language gaps. Join me as we explore this powerful tool and its potential impact on your projects.

Azure Translator is designed to effortlessly translate text and spoken language between multiple languages, making it a vital tool for applications aiming to break down language barriers.

With Azure Translator, you can:
-Achieve highly accurate translations.
-Detect and identify languages in real-time.
-Translate entire documents while preserving formatting.
-Enable multilingual chatbots for superior customer support.
-Transliterate to different scripts

Show me the code!

You can find the demo code here

Explain me the design

The AI Azure services demo is a C# solution created in Visual Studio using .NET7.

The solution contains two assemblies:

  1. Class library AzureAI.Poc.Services.Api
  2. xUnit test project which contains:
    • The app root composition code
    • The integration test fixtures for each AI service (for this post, we are reviewing the Translator tests)

The solution uses the following Azure resources:

  1. Azure Key Vault to store the AI Services API keys
  2. Azure App Configuration (do I need to explain why we need this?). To store the application configurations.
  3. Azure multi-AI service Azure resources Please notice you can deploy the needed Azure resources using the ARM template that you can find in the GitHub repository

AzureAI.Poc.Services.Api

This assembly contains all the code you might use in your own implementations. The goal of this assembly is to show you and luckily bring you some ideas of how to add the AI Azure services to your applications. In this assembly you can find:

  • Common classes like the HTTP Proxy and the global configurations model.
  • The strong type model classes for the different AI services (where applicable)
  • The contract and implementation to connect to the Azure AI Rest services and get string JSON formatted responses. See the class TranslatorRestClient
public async Task<string> DetectAsync(IEnumerable<DetectRequest> 
request, CancellationToken cancellationToken)
{
    var route = _routeBuilder.BuildDetectRoute();
    var body = request.Select(r => new TranslatorRequestBody 
               { 
                   Text = r.Text 
               }).ToArray();

    var response = await PostAsync(route, body, 
                                   cancellationToken);        
    return response;
}
Enter fullscreen mode Exit fullscreen mode
  • The contract and implementation to connect to Azure AI Rest services and get a strong type response. See the class TranslatorService
public async Task<DetectResult[]?> DetectAsync(
IEnumerable<DetectRequest> request, 
CancellationToken cancellationToken = default)
{
    var detectResult = await _translatorRestClient.DetectAsync(
                       request, cancellationToken);
    var result = JsonConvert.DeserializeObject<DetectResult[](
                 detectResult);

    return result;
}
Enter fullscreen mode Exit fullscreen mode

Class view of the main classes involved in the translation functionality
Translator main classes

Ok, ok, now show me how to invoke it!

Call the none strong-typed service

var request = new TranslateRequest
{
    FromLanguage = "en",
    ToLanguages = new List<string> {"es-MX"},
    IncludeAlignment = true,
    IncludeSentenceLength = true
};

request.Text.Add("Hello!, what's your name?");

var response = await _translatorRestClient.TranslateAsync(request,
 CancellationToken.None);
Enter fullscreen mode Exit fullscreen mode

Call the strong-typed service

var request = new TranslateRequest
{
    FromLanguage = "en",
    ToLanguages = new List<string> { "es-MX" },
    IncludeAlignment = true,
    IncludeSentenceLength = true
};

request.Text.Add("Hello!, what's your name?");

var response = await _systemUnderTest.TranslateAsync(request, 
CancellationToken.None);
Enter fullscreen mode Exit fullscreen mode

Final comments

In this Azure Translator Service demo, we have used the REST API to showcase the most used language translation capabilities. Stay tuned for more in these series, and start your own AI journey today.

References

  1. Azure AI Translator Service:
    [Online]. https://azure.microsoft.com/en-us/products/ai-services/ai-translator

  2. Azure AI Translator APIs:
    [Online]. https://learn.microsoft.com/en-us/azure/ai-services/translator/translator-text-apis?tabs=csharp

  3. Azure AI Translator Documentation:
    [Online]. https://learn.microsoft.com/en-us/azure/ai-services/translator/

  4. More Training Resources:
    [Online]. https://learn.microsoft.com

Top comments (0)