Skip to content

Instantly share code, notes, and snippets.

@prule
Last active April 9, 2017 23:27
Show Gist options
  • Save prule/75da618b519979b53ebf to your computer and use it in GitHub Desktop.
Save prule/75da618b519979b53ebf to your computer and use it in GitHub Desktop.
An example use of the Google Translate API via the google-api-services-translate java classes.
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.TranslateRequestInitializer;
import com.google.common.collect.ImmutableList;
/**
* Date: 15/02/2016
*/
/*
* Dependencies
* compile 'com.google.apis:google-api-services-translate:v2-rev43-1.21.0'
* compile 'com.googlecode:google-api-translate-java:0.92'
*/
public class SpikeGoogleTranslate {
final String KEY = "the key you generated from the google console goes here";
/*
* Before starting you need to:
* Log in to Google Cloud Console @ https://console.cloud.google.com/ :
* Create a project
* Enable billing for that project (you can sign up to get $300 credit for 2 months) @ https://console.cloud.google.com/billing/
* Enable the translate API for that project
* Create a server key for the translate api for that project
* Useful links
* https://cloud.google.com/translate/v2/getting_started
* https://developers.google.com/api-client-library/java/
* https://developers.google.com/apis-explorer/
*
* The Translate Java API is not specifically documented, so you have to look at the general api to figure it out:
*
* https://developers.google.com/api-client-library/java/google-api-java-client/release-notes
* https://developers.google.com/api-client-library/java/google-api-java-client/samples
*
* To view all the samples that are available for the Google API Client Library for Java, browse through the samples on GitHub. Each sample has an instructions.html file that explains how to set up and use the sample. (To more easily view the instructions in a browser, visit http://htmlpreview.github.io/ and paste the URL of the HTML file into the text box.)
*/
public static void main(String[] args) throws Exception {
// set key created via google cloud console
final TranslateRequestInitializer KEY_INITIALIZER = new TranslateRequestInitializer(KEY);
// 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 Project")
.setTranslateRequestInitializer(KEY_INITIALIZER)
.build();
// list languages
{
System.out.println(translate.languages().list().execute());
// output: {"languages":[{"language":"af"},{"language":"ar"},{"language":"az"},{"language":"be"},{"language":"bg"},{"language":"bn"},{"language":"bs"},{"language":"ca"},{"language":"ceb"},{"language":"cs"},{"language":"cy"},{"language":"da"},{"language":"de"},{"language":"el"},{"language":"en"},{"language":"eo"},{"language":"es"},{"language":"et"},{"language":"eu"},{"language":"fa"},{"language":"fi"},{"language":"fr"},{"language":"ga"},{"language":"gl"},{"language":"gu"},{"language":"ha"},{"language":"hi"},{"language":"hmn"},{"language":"hr"},{"language":"ht"},{"language":"hu"},{"language":"hy"},{"language":"id"},{"language":"ig"},{"language":"is"},{"language":"it"},{"language":"iw"},{"language":"ja"},{"language":"jw"},{"language":"ka"},{"language":"kk"},{"language":"km"},{"language":"kn"},{"language":"ko"},{"language":"la"},{"language":"lo"},{"language":"lt"},{"language":"lv"},{"language":"mg"},{"language":"mi"},{"language":"mk"},{"language":"ml"},{"language":"mn"},{"language":"mr"},{"language":"ms"},{"language":"mt"},{"language":"my"},{"language":"ne"},{"language":"nl"},{"language":"no"},{"language":"ny"},{"language":"pa"},{"language":"pl"},{"language":"pt"},{"language":"ro"},{"language":"ru"},{"language":"si"},{"language":"sk"},{"language":"sl"},{"language":"so"},{"language":"sq"},{"language":"sr"},{"language":"st"},{"language":"su"},{"language":"sv"},{"language":"sw"},{"language":"ta"},{"language":"te"},{"language":"tg"},{"language":"th"},{"language":"tl"},{"language":"tr"},{"language":"uk"},{"language":"ur"},{"language":"uz"},{"language":"vi"},{"language":"yi"},{"language":"yo"},{"language":"zh"},{"language":"zh-TW"},{"language":"zu"}]}
}
// translate
{
// phrases
final ImmutableList<String> phrasesToTranslate = ImmutableList.<String>builder().add("Hello world").add("Where can I walk my dog").build();
// perform
System.out.println(translate.translations().list(phrasesToTranslate, "fr").execute());
// output: {"translations":[{"detectedSourceLanguage":"en","translatedText":"Bonjour le monde"},{"detectedSourceLanguage":"en","translatedText":"Où puis-je promener mon chien"}]}
}
}
}
@fierascu
Copy link

Non-static field KEY cannot be referenced from a static context: final String KEY should be final static String KEY

@grantespo
Copy link

Translate.Builder is unrecognized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment