Timothy Yeung
Timothy Yeung

Reputation: 21

How do I integrate Google Translate into Java?

I was wondering if there's a way that I could integrate google translate into a Java program I am making. I use Eclipse. I sorta did a few things but now I'm completely lost (I'm only a rookie when it comes to programming).

my progress so far: - I have a Google Translate API Key. - I got the "google tab" for eclipse and installed the Translate API.

I also have the "Google CLoud SDK shell" downloaded but now I'm not exactly sure what to do.

Help walking me through the steps would be appreciated!

Thank You!

Upvotes: 0

Views: 3552

Answers (1)

Fabien Leborgne
Fabien Leborgne

Reputation: 387

I suggest you to see the google-api-services-translate-v2 here: https://developers.google.com/api-client-library/java/apis/translate/v2

Basically, if you are using maven you have to add this dependency:

<dependency>
  <groupId>com.google.apis</groupId>
  <artifactId>google-api-services-translate</artifactId>
  <version>v2-rev48-1.22.0</version>
</dependency>

Then you can use it:

    TranslateRequestInitializer translateRequestInitializer = new TranslateRequestInitializer(
            "Generated key from google console");

    // Set up the HTTP transport and JSON factory
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

    // set up translate
    final Translate translate = new Translate.Builder(httpTransport, jsonFactory, null)
            .setApplicationName("My Apps").setTranslateRequestInitializer(translateRequestInitializer).build();

    List<String> sourceTextList = Arrays.asList("source Text");
    // translate
    System.out.println(translate.translations().list(sourceTextList, "fr").execute());

You can find samples here: https://developers.google.com/api-client-library/java/google-api-java-client/samples

Upvotes: 2

Related Questions