Localize content using Amazon Translate

Amazon Translate is a neural machine translation service that delivers fast, high-quality, and affordable language translation. Neural machine translation is a form of language translation automation that uses deep learning models to deliver more accurate and more natural sounding translation than traditional statistical and rule-based translation algorithms. Amazon Translate allows you to localize content - such as websites and applications - for international users, and to easily translate large volumes of text efficiently.

In this lab, we will use Amazon Translate to translate text to four languages: French, German, Spanish, and Polish. So the output of this lab would be four text files.

Create project

  1. Create a new .NET Core console application project.

Comprehend

  1. Add AWSSDK.Translate Nuget package to the project:

Nuget

  1. Add the following import statements to Program.cs:
using System;
using System.IO;
using System.Text;

using System.Threading.Tasks;

using Amazon.Translate;
using Amazon.Translate.Model;
  1. Replace the Main method in Program.cs with the following async version:
static async Task Main(string[] args)
{
    if (args.Length != 1)
    {
        Console.WriteLine("Please provide text file name (English text) that you want to translate.");

        return;
    }

    var fileName = args[0];

    await TranslateInputFile(fileName);

    Console.WriteLine("The process is complete");
}
  1. Add new method TranslateInputFile. This method is the main one used by the Main to process the job. It reads the input text file and calls TranslateFromEnglish for different languages.
static async Task TranslateInputFile(string fileName)
{
    var text = File.ReadAllText(fileName);

    string polishTranslated = await TranslateFromEnglish(text, "pl");

    string spanishTranslated = await TranslateFromEnglish(text, "es");

    string germanTranslated = await TranslateFromEnglish(text, "de");

    string frenchTranslated = await TranslateFromEnglish(text, "fr");

    var polishOutputFileName = fileName.Replace(Path.GetExtension(fileName), "_polish.txt");
    File.WriteAllText(polishOutputFileName, polishTranslated);

    var spanishOutputFileName = fileName.Replace(Path.GetExtension(fileName), "_spanish.txt");
    File.WriteAllText(spanishOutputFileName, spanishTranslated);

    var germanOutputFileName = fileName.Replace(Path.GetExtension(fileName), "_german.txt");
    File.WriteAllText(germanOutputFileName, germanTranslated);

    var frenchOutputFileName = fileName.Replace(Path.GetExtension(fileName), "_french.txt");
    File.WriteAllText(frenchOutputFileName, frenchTranslated);
}
  1. Add method TranslateFromEnglish to handle the translation process, it uses AmazonTranslateClient in order to achieve that:

Please note that when you initialize AWS SDK’s AmazonTranslateClient, you need to pass the RegionEndpoint of the region you are making labs in. The code below initializes AmazonTranslateClient in the EUWest1 region.

static async Task<string> TranslateFromEnglish(string toTranslate, string targetLanguageCode)
{
    string outputString;

    using (var translateClient = new AmazonTranslateClient(Amazon.RegionEndpoint.EUWest1))
    {
        var translatRequest = new TranslateTextRequest
        {
            Text = toTranslate,
            SourceLanguageCode = "EN",
            TargetLanguageCode = targetLanguageCode
        };

        var translatResponse = await translateClient.TranslateTextAsync(translatRequest);

        outputString = translatResponse?.TranslatedText;

        Console.WriteLine(outputString);
        Console.WriteLine();

    }

    return outputString;
}

Run application

Download book review using the following links, and make sure to save it locally:

book-review-01.txt

Now you can build the application and run it by passing the path to the sample text file:

Translate.exe C:\projects\book-review-01.txt

You will see the following output: Translate

Check the folder with original text files, you should see the following new files:

book-review-01_french.txt
book-review-01_german.txt
book-review-01_polish.txt
book-review-01_spanish.txt

Open the files and check the translated content.