Install google-translate-api via npm
masterInstall the package as a dependency using npm:
npm install --save google-translate-apirepository·master·Indexed 25 days ago
https://github.com/matheuss/google-translate-apiA free and unlimited Node.js API for Google Translate (version 2.3.0) that supports automatic language detection, spelling correction, and language correction. It provides a translate(text, options) method to translate text between languages using the same servers as translate.google.com.
Install the package as a dependency using npm:
npm install --save google-translate-apiThe API provides automatic spelling and language correction. When a correction is made, the res.from.text object is populated. The corrected value is returned in res.from.text.value with the corrections wrapped in brackets [ ].
translate('I spea Dutch').then(res => {
console.log(res.from.text.value);
//=> I [speak] Dutch
}).catch(err => {
console.error(err);
});The primary API method translate(text, options) performs the translation. It accepts a text string and an options object. It returns a Promise that resolves to an object containing the translated text and metadata about the source language and any corrections made.
const translate = require('google-translate-api');
translate('Ik spreek Engels', {to: 'en'}).then(res => {
console.log(res.text);
//=> I speak English
console.log(res.from.language.iso);
//=> nl
}).catch(err => {
console.error(err);
});The translate() method returns an object with the following structure:
text (string): The translated text.from (object): Metadata about the detected source language.language (object)iso (string): The ISO code of the recognized language.didYouMean (boolean): true if the API suggests a correction in the source language.text (object): Correction details (only present if autoCorrected or didYouMean is true).autoCorrected (boolean): true if the API auto-corrected the text.value (string): The corrected text, with corrections delimited by brackets (e.g., I [speak] Dutch).didYouMean (boolean): true if the API suggested corrections.raw (string): The raw response from Google Translate (only if options.raw was set to true).The options object for the translate method supports the following keys:
from (string, default: 'auto'): The source language. Must be 'auto' or a valid language code/name.to (string, default: 'en'): The target language. Must be a valid language code/name.raw (boolean, default: false): If true, the returned object includes a raw property containing the raw string response from Google Translate servers.The translate function performs a translation of the provided text using Google Translate. It returns a Promise that resolves to a translation object.
Options (opts):
from (string): The source language code. Defaults to 'auto'.to (string): The target language code. Defaults to 'en'.raw (boolean): If true, the raw property in the result object will contain the unparsed response body from Google.Returns an object containing:
text (string): The translated text.from (object): Information about the source language and text, including:language.iso (string): The ISO code of the detected language.language.didYouMean (boolean): Indicates if the detected language was not what was expected.text.value (string): The original text (potentially auto-corrected).text.autoCorrected (boolean): Indicates if the source text was auto-corrected.text.didYouMean (boolean): Indicates if the source text was auto-corrected.raw (string, optional): The raw response body if opts.raw was set to true.The translate function may reject with errors containing specific error codes:
400: Occurs if a provided language code in from or to is not supported. The error message will be The language '<lang>' is not supported.BAD_REQUEST: Occurs if the request to Google Translate returns a status code other than 200.BAD_NETWORK: Occurs if there is a network-level error during the request.