How to Use the Google Translate API for Free

H
Published in: Google Apps Script

The official Google Translate API is available for businesses only but you can use Google Apps Script to create your own Google Language Translation API without having to pay the enterprise license fee.

The text can be translated from one language to another using the LanguageApp service or, if you run out of quota, you can make a call to the secret translate.googleapis.com API that is internally used by the Google Translate extension for Chrome and requires no authentication.

You can publish the Google script and deploy it as a web app with parameters for source and target languages and the text query. You can specify any ISO language pair or say “auto” and the Google Translation API will auto detect the language of the source text.

/* Written by Amit Agarwal */
/* web: ctrlq.org          */

function doGet(e) {

  var sourceText = ''
  if (e.parameter.q){
    sourceText = e.parameter.q;
  }

  var sourceLang = 'auto';
  if (e.parameter.source){
    sourceLang = e.parameter.source;
  }

  var targetLang = 'ja';
  if (e.parameter.target){
    targetLang = e.parameter.target;
  }

  /* Option 1 */

  var translatedText = LanguageApp.translate(sourceText, sourceLang, targetLang)

  /* Option 2 */

  var url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl="
            + sourceLang + "&tl=" + targetLang + "&dt=t&q=" + encodeURI(sourceText);

  var result = JSON.parse(UrlFetchApp.fetch(url).getContentText());

  translatedText = result[0][0][0];

  var json = {
    'sourceText' : sourceText,
    'translatedText' : translatedText
  };

  // set JSONP callback
  var callback = 'callback';
  if(e.parameter.callback){
    callback = e.parameter.callback
  }

  // return JSONP
  return ContentService
           .createTextOutput(callback + '(' + JSON.stringify(json) + ')')
           .setMimeType(ContentService.MimeType.JAVASCRIPT);
}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube