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
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);
}
Then go and deploy your api
Make sure that type is Web app and that everyone has access to it
Testing
Go to your url and add query parameters
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'});
}
Like this:
Press "run", and return on sheets.
Then you can use it like this:
Top comments (4)
Oh wow! Never knew this was a thing. Thanks a lot for sharing this :)
Cool stuff. Thanks for sharing!!
Incredibly helpful - it was the key to cracking something I've been trying to do for a year. Thanks so much
Welcome to DEV community Rich