Friday, September 18, 2015

Use Google Translate API for FREE

My company subscribed to Google Apps for Education. So, I though that access to Google Translate API would be included in our contract... but, that's not the case :(

Hummm... hummm... let's hack this for free.

Prerequisite is to have a Google account (even a free gmail account). What we will do is the create a sctipt on Google that will use the translate api freely and publish it as a REST web service which has the same JSON output format as the original.

Let's go!


  1. Connect to http://script.google.com and create an empty project.
  2. Copy paste the code below
  3. Publish it as web application
  4. Grant access to the url of your script to anonymous access if needed


var mock = {
  parameter:{
    q:'hello',
    source:'en',
    target:'fr'
  }
};


function doGet(e) {
  e = e || mock;
  
  var sourceText = ''
  if (e.parameter.q){
    sourceText = e.parameter.q;
  }
  
  var sourceLang = 'auto';
  if (e.parameter.source){
    sourceLang = e.parameter.source;
  }

  var targetLang = 'en';
  if (e.parameter.target){
    targetLang = e.parameter.target;
  }
  
  var translatedText = LanguageApp.translate(sourceText, sourceLang, targetLang)
  
  var json = {
    'data': {
      'translations' : [{
        'translatedText' : translatedText
        }]
       }
  };
   
  return ContentService.createTextOutput(JSON.stringify(json)).setMimeType(ContentService.MimeType.JSON);
}


The original Translation API from Google is https://www.googleapis.com/language/translate/v2?q=[encodedText]&target=[language]&source=[language]&key=APIKEY

Your's will be something like https://script.google.com/macros/s/AKfyxxxxx/exec?q=[encodedText]&target=[language]&source=[language]

The output formats of the two REST service are identicals.

Enjoy!


2 comments:

Greg David said...

Hello! If you are working on software translation projects, I may recommend to evaluate https://poeditor.com/. It is an online localization management tool equipped with great features (API, translation memory, Gengo integration, automatic translation, Bitbucket and GitHub integration) and a user-friendly interface that will automate your workflow.

simashree said...

Thanks for the great information in your blog Selenium Training in Chennai