0

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!

Timothy Yeung
  • 21
  • 1
  • 1
  • 2
    Possible duplicate of [How to use Google Translate API in my Java application?](http://stackoverflow.com/questions/8147284/how-to-use-google-translate-api-in-my-java-application) – Roman Cherepanov Apr 02 '17 at 22:25

1 Answers1

2

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

Fabien Leborgne
  • 377
  • 1
  • 5