Amazon Translate
Developer Guide

Translating Text Using the AWS SDK for Python (Boto)

The following example demonstrates using the TranslateText operation in Python. To run it, you must first install Amazon Translate via the AWS CLI. For instructions, see Step 2: Set Up the AWS Command Line Interface (AWS CLI).

import boto3 translate = boto3.client(service_name='translate', region_name='region', use_ssl=True) result = translate.translate_text(Text="Hello, World", SourceLanguageCode="en", TargetLanguageCode="de") print('TranslatedText: ' + result.get('TranslatedText')) print('SourceLanguageCode: ' + result.get('SourceLanguageCode')) print('TargetLanguageCode: ' + result.get('TargetLanguageCode'))

You can change the source and target languages as long as the language pair selected is supported by Amazon Translate in the correct combination. For more information, see Supported Language Pairs

Another example, this one demonstrating using the custom terminologies operations in Python:

import boto3 translate = boto3.client(service_name='translate') # The terminology file 'my-first-terminology.csv' has the following contents: ''' en,fr Amazon Family,Amazon Famille ''' # Read the terminology from a local file with open('/tmp/my-first-terminology.csv', 'rb') as f: data = file_data = bytearray(data) print("Importing the terminology into Amazon Translate...") response = translate.import_terminology(Name='my-first-terminology', MergeStrategy='OVERWRITE', TerminologyData={"File": file_data, "Format": 'CSV'}) print("Terminology imported: "), print(response.get('TerminologyProperties')) print("\n") print("Getting the imported terminology...") response = translate.get_terminology(Name='my-first-terminology', TerminologyDataFormat='CSV') print("Received terminology: "), print(response.get('TerminologyProperties')) print("The terminology data file can be downloaded here: " + response.get('TerminologyDataLocation').get('Location')) print("\n") print("Listing the first 10 terminologies for the account...") response = translate.list_terminologies(MaxResults=10) print("Received terminologies: "), print(response.get('TerminologyPropertiesList')) print("\n") print("Translating 'Amazon Family' from English to French with no terminology...") response = translate.translate_text(Text="Amazon Family", SourceLanguageCode="en", TargetLanguageCode="fr") print("Translated text: " + response.get('TranslatedText')) print("\n") print("Translating 'Amazon Family' from English to French with the 'my-first-terminology' terminology...") response = translate.translate_text(Text="Amazon Family", TerminologyNames=["my-first-terminology"], SourceLanguageCode="en", TargetLanguageCode="fr") print("Translated text: " + response.get('TranslatedText')) print("\n") print("Cleaning up by deleting 'my-first-terminology'...") translate.delete_terminology(Name="my-first-terminology") print("Terminology deleted.") print("\n")