How to translate HTML for free using google translate.

jakovglavac

JakovGlavac

Posted on August 20, 2022

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

💖 💪 🙅 🚩
jakovglavac
JakovGlavac

Posted on August 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related