X

Geertjan's Blog

  • January 2, 2014

Google Translate in NetBeans IDE 7.4

Geertjan Wielenga
Product Manager

Google Translate API is a paid service from the Google Cloud. Once you've set up your API key and billing information, you can use it in NetBeans IDE, too, of course.

Notice the from/to toolbar, where I've selected English/German: 

Then, above I right-click over a selection and choose "Translate", which I get back in German: 

The code is more or less the same as it was in my 2007 article on this topic:

http://www.javalobby.org/java/forums/t103431.html

However, in the meantime there's a REST API that can be used instead. I'm not using the REST API, still the same code as the above, though slightly changed because of changes in the Google Translate API:

@Override
public void actionPerformed(ActionEvent ev) {
Language from = TranslatorPanel.from();
Language to = TranslatorPanel.to();
try {
JTextComponent comp = EditorRegistry.lastFocusedComponent();
String toChange = comp.getSelectedText();
TranslateV2.setHttpReferrer("http://code.google.com/p/google-api-translate-java");
TranslateV2.setKey("your-key-goes-here");
String text = TranslateV2.DEFAULT.execute(toChange, from, to);
comp.replaceSelection(text);
} catch (java.lang.NullPointerException ex1) {
} catch (GoogleAPIException ex) {
}
}

All the source code of the above is here:

https://java.net/projects/nb-api-samples/sources/api-samples/show/versions/7.4/misc/GoogleTranslator

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.