GithubHelp home page GithubHelp logo

adizbek / google-translate-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from iamtraction/google-translate

0.0 1.0 0.0 44 KB

๐Ÿˆฏ A Node.JS library to consume Google Translate for free.

Home Page: https://www.npmjs.com/package/@k3rn31p4nic/google-translate-api

License: GNU General Public License v3.0

JavaScript 100.00%

google-translate-api's Introduction

Google Translate API

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

GitHub release Dependencies Known Vulnerabilities license Say Thanks!

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 @k3rn31p4nic/google-translate-api

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

Usage

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

// If you've installed from GitHub, do:
const translate = require('google-translate-api');

Method: translate(text, options)

translate(text, options).then(console.log).catch(console.error);
Parameter Type Optional Default Description
text String No - The text you want to translate.
options Object - - The options for translating.
options.from String Yes 'auto' The language name/ISO 639-1 code to translate from. If none is given, it will auto detect the source language.
options.to String Yes 'en' The language name/ISO 639-1 code to translate to. If none is given, it will translate to English.
options.raw Boolean Yes false If true, it will return the raw output that was received from Google Translate.

Returns: Promise<Object>

Response Object:

Key Type Description
text String The translated text.
from Object -
from.language Object -
from.language.didYouMean Booelan Whether or not the API suggest a correction in the source language.
from.language.iso String The ISO 639-1 code of the language that the API has recognized in the text.
from.text Object -
from.text.autoCorrected Booelan Whether or not the API has auto corrected the original text.
from.text.value String The auto corrected text or the text with suggested corrections. Only returned if from.text.autoCorrected or from.text.didYouMean is true.
from.text.didYouMean Booelan Wherether or not the API has suggested corrections to the text
raw String The 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('Thnak 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('Thnak 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 and/or send a thank you note.

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.

google-translate-api's People

Contributors

iamtraction avatar jqlu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.