Convert text from one language to another using Google Translate API in PHP / JavaScript

Google Translate API in PHP / JavaScript

In this article, I have explained how to translate text from one language to another using Google Translate API in PHP / JavaScript.

The daily limit of the Translate API is 2 million characters per day. You can increase the limit to 50 Millions characters per day in the cloud console. They will charge 20 $for 1 Million characters. For Pricing Details check this URL.

Google provides API to access their services. In order to use a Google service in your website, you need a API Key. Google API Console: https://code.google.com/apis/console

After this guide you will be able to fetch translations direct from your application from the Google Translate API. You’ll learn how to access the API, how to use it and if errors occur how to handle it.

HOW TO GET GOOGLE TRANSLATE API KEY

To use the API you need get a Google API key;

Step-1: Go to Cloud Console

Step-2: Create a Project and select it.

Step-3:

Google Translate API
Google Translate API
Google Translate API
Google Translate API
Google Translate API

GOOGLE TRANSLATE API PHP EXAMPLE.

The Translate API offers 3 methods:
– translate, which translates the given text from one language to another,
– detect, which detects the language of the given text,
– languages, which lists the source and target languages supported by the API.

All methods will be called by getting requests. A simple way to make such a request in PHP is to use the cURL library which we will use in the following examples. The parameters passed on to each process must be encoded with URLs which can be done in PHP using the function rawurlencode(). Remember you need to transfer your API key as a key parameter in every call.

Translate and detect services are charged, but we can use the third option-languages-just to test if our application will connect to the API. To do this we will refer to the following URL:

https://www.googleapis.com/language/translate/v2/languages

The core functionality of the Google Translate API is available through its translate method. It is accessible under the following URL:

https://www.googleapis.com/language/translate/v2

The translate method has several parameters. The most important are:
– q – the input text,
– source – the source language (if it’s not specified, Google will try to identify it automatically),
– target – the target language

A sample response containing the translated text looks as follows:

tutorials website

What happens if you don’t set the source language?

1. Google will manage to detect the language by itself, the JSON response will consequently contain an additional detectedSourceLanguage property holding the source language code.
2. The source language will not be successfully detected (e.g. when the source text was too short) and the Google Translate API will return an HTTP 500 error. 

Handling errors

The Google Translate API can return the following error codes:
– 400 (Bad request) – your request is missing some parameters or you have passed wrong values to the parameters present in the request (e.g. an invalid language code),
– 403 (Forbidden) – you have entered an incorrect API key or have exceeded your quota,
– 500 (Internal Server Error) – Google cannot identify the source language of your text or another error occurred.

GOOGLE TRANSLATE API JAVASCRIPT EXAMPLE.

To use Translate API in JavaScript, you need to get “API Key” from Google API. In the URL Request, we can pass the callback parameter, so callback function will be invoked if the request is successful.

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

OUTPUT:

Convert text from one language to another using Google Translate API in PHP / JavaScript

Conclusion:

You now know the basics for connecting the application to the Google Translate API. Auto-fetching translations can be more difficult when a user submits some content.

Are you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request

Related posts