DEV Community

JakovGlavac
JakovGlavac

Posted on • Updated on

How to translate HTML for free using google translate.

Introduction

While working on a new feature for my side project eduo instrukcije, I needed to translate HTML to other languages which was challenging to do for free. Google translate api has parameter "format" that when set to "html" ignores all tags and translates only real text, but it's not free. There are couple of reversed engineered google translate apis that are totally free, but none of them had the format option available. Then I found out that you can use Google Translate api inside google sheets, and you can create real apis with google sheets, and I think that's pretty cool.

How to do it?

First open google sheets and open a new app script
Image description

Then paste this doGet function

function doGet(e){
  var params = e.parameter;
  var translation = LanguageApp.translate(params.text, params.froml, params.to, {contentType: 'html'});

  return ContentService.createTextOutput(translation).setMimeType(ContentService.MimeType.TEXT);
}
Enter fullscreen mode Exit fullscreen mode

Then go and deploy your api

Image description

Make sure that type is Web app and that everyone has access to it
Image description

Then copy your url Image description

Testing

Go to your url and add query parameters
Image description

And that's it!

One more thing

If you want to use this inside sheets, just modify the script:

function translation(text, froml, tol) {
  return LanguageApp.translate(text, froml, tol, {contentType: 'html'});
}
Enter fullscreen mode Exit fullscreen mode

Like this:
Image description
Press "run", and return on sheets.
Then you can use it like this:

Image description

Top comments (4)

Collapse
 
trillionclues profile image
Excel Nwachukwu

Oh wow! Never knew this was a thing. Thanks a lot for sharing this :)

Collapse
 
shivampawar profile image
Shivam Pawar

Cool stuff. Thanks for sharing!!

Collapse
 
designrichly profile image
Rich Brown

Incredibly helpful - it was the key to cracking something I've been trying to do for a year. Thanks so much

Collapse
 
mannu profile image
Mannu

Welcome to DEV community Rich