FreddieDeWitt / google-translate-extended-api

Free Google Translate API - get word definitions, examples, synonyms and a lot more
MIT License
51 stars 14 forks source link
google-translate-api nodejs npm-module

google-translate-extended-api

Free Extended Google Translate API - get word definitions, examples, synonyms and a lot more

Updates

Table of Contents

Features

Installation

npm i google-translate-extended-api

Usage examples

The first argument is query (it can be a single word or a sentence).
The second argument is a source language of query, third - desired translation language.
The fourth is optional - it is dataOptions object.


const translate = require('google-translate-extended-api');

translate("parlous", "en", "de").then((res) => {
    console.log(JSON.stringify(res, undefined, 2));
}).catch(console.log);

Result is:


{
  "word": "parlous",
  "translation": "parlous",
  "wordTranscription": "ˈpärləs",
  "translationTranscription": null,
  "translations": {
    "adjective": [
      "gefährlich",
      "schlimm"
    ]
  },
  "definitions": {
    "adjective": [
      "full of danger or uncertainty; precarious."
    ],
    "adverb": [
      "greatly or excessively."
    ]
  },
  "examples": [
    "And how do you sap the energy of the insurgency when the parlous state of the economy keeps everyone desperately poor?",
    "Finally, I want to talk quite seriously about the parlous state of politics in this House.",
    "Is the state of American political fiction really so parlous perilous?"
  ]
}

The list of all available languages is available here. Use 'auto' as a source language to use Google's language detection.

Or you can disable examples using dataOptions object:


translate("dog", "en", "de", {examples: false}).then((res) => {
    console.log(JSON.stringify(res, undefined, 2));
}).catch(console.log);

Which yields:


{
  "word": "dog",
  "translation": "Hund",
  "wordTranscription": "dôg",
  "translationTranscription": null,
  "translations": {
    "noun": [
      "Hund",
      "Rüde",
      "Pleite"
    ]
  },
  "definitions": {
    "noun": [
      "a domesticated carnivorous mammal that typically has a long snout, an acute sense of smell, and a barking, howling, or whining voice. It is widely kept as a pet or for work or field sports.",
      "a person regarded as unpleasant, contemptible, or wicked (used as a term of abuse)."
    ],
    "verb": [
      "follow (someone or their movements) closely and persistently.",
      "act lazily; fail to try one's hardest."
    ]
  }
}

API

translate(text, sourceLang, destLang, dataOptions)

Arguments

Response

returns Promise that:

dataOptions Object

Fields

Defaults


defaultDataOptions = {
    returnRawResponse: false,
    detailedTranslations: true,
    definitionSynonyms: false,
    detailedTranslationsSynonyms: false,
    definitions: true,
    definitionExamples: false,
    examples: true,
    removeStyles: true
}

You can always change defaults like this:


const translate = require('extended-google-translate-api');
translate.defaultDataOptions.definitions = true;

Response Object

if (dataOptions.returnRawResponse === true) then the result will be the raw data from request to translate.google.com.

Otherwise:

This object always contains the following fields:

Availability of other fields depends on dataOptions Object, the third argument of translate function

Contacts

If you see a mistake in README, have a question or encountered a problem - please open an issue here or write me at freddie.dewitt@yandex.com