google-translate-api-nodejs-client
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -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", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
136
6765