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

google-translate-api-nodejs-client

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

google-translate-api-nodejs-client - npm Package Compare versions

Comparing version 1.0.4 to 1.0.5

132

google-translate-api-nodejs-client.js

@@ -1,8 +0,10 @@

var request = require('request'),
Logger = require('logb').getLogger(module.filename),
Entities = require('html-entities').AllHtmlEntities,
entities = new Entities(),
RateLimiter = require('limiter').RateLimiter;
/* jshint node:true */
'use strict';
var request = require('request'),
Logger = require('logb').getLogger(module.filename),
Entities = require('html-entities').AllHtmlEntities,
entities = new Entities(),
RateLimiter = require('limiter').RateLimiter;

@@ -12,55 +14,55 @@

var GoogleTranslateApi = module.exports = function GoogleTranslateApi(options) {
// credentials
this.API_KEY = options.API_KEY;
this.URL = 'https://www.googleapis.com/language/translate/v2';
// throttling
this.limiter = new RateLimiter(1, 'second');
// credentials
this.API_KEY = options.API_KEY;
this.URL = 'https://www.googleapis.com/language/translate/v2';
// throttling
this.limiter = new RateLimiter(1, 'second');
Logger.info('Using API_KEY', this.API_KEY);
Logger.info('Using API_KEY', this.API_KEY);
};
GoogleTranslateApi.prototype.translate = function(opts, cb) {
var self = this;
var self = this;
self.limiter.removeTokens(1, function() {
self._translate(opts, cb);
});
}
self.limiter.removeTokens(1, function() {
self._translate(opts, cb);
});
};
GoogleTranslateApi.prototype._translate = function translate(opts, cb) {
var self = this;
var self = this;
request.post({
url: self.URL,
headers: {
'X-HTTP-Method-Override': 'GET'
},
form: {
key: self.API_KEY,
q: opts.text,
source: opts.source,
target: opts.target
}
}, function(err, response, body) {
request.post({
url: self.URL,
headers: {
'X-HTTP-Method-Override': 'GET'
},
form: {
key: self.API_KEY,
q: opts.text,
source: opts.source,
target: opts.target
}
}, function(err, response, body) {
var translation;
var translation;
if (err) {
cb(err);
return;
}
if (err) {
cb(err);
return;
}
body = JSON.parse(body);
body = JSON.parse(body);
if (!body.data || !body.data.translations) {
Logger.warn('response from Google Translate API returned weird body', body);
return cb(new Error('Weird response from Google Translate API'));
}
if (!body.data || !body.data.translations) {
Logger.warn('response from Google Translate API returned weird body', body);
return cb(new Error('Weird response from Google Translate API'));
}
translation = body.data.translations[0].translatedText;
translation = entities.decode(translation);
translation = body.data.translations[0].translatedText;
translation = entities.decode(translation);
return cb(null, translation);
return cb(null, translation);
});
});
};

@@ -72,23 +74,35 @@

Logger.info('Translating from German to English...');
Logger.info('Translating from German to English...');
this.translate({
source: 'de',
target: 'en',
text: text
}, cb);
this.translate({
source: 'de',
target: 'en',
text: text
}, cb);
return;
}
return;
};
GoogleTranslateApi.prototype.fromDutchToEnglish = function(text, cb) {
Logger.info('Translating from German to English...');
this.translate({
source: 'nl',
target: 'en',
text: text
}, cb);
return;
};
GoogleTranslateApi.prototype.fromSimplifiedChineseToEnglish = function(text, cb) {
Logger.info('Translating from German to English...');
Logger.info('Translating from German to English...');
this.translate({
source: 'zh-CN',
target: 'en',
text: text
}, cb);
this.translate({
source: 'zh-CN',
target: 'en',
text: text
}, cb);
return;
}
return;
};
{
"name": "google-translate-api-nodejs-client",
"version": "1.0.4",
"version": "1.0.5",
"description": "",

@@ -5,0 +5,0 @@ "main": "google-translate-api-nodejs-client.js",

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