2013-12-20

In Google Translate API Tutorial, I have explained how to convert text from one language to another using Google Translate API in PHP / JavaScript.
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.

Topics covered in this article
1) Get Google Translate API Key
2) List of Google Translate API
3) Google Translate API PHP Example.
4) Google Translate API Javascript Example.

 

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.

 

Check this page, how to get Google API key

 

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

You need to send GET request in the below format.

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 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.

Reference: Google Documentation

The post Google Translate API Tutorial for PHP / Javascript appeared first on hayaGeek.

Show more