Google Translate API using PHP, Javascript Tutorial

7

In Google Translate API Tutorial, I have explained how to convert text from one language to another using Google Translate API in PHP / JavaScript / Java.
Translate API has daily limit of 2 Million characters per day. You can increase the limit in the cloud console up to 50 Millions characters per day.They charge 20$ for 1 Million characters. Check this URL for Pricing Details.

1). HOW TO GET GOOGLE TRANSLATE API KEY.

To use the API you need get a Google API key;
a). Go to Cloud Console
b). Create a Project and select it.
c). On the left side bar, Go to “APIs & auth” Tab => APIs Tab
d). Enable “Translate API”
e). Go to “Credentials” Tab
f). Click on “CREATE NEW KEY”
g). Create “Server Key” for PHP / Java / Ruby / Python. Create “Browser Key” for JavaScript.

2). LIST OF GOOGLE TRANSLATE API

Google Provided REST API, so you can use them in any language.

You can pass below parameters to Translate API.

a).callback  -> this is for JavaScript. callback is invoked the when the translation is successful.
b).key  -> Google API key, without this you are not allowed to use this API.
c).source -> Source language
d).target  -> Destination language.
e).prettyprint  -> If it set to true, response will be readable format. Default is true
f).q -> Text to be translated.
Below is the list of API.
2.1) translate – You can use this API  to translate text from source language to destination language

 

Response from API :

You can request multiple translations in single request. You can do this by sending multiple ‘q’ parameters.
Sample GET request:

Response format:

2.2) detect – You can use this API to detect the language of given text.

Sample GET Request:

Response:

2.3). languages – This API Lists the Source and Destination languages supported by the translate method

Sample GET Request:

How to handle Errors ? :

If there is any error in the URL request, you will get the error in the below formats.
ERROR 1:  Response when the key is invalid

 

ERROR 2: Response when target parameter is missing.

3) GOOGLE TRANSLATE API PHP EXAMPLE.

To use Google Translate API in PHP, you need to get Google API “Server Key”. All the translate API are called using GET requests. So we can use file_get_contents() or PHP CURL Library.To encode the parameter, rawurlencode() function is used. To decode JSON text as object, json_decode() function is used.

I have given two examples, you can use any of these two examples.

Using CURL library:

PHP Example Using file_get_contents()

4) GOOGLE TRANSLATE API JAVA EXAMPLE.

To use Translate API in Java, you need to get Google API “Server Key”.   GSON Library is used to parse JSON text.

You can use the below Java code to translate text.

5) GOOGLE TRANSLATE API JAVASCRIPT EXAMPLE.

To use Translate API in JavaScript, you need to get Google API “Browser Key”. We can pass callback parameter in the URL Request, So callback function is invoked when the request is successful.
Sample GET Request:

Response is:

Below is the JavaScript example:

We are dynamically adding a script to head, So when the request is successful showIt() function called.

If you want to translate text with AJAX call, you can use the below example.

 

Leave A Reply

Your email address will not be published.