Sample code for 30+ languages & platforms
Java

Google Translate Text

See more Google Translate Examples

Demonstrates how to use the Cloud Translation API to translate text from one spoken language to another. The example translates from English to Spanish.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // --------------------------------------------------------------------------------
        // IMPORTANT:
        // Don't forget you need to 1st enable the Cloud Translation API in your Google Developers Console at https://console.cloud.google.com
        // --------------------------------------------------------------------------------

        // It is assumed we previously obtained an OAuth2 access token.
        // This example loads the JSON access token file
        JsonObject jsonToken = new JsonObject();
        try {
            jsonToken.loadFile("qa_data/tokens/_googleTranslate.json");
        } catch (ChilkatException e) {
            System.out.println("Failed to load _googleTranslate.json");
            return;
        }

        Http http = new Http();

        http.setAuthToken(jsonToken.stringOf("access_token"));

        // The following JSON is sent in the request body.

        // {
        //     "q": "The quick brown fox jumped over the lazy dog.",
        //     "source": "en",
        //     "target": "es",
        //     "format": "text"
        // }

        JsonObject json = new JsonObject();
        // The following code creates the JSON request body.
        json.updateString("q", "The quick brown fox jumped over the lazy dog.");
        json.updateString("source", "en");
        json.updateString("target", "es");
        json.updateString("format", "text");

        json.setEmitCompact(false);
        System.out.println(json.emit());

        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", "https://translation.googleapis.com/language/translate/v2", json, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject jResp = new JsonObject();
        resp.getBodyJson(jResp);
        jResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("Failed.");
            return;
        }

        // Sample JSON response:
        // (Sample code for parsing the JSON response is shown below)

        // {
        //   "data": {
        //     "translations": [
        //       {
        //         "translatedText": "El zorro r�pida salt� sobre el perro perezoso."
        //       }
        //     ]
        //   }
        // }

        String translatedText = "";

        int i = 0;
        int n = jResp.sizeOfArray("data.translations");
        while (i < n) {
            jResp.setI(i);
            translatedText = jResp.stringOf("data.translations[i].translatedText");
            System.out.println(translatedText);
            i = i + 1;
        }
    }
}