Click here to Skip to main content
12,632,072 members (27,184 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked

Google Translator API v2 Implementation

, 25 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This is a very simple implementation of Google Translator API v2 implementation. It is a simple HTML file and makes use of jQuery for client side operations.


These days, Google is busy in shutting down their free goodies/services that were available. Language Translation service is one among them which was shut down completely from Dec 01, 2011. Now Google has come up with Translation API V2 which is a paid version of Translation Service. Many apps which were dependent on the free version of service have been affected and some have opted for the paid version as some are in the process of shifting the providers. There’s one freebie from Microsoft too Bing Translation APIs. Anyways, I won’t be getting into Bing here but rather concentrate on the Google Language Translator API v2.


This is a very simple and easy to understand implementation of API which is built on simple HTML page and makes use of jQuery to get much of the client side related activities done.

Note: Before I continue, I would like to stress that this article makes an assumption that you have a valid Google API v2. This video provides all the guidance required to get a Google API key.

Using the Code

To make it simple, I have segmented the article content into 3 portions:

Supported Languages – This portion would concentrate on listing the languages which are supported by Google Translator.

The API provides the list of codes for the supported languages but by default doesn’t provide the name of the languages. In case of requirement of the language name, we can pass in additional parameter Target and specify the language code in which we want the language name to be provided in. For e.g., if we specify the code as ‘zh-CN’ it would provide the language names in ‘Chinese Simplified’ and if it's ‘en’ it would list out the names in ‘English’.

Code functionality for loading all the supported Languages by Google API 
function loadLanguages() {
   var apiurl = "" + 
    apiKey<span class="Apple-tab-span" style="white-space: pre; ">        

Detect Language – This portion would be concentrating on detecting the language of the text that’s been provided by the end user for translation. API would make use of its internal AI to identify the language and notify it to the end user. In case it's not possible, it would notify as Unknown.

Code functionality for auto detecting the language of the text to be translated 
function detectLanguage() {
   var text = $.trim($('#txtOrgText').val());
   if (text.length > 0) {
     var apiurl = "" + 
    apiKey <span class="Apple-tab-span" style="white-space: pre; ">    

Translation – This would be our ultimatum. Get the text translated into language (supported by Google API) as required.

Code functionality to translate the text 
 $('#btnTranslate').click(function (e) {

     var text = $.trim($('#txtOrgText').val());
     if (text.length > 0) {
         var langSource = $('#selSourceLanguage').val();
         var langTarget = $('#selTargetLanguage').val();

         if (langSource === "" || langTarget === "") {
             alert("Select Source Language and Target Language");
         else if (langSource === langTarget) {
             alert("Source Language and Target Language cannot be same");
         else {
              $.blockUI({ css: {
              border: 'none',
              padding: '15px',
              backgroundColor: '#000',
              '-webkit-border-radius': '10px',
              '-moz-border-radius': '10px',
              opacity: .5,
              color: '#fff'
     var apiurl = "" + apiKey + 
    "                       &source=" + langSource + "&target=" + langTarget + "&q=";

        url: apiurl + encodeURIComponent(text),
        dataType: 'jsonp',
        async: false,
        success: function (data) {
        <span class="Apple-tab-span" style="white-space: pre; ">    

The process followed in this article implementation is very simple. UI for the implementation is self-explanatory. We have a text box where the end user can key in the text which he/she wishes to be translated. There are 2 drop downs which are pre-loaded with the list of languages supported for translation by Google API and 2 buttons which perform the action as indicated ‘Detect’ and ‘Translate’.

Once text is provided, the user can make use of the ‘Detect’ button and use the Google API service which tries to detect the language of the language and set the Detected Language as the Source Language. If in case Google Service is unable to detect the language, it would set the source language as Unknown. And at any point of time, the user can manually select the source language and the target language and use the ‘Translate’ option to get the translation done.

Points of Interest

This is a very basic implementation and can be improved upon.


You can find the implementation of Google Translator which used the earlier version of Google API here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Govardhana Reddy
Web Developer India
India India
This is Govardhana Reddy, i am here to explore this world of INTERNET. I feel this is one way through which i can explore this world of INTERNET.

I want to be one among the best in this profession (a Software Developer, not a Software Engineer its a bit Controversial.) if not the "BEST"

My definition of a Software Engineer : "A person who knows what to cut/copy and where to paste".

Apart from my technical stuff I love Long Drives, Computer Gaming, Sports, Bikes and much more to say.

Anyways long road ahead keep me accompanied...

You may also be interested in...


Comments and Discussions

SuggestionNothing is free from google... Pin
ii_noname_ii28-Dec-11 1:23
memberii_noname_ii28-Dec-11 1:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 25 Dec 2011
Article Copyright 2011 by Govardhana Reddy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid