google-translate-api-nodejs-client
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,6 +0,5 @@ | ||
var config = require('./config'), | ||
request = require('request'), | ||
var request = require('request'), | ||
Logger = require('logb').getLogger(module.filename); | ||
var API_KEY = config.GOOGLE_TRANSLATE_API_KEY, | ||
var API_KEY = process.env.GOOGLE_TRANSLATE_API_KEY, | ||
URL = 'https://www.googleapis.com/language/translate/v2/languages?key=' + API_KEY; | ||
@@ -7,0 +6,0 @@ |
var request = require('request'), | ||
config = require('./config'), | ||
Logger = require('logb').getLogger(module.filename), | ||
@@ -7,10 +6,16 @@ Entities = require('html-entities').AllHtmlEntities, | ||
var API_KEY = config.GOOGLE_TRANSLATE_API_KEY, | ||
URL = 'https://www.googleapis.com/language/translate/v2'; | ||
function translate(opts, cb) { | ||
var GoogleTranslateApi = module.exports = function GoogleTranslateApi(options) { | ||
this.API_KEY = options.API_KEY; | ||
this.URL = 'https://www.googleapis.com/language/translate/v2'; | ||
} | ||
GoogleTranslateApi.prototype.translate = function translate(opts, cb) { | ||
var self = this; | ||
request.post({ | ||
url: URL, | ||
url: self.URL, | ||
headers: { | ||
@@ -20,3 +25,3 @@ 'X-HTTP-Method-Override': 'GET' | ||
form: { | ||
key: API_KEY, | ||
key: self.API_KEY, | ||
q: opts.text, | ||
@@ -34,3 +39,5 @@ source: opts.source, | ||
} | ||
body = JSON.parse(body); | ||
translation = body.data.translations[0].translatedText; | ||
@@ -45,9 +52,8 @@ translation = entities.decode(translation); | ||
module.exports.translate = translate; | ||
module.exports.fromGermanToEnglish = function(text, cb) { | ||
GoogleTranslateApi.prototype.fromGermanToEnglish = function fromGermanToEnglish(text, cb) { | ||
Logger.info('Translating from German to English...'); | ||
translate({ | ||
this.translate({ | ||
source: 'de', | ||
@@ -54,0 +60,0 @@ target: 'en', |
{ | ||
"name": "google-translate-api-nodejs-client", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "google-translate-api-nodejs-client.js", |
@@ -8,12 +8,18 @@ # google-translate-api-nodejs-client | ||
``` | ||
translate({ | ||
source:'en', | ||
target:'ge', | ||
q:'...text is here...' | ||
}, function(err,translation){ | ||
var GoogleTranslateApi=require('google-translate-api-nodejs-client'); | ||
new GoogleTranslateApi({ | ||
API_KEY:'...' | ||
}) | ||
.translate({ | ||
source:'en', | ||
target:'ge', | ||
q:'...text is here...' | ||
}, function(err,translation){ | ||
// translation is a string representing the output translation | ||
// original response would be in html, this client converts the html to a proper text string | ||
// translation is a string representing the output translation | ||
// original response would be in html, this client converts the html to a proper text string | ||
}); | ||
}); | ||
``` | ||
@@ -25,3 +31,3 @@ | ||
cd google-translate-api-nodejs-client | ||
node availableLanguages.js | ||
GOOGLE_TRANSLATE_API_KEY=... node availableLanguages.js | ||
``` |
@@ -19,4 +19,2 @@ var expect = require('chai').expect, | ||
}); | ||
@@ -26,9 +24,16 @@ | ||
if (!process.env.GOOGLE_TRANSLATE_API_KEY) { | ||
throw new Error('Please specify the env variable GOOGLE_TRANSLATE_API_KEY'); | ||
return; | ||
} | ||
GoogleTranslateApiClient.fromGermanToEnglish(germanText, function(err, translation) { | ||
expect(err).to.be.null; | ||
expect(translation).to.be.not.undefined; | ||
expect(translation).to.be.eql(germanTextEnglishTranslation); | ||
done(); | ||
}); | ||
new GoogleTranslateApiClient({ | ||
API_KEY: process.env.GOOGLE_TRANSLATE_API_KEY | ||
}) | ||
.fromGermanToEnglish(germanText, function(err, translation) { | ||
expect(err).to.be.null; | ||
expect(translation).to.be.not.undefined; | ||
expect(translation).to.be.eql(germanTextEnglishTranslation); | ||
done(); | ||
}); | ||
@@ -35,0 +40,0 @@ |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
4683
81
32
3