Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@iamtraction/google-translate

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@iamtraction/google-translate

A Node.JS library to consume Google Translate API for free.

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.4K
decreased by-27.69%
Maintainers
1
Weekly downloads
 
Created
Source

Google Translate API

A Node.JS library to consume Google Translate for free.

GitHub release Dependencies Known Vulnerabilities license

Feature Highlights

  • Automatically detect source language
  • Automatic spelling corrections
  • Automatic language correction
  • Fast and reliable

Table of Contents

Installation

# Stable version, from npm repository
npm install --save @iamtraction/google-translate

# Latest version, from GitHub repository
npm install --save iamtraction/google-translate

Usage

// If you've installed from npm, do:
const translate = require('@iamtraction/google-translate');

// If you've installed from GitHub, do:
const translate = require('google-translate');
Method: translate(text, options)
translate(text, options).then(console.log).catch(console.error);
ParameterTypeOptionalDefaultDescription
textStringNo-The text you want to translate.
optionsObject--The options for translating.
options.fromStringYes'auto'The language name/ISO 639-1 code to translate from. If none is given, it will auto detect the source language.
options.toStringYes'en'The language name/ISO 639-1 code to translate to. If none is given, it will translate to English.
options.rawBooleanYesfalseIf true, it will return the raw output that was received from Google Translate.
Returns: Promise<Object>

Response Object:

KeyTypeDescription
textStringThe translated text.
fromObject-
from.languageObject-
from.language.didYouMeanBooleanWhether or not the API suggest a correction in the source language.
from.language.isoStringThe ISO 639-1 code of the language that the API has recognized in the text.
from.textObject-
from.text.autoCorrectedBooleanWhether or not the API has auto corrected the original text.
from.text.valueStringThe auto corrected text or the text with suggested corrections. Only returned if from.text.autoCorrected or from.text.didYouMean is true.
from.text.didYouMeanBooleanWherether or not the API has suggested corrections to the text
rawStringThe raw response from Google Translate servers. Only returned if options.raw is true in the request options.

Examples

From automatic language detection to English:
translate('Tu es incroyable!', { to: 'en' }).then(res => {
  console.log(res.text); // OUTPUT: You are amazing!
}).catch(err => {
  console.error(err);
});
From English to French, with a typo:
translate('Thank you', { from: 'en', to: 'fr' }).then(res => {
  console.log(res.text); // OUTPUT: Je vous remercie
  console.log(res.from.autoCorrected); // OUTPUT: true
  console.log(res.from.text.value); // OUTPUT: [Thank] you
  console.log(res.from.text.didYouMean); // OUTPUT: false
}).catch(err => {
  console.error(err);
});
Sometimes Google Translate won't auto correct:
translate('Thank you', { from: 'en', to: 'fr' }).then(res => {
  console.log(res.text); // OUTPUT: ''
  console.log(res.from.autoCorrected); // OUTPUT: false
  console.log(res.from.text.value); // OUTPUT: [Thank] you
  console.log(res.from.text.didYouMean); // OUTPUT: true
}).catch(err => {
  console.error(err);
});

Extras

If you liked this project, please give it a ⭐ in GitHub.

Credits to matheuss for writing the original version of this library. I rewrote this, with improvements and without using many external libraries, as his library was not actively developed and had vulnerabilities.

Keywords

FAQs

Package last updated on 14 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc