matheuss / google-translate-api

A free and unlimited API for Google Translate :dollar::no_entry_sign:
https://npmjs.com/package/google-translate-api
MIT License
2.87k stars 604 forks source link

Allow 'format' queryparam to be passed in to Google Translate API #81

Open thejeff77 opened 6 years ago

thejeff77 commented 6 years ago

Allow the 'format' query parameter string so that the user can pass in html to the google translate API.

jalMogo commented 5 years ago

I am not familiar with the google translate API, but I was able to pass raw HTML to the translate api and it seems to work fine:

// test.js
const translate = require("google-translate-api");
const content = `<h3>a cat plus a rat plus a dog, is a hog</h3>
<h4>Yes, the above statement is true</h4>`;
translate(content, { from: "en", to: "es" })
  .then(res => {
    console.log("res.text:", res.text);
  })
  .catch(err => {
    console.error("translate: error! err:", err);
  });
$ node test.js
res.text: <h3> un gato más una rata más un perro, es un cerdo </h3>
<h4> Sí, la declaración anterior es verdadera </h4>

Was there a more specific issue you were having with passing in html?

thejeff77 commented 5 years ago

Forgive me, but you're answering issues for a library which talks to the Google Translate API but you have no familiarity with it? That is interesting.

The translate API is documented publicly here:

https://cloud.google.com/translate/docs/reference/translate

From the docs linked to above:

format | string                                        The format of the source text, in either HTML (default) or plain-text. A                     value of html indicates HTML and a value of text                     indicates plain-text.
-- | --

It seems one of the things that is missing from this library to be able to manually specify in the request. Depending on the value of this param, it will override the default behavior of the service & can change the response to a desirable outcome given the request body.

It also appears that manual specification of the model param is also not supported by this library.

It would be pretty cool to get full support for all API options through this library, and luckily there aren't that many options for the API, so supporting it fully isn't far off.

milahu commented 3 years ago

Was there a more specific issue you were having with passing in html?

in my case, i need character-exact html translations with google's free API so i ended up writing an html2google encoder in my translate.js script

also see my post in Prohibit the translation of pieces of text in Google Translate