Google Translate in NetBeans IDE 7.4

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

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today