Using Amazon Translate Api on Java

Sat, Jul 21, 2018 One-minute read

In this post i’ll show how to use amazon translate api on java with aws sdk to do real-time translations.

Amazon Translate

Translate service offers neural and accurate translation service. Using 2million “character” per month is free.

In this post we use this service with our java code.

First of all amazon dependency on our maven code.

<dependencies>
        <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.11.371</version>
        </dependency>

    </dependencies>

Our code is translating english to italian

import com.amazonaws.services.translate.AmazonTranslate;
import com.amazonaws.services.translate.AmazonTranslateClient;
import com.amazonaws.services.translate.model.TranslateTextRequest;
import com.amazonaws.services.translate.model.TranslateTextResult;


public class TranslateApp {

    public static void main(String[] args) {
        AmazonTranslate translate = AmazonTranslateClient.builder()
                .withRegion("us-west-2")
                .build();

        TranslateTextRequest request = new TranslateTextRequest()
                .withText("Greetings from webischia :)")
                .withSourceLanguageCode("en")
                .withTargetLanguageCode("it");
        TranslateTextResult result  = translate.translateText(request);
        System.out.println(result.getTranslatedText());
    }
}

Our response is :

/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/java... 
Saluti da webischia:)

Process finished with exit code 0

You can find source code on github.