Translate Text with AWS Translate

Code


Below, I'm going to show you some simple Python code that quickly gets you up and translating. As for prerequisites, you will need an AWS credentials file and a current version of boto3 (hint: just run pip install -U boto3).

Currently, there are six supported languages in addition to English. You can translate to and from English, but at least one language must be English. If you want to translate between two non-English languages, you must first translate the source text to English, then translate the English translation of the source text to the final language.

Here is a 'Hello, World!' example in Python. In the final section, there is a live demo of the AWS Translate API.


import boto3

translate = boto3.client(
    service_name='translate', 
    region_name='us-east-1', 
    use_ssl=True,
)

result = translate.translate_text(
    Text="Hello, World!", 
    SourceLanguageCode="en", # English
    TargetLanguageCode="es", # Spanish
)

print('TranslatedText: ' + result.get('TranslatedText'))
print('SourceLanguageCode: ' + result.get('SourceLanguageCode'))
print('TargetLanguageCode: ' + result.get('TargetLanguageCode'))



Live Demo


Let's take a look at a live example. First, you'll need to select one of the predefined English statements in "Source Text" field. This is an example of text that you'll want to translate to another language. (For security purposes, I did not provide a blank input field for some hacker to misuse. Although the source text is predefined, this live demo uses AWS Translate for each try.)

Next, select one of six languages that you want as the "Target Language". Once that is done, you're all set. Click "Translate" and look below for the translated text!