Saturday, March 31, 2018

Service Account Keys - Google Translation API


In my last post, I have mentioned about Google Translation API and how to invoke the api using GCP Platform console and via Postman using API Keys. Now, we will see how to use in our Java code by using Service account keys. To use Service Account Keys, we need to install the client library. Following dependency need to add to the pom.xml file of our project,


In GCP Console when we create a service account key, a JSON file which contains the service account key will be downloaded to our computer. The next step is to add the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key as below,




Here is a sample code to test the translation,


import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
public class TranslationApi {
            public static void main(String... args) throws Exception {
                        // Instantiates a client
                        Translate translate = TranslateOptions.getDefaultInstance()
                                                .getService();
                        String text = "Hello";

                        // Translates some text into Chinese (Traditional)
                        Translation translation = translate.translate(text,
                                                TranslateOption.sourceLanguage("en"),
                                                TranslateOption.targetLanguage("zh-TW"));

                        System.out.println("Word in English - " + text);
                        System.out.println("Word in Chinese (Traditional) - "
                                                + translation.getTranslatedText());

            }
}

The Console Output will be,

Word in English - Hello
Word in Chinese (Traditional) - ??

In Eclipse console, chinese characters are displayed as “??..” by default. To display the chinese characters, we need to change the run configuration as follows,


After the above change, the console output will be,

Word in English - Hello
Word in Chinese (Traditional) - 你好

Expect Future Posts, Thank You.

Tuesday, March 27, 2018

Google Translation API


When you want to translate words from one language to another language, What will you do? Go to google translate and select the language origin and the target language as described below,





What if you want to translate in your Java code? For this Google has a solution in thier Google Cloud Platform(GCP), Google Cloud Translation API. In GCP platform, Google provides their api’s and in the platform you can do the api analytics and billing! Forget to mention its not free but for initial signup there are giving $300 credits free and we can use for 1 year.

Let’s look at the Google Cloud Translation API,



In the translation API, there are five methods listed below,




For the translation, we need to use language.translations.translate method. 

In the GCP platform we can test the api as follows,


For the “q” property, we need to type the text to be translated.

For the “source” property, we need to give the text language origin
For the “target” property, we need to give the target language.

The below image depicts the api call result,



So, we have tested the api using GCP platform, how about testing the api from externally

So, here comes POSTMAN,



Postman is an api testing tool which can be installed in Google Chrome as a plugin. To test the api, Google needs to authenticate us, so Google provides authentication mechanism like API Key, Oauth 2.0 Client ID’s.  As i earlier mentioned, its not free so we need to sign up for billing using our credit card and can use the free credits.

To get the API Key, we need to create a project in GCP platform and have to generate the key,
The api can be invoked by calling,

The Postman result follows,



Expect Future Posts, Thank You