Git Product home page Git Product logo

google-translate-api's Introduction

google-translate-api Build Status XO code style Coverage Status Known Vulnerabilities

A free and unlimited API for Google Translate πŸ’΅πŸš«

Features

  • Auto language detection
  • Spelling correction
  • Language correction
  • Fast and reliable – it uses the same servers that translate.google.com uses

Install

npm install --save google-translate-api

Usage

From automatic language detection to English:

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);
});

From English to Dutch with a typo:

translate('I spea Dutch!', {from: 'en', to: 'nl'}).then(res => {
    console.log(res.text);
    //=> Ik spreek Nederlands!
    console.log(res.from.text.autoCorrected);
    //=> true
    console.log(res.from.text.value);
    //=> I [speak] Dutch!
    console.log(res.from.text.didYouMean);
    //=> false
}).catch(err => {
    console.error(err);
});

Sometimes, the API will not use the auto corrected text in the translation:

translate('I spea Dutch!', {from: 'en', to: 'nl'}).then(res => {
    console.log(res);
    console.log(res.text);
    //=> Ik spea Nederlands!
    console.log(res.from.text.autoCorrected);
    //=> false
    console.log(res.from.text.value);
    //=> I [speak] Dutch!
    console.log(res.from.text.didYouMean);
    //=> true
}).catch(err => {
    console.error(err);
});

API

translate(text, options)

text

Type: string

The text to be translated

options

Type: object

from

Type: string Default: auto

The text language. Must be auto or one of the codes/names (not case sensitive) contained in languages.js

to

Type: string Default: en

The language in which the text should be translated. Must be one of the codes/names (not case sensitive) contained in languages.js.

raw

Type: boolean Default: false

If true, the returned object will have a raw property with the raw response (string) from Google Translate.

Returns an object:

  • text (string) – The translated text.
  • from (object)
    • language (object)
      • didYouMean (boolean) - true if the API suggest a correction in the source language
      • iso (string) - The code of the language that the API has recognized in the text
    • text (object)
      • autoCorrected (boolean) – true if the API has auto corrected the text
      • value (string) – The auto corrected text or the text with suggested corrections
      • didYouMean (booelan) – true if the API has suggested corrections to the text
  • raw (string) - If options.raw is true, the raw response from Google Translate servers. Otherwise, ''.

Note that res.from.text will only be returned if from.text.autoCorrected or from.text.didYouMean equals to true. In this case, it will have the corrections delimited with brackets ([ ]):

translate('I spea Dutch').then(res => {
    console.log(res.from.text.value);
    //=> I [speak] Dutch
}).catch(err => {
    console.error(err);
});

Otherwise, it will be an empty string ('').

Related

License

MIT Β© Matheus Fernandes

google-translate-api's People

Contributors

arthurlacoste avatar jukeboxrhino avatar matheuss avatar tgyou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

google-translate-api's Issues

why i am getting 503?

It works only for first few requests. After many requests it rejects and shows 503 Error. Did they implement any rate limiting?

Please check it out.

{ HTTPError: Response code 503 (Service Unavailable)
at stream.catch.then.data (/index.js:123:13)
at
at process._tickCallback (internal/process/next_tick.js:160:7)
..................

Text Limit with English characters

I found that using this package, the text limit is around 1500 characters.
is it because it is using GET request instead of POST ? As i am expecting a 5000 characters limit.

Thanks

TypeError: Cannot read property 'prototype' of undefined

I've this error in the console

  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);
  });

=> TypeError: Cannot read property 'prototype' of undefined

How do you do that? Is this legal?

Sorry for be a gossip, but, how??
Don't misunderstandme, I admire you for create this awsome lib, but I would like to know your secret, cuz this is a non-free service by google.
Thanks for the lib by the way. :D

invalid function

TypeError: a.charCodeAt is not a function

at sM (/app/node_modules/google-translate-token/index.js:33:19)

at /app/node_modules/google-translate-token/index.js:114:18

at <anonymous>

at process._tickCallback (internal/process/next_tick.js:188:7)

fixed

How to use with Titanium?

Hi Team,

Could you please help me that how can I use Google API in mobile application development.

Please help me on this issue.

Thanks,
Hemant Solanki

Text Limit with Asian Characters

You can't put anywhere near as many kanji, hiragana or katakana characters as you can put english characters. Also the limit for both of them is far less then 2000 characters.

How to use in website?

Hi,
anyone knows how to implement it on a website, for instance as a jquery ajax call?
thanks!

code: 'BAD_NETWORK'

This is the code I'm running:

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

translate('hola', { to: 'en' })
.then(res => { console.log(res.text) })
.catch(console.log);

How to run it...

I'am a linux developer and i am learn Python and want to developent a dict ...

but...i dont know that how can i run this project. i tried node index.js but fail

How to return result from .then() function

I try as below code, but don't work:

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

var Tran = async (callback) => {
     return await Translate('test', {from: 'en', to: 'th'});
};
console.log("Before");
console.log(Tran());
console.log("After");

Result is:

Before
Promise { <pending> }
After

Have any idea?

Google translate in angular 5 not working

I want to add multi language support in my website.However most of the data is coming from API so I cant make a JSON of it. I am using Google Translate but it is not working. Can you please share steps as how to inject it in my angular 5 application? Much thanks in advance :)

BAD_NETWORK

{ Error
at /app/node_modules/google-translate-api/index.js:105:17
at process._tickCallback (internal/process/next_tick.js:109:7) code: 'BAD_NETWORK' }

This occurs at every translation request.

Release 2.1.0 and console logs

Hi,

Just tell you that lastest version has console logs

[ [ [ 'Hoi', 'hi', , , 1 ], [ , , , 'hΔ«' ] ],
  ,
  'en',
  ,
  ,
  [ [ 'hi',
      32000,
      [ [ 'Hoi', 1000, true, false ],
        [ 'Hi', 1000, true, false ],
        [ 'high', 0, true, false ],
        [ 'hallo', 0, true, false ],
        [ 'hifi', 0, true, false ] ],
      [ [ 0, 2 ] ],
      'hi',
      0,
      0 ] ],
  0.55577302,
  ,
  [ [ 'en', 'ja' ],
    ,
    [ 0.55577302, 0.33659491 ],
    [ 'en', 'ja-Latn' ] ],
  ,
  ,
  [ [ 'noun',
      [ [ [ 'howdy', 'hullo', 'how-do-you-do', 'hello' ], '' ] ],
      'hi' ] ],
  [ [ 'exclamation',
      [ [ 'used as a friendly greeting or to attract attention.',
          'm_en_us1254777.001',
          'β€œHi there. How was the flight?”' ] ],
      'hi' ],
    [ 'abbreviation',
      [ [ 'Hawaii (in official postal use).', 'm_en_us1254776.001' ] ],
      'HI' ] ],
  [ [ [ 'I\'d just like to say <b>hi</b> to my friends and family back home.',
        ,
        ,
        ,
        3,
        'm_en_us1254777.001' ],
      [ '<b>Hi</b> there everyone. My name\'s Jeb.',
        ,
        ,
        ,
        3,
        'm_en_us1254777.001' ],
      [ '<b>Hi</b> , Jessica, how are you?',
        ,
        ,
        ,
        3,
        'm_en_us1254777.001' ],
      [ 'β€˜ <b>Hi</b> , is Danielle there?’ asked a guy\'s voice.',
        ,
        ,
        ,
        3,
        'm_en_us1254777.001' ] ] ],
  [ [ 'Hi!',
      'HI',
      'say hi',
      'hi-fi',
      'hi-tech',
      'hi, how are you?',
      'hi tech' ] ] ]

Different BAD_NETWORK issue?

node: 8.1.4
npm: 5.3.0

{ Error: unable to get local issuer certificate
    at /Users/xxx/test/nodejs/node_modules/google-translate-token/index.js:103:25
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:169:7)
  code: 'BAD_NETWORK',
  message: 'unable to get local issuer certificate' }

Empty array when I put translate into a function

Here is my code:

function translateWord(word, baselanguage, translatelanguage) {

return translate(word, {from:  baselanguage, to: translatelanguage, raw: true});

}

translateMot("bonjour", "fr", "en").then(word => {console.log(word.raw)}).catch(err => {
    console.error(err);
});

And here is what I get:
[[["",""]],null,"fr",null,null,null,0,null,[["fr"],null,[0],["fr"]]]

But when I call "translate" outside this works perfectly.
Anyone know how I can handle this?

Why it's unlimited?

I estimate we will invoke 100K times and totally 34 million characters per month, does this support?

Error while installing the js file

Hi Guys

I am facing the below issue when running the "npm install --save google-translate-api" command

npm ERR! Refusing to install google-translate-api as a dependency of itself

Can any one help me in sorting my issue

Shows error while build the project

After installing the package When I build the project it shows below error:
@ .//got/index.js 19:12-32
@ ./
/google-translate-api/index.js
@ ./src/app/ui/common/address-info.component.ts
@ ./src/app/app.module.ts
@ ./src/main.ts
@ multi webpack-dev-server/client?http://localhost:4200/ ./src/main.ts

Please let me know any solution of how to tackle with this error.
Thanks in advance.

Service Unavailable

Error: Response code 503 (Service Unavailable)
at /home/mayank/Desktop/node_modules/google-translate-token/index.js:103:25
at process._tickCallback (internal/process/next_tick.js:109:7)
code: 'BAD_NETWORK',
message: 'Response code 503 (Service Unavailable)'

Error: getaddrinfo ENOTFOUND translate.google.com translate.google.com:443
at /home/mayank/Desktop/node_modules/google-translate-token/index.js:103:25
at process._tickCallback (internal/process/next_tick.js:109:7)
code: 'BAD_NETWORK',
message: 'getaddrinfo ENOTFOUND translate.google.com translate.google.com:443'

Can't translate for Hindi Language

var toLang = 'hi';
translate(engText, {from:'en',to: toLang}).then(resp => {
var outStr = resp.text;
console.log("output - 1: " +outStr);
}).catch(err => {
console.error(err);
});

Advice on how to import for React

Hi,

In my React project, spun up with Create React App, I have done:

npm install --save google-translate-api

But when I try to:

import Translate from 'google-translate-api'

I get the following error:

TypeError: Cannot read property 'prototype' of undefined

Any idea on how to solve or what I could be doing wrong? Thank you.

Error on request..

Hi Matheuss,

I have this error on request to google translate without api key,

what does it mean?:

{ Error: socket hang up at TLSSocket.onHangUp (_tls_wrap.js:1124:19) at TLSSocket.g (events.js:292:16) at emitNone (events.js:91:20) at TLSSocket.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12) at _combinedTickCallback (internal/process/next_tick.js:80:11) at process._tickDomainCallback (internal/process/next_tick.js:128:9) code: 'ECONNRESET' }

thanks for your answer :)

Number of characters per months ?

Hi matheuss,

I would test it myself but is there a limitation of characters like with the API. (1 million, 2 millions... 500 millions ?)

Thanks a lot for your response.

Laurent Boukhadcha

China doesn't work: Default URL banned, no proxy support, alternate URL not working

In China the regular page is banned, normally we have to use translate.google.cn instead.

No URL spec option
There doesn't seem to be an option to redirect the queries to a new URL (China's is probably the only meaningful one out there).

I tried manually modifying the URL in my version of the library but it didn't work. Unfamiliar with node debugging so unsure how to get low level HTTP response information. However, this is what it looks like from a browser.

google-translate-china

No proxy option
I looked for a proxy option but apparently that doesn't exit either. I also tried setting the standard unix environment variables: proxy, http_proxy, https_proxy, proxy_all, etc. but none appeared to take effect.

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.