@iamtraction/google-translate
Advanced tools
Comparing version 1.1.2 to 2.0.0
{ | ||
"name": "@iamtraction/google-translate", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "A Node.JS library to consume Google Translate API for free.", | ||
@@ -8,3 +8,3 @@ "main": "src/index.js", | ||
"scripts": { | ||
"test:lint": "./node_modules/.bin/eslint .", | ||
"test:lint": "eslint .", | ||
"test": "npm run test:lint", | ||
@@ -26,10 +26,10 @@ "start": "node ." | ||
"engines": { | ||
"node": ">=8.0.0" | ||
"node": ">=16.0.0" | ||
}, | ||
"dependencies": { | ||
"got": "^11.8.1" | ||
"undici": "^5.11.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^7.18.0" | ||
"eslint": "^8.25.0" | ||
} | ||
} |
224
src/index.js
@@ -0,5 +1,6 @@ | ||
const querystring = require("querystring"); | ||
const { request } = require("undici"); | ||
const languages = require("./languages"); | ||
const tokenGenerator = require("./tokenGenerator"); | ||
const querystring = require("querystring"); | ||
const got = require("got"); | ||
@@ -13,139 +14,126 @@ /** | ||
async function translate(text, options) { | ||
try { | ||
if (typeof options !== "object") options = {}; | ||
text = String(text); | ||
if (typeof options !== "object") options = {}; | ||
text = String(text); | ||
// Check if a lanugage is in supported; if not, throw an error object. | ||
let error; | ||
[ options.from, options.to ].forEach((lang) => { | ||
if (lang && !languages.isSupported(lang)) { | ||
error = new Error(); | ||
error.code = 400; | ||
error.message = `The language '${lang}' is not supported.`; | ||
} | ||
}); | ||
if (error) throw error; | ||
// Check if a lanugage is in supported; if not, throw an error object. | ||
let error; | ||
[ options.from, options.to ].forEach((lang) => { | ||
if (lang && !languages.isSupported(lang)) { | ||
error = new Error(); | ||
error.code = 400; | ||
error.message = `The language '${lang}' is not supported.`; | ||
} | ||
}); | ||
if (error) throw error; | ||
// If options object doesn"t have "from" language, set it to "auto". | ||
if (!Object.prototype.hasOwnProperty.call(options, "from")) options.from = "auto"; | ||
// If options object doesn"t have "to" language, set it to "en". | ||
if (!Object.prototype.hasOwnProperty.call(options, "to")) options.to = "en"; | ||
// If options object has a "raw" property evaluating to true, set it to true. | ||
options.raw = Boolean(options.raw); | ||
// If options object doesn"t have "from" language, set it to "auto". | ||
if (!Object.prototype.hasOwnProperty.call(options, "from")) options.from = "auto"; | ||
// If options object doesn"t have "to" language, set it to "en". | ||
if (!Object.prototype.hasOwnProperty.call(options, "to")) options.to = "en"; | ||
// If options object has a "raw" property evaluating to true, set it to true. | ||
options.raw = Boolean(options.raw); | ||
// Get ISO 639-1 codes for the languages. | ||
options.from = languages.getISOCode(options.from); | ||
options.to = languages.getISOCode(options.to); | ||
// Get ISO 639-1 codes for the languages. | ||
options.from = languages.getISOCode(options.from); | ||
options.to = languages.getISOCode(options.to); | ||
// Generate Google Translate token for the text to be translated. | ||
let token = await tokenGenerator.generate(text); | ||
// Generate Google Translate token for the text to be translated. | ||
let token = await tokenGenerator.generate(text); | ||
// URL & query string required by Google Translate. | ||
let baseUrl = "https://translate.google.com/translate_a/single"; | ||
let data = { | ||
client: "gtx", | ||
sl: options.from, | ||
tl: options.to, | ||
hl: options.to, | ||
dt: [ "at", "bd", "ex", "ld", "md", "qca", "rw", "rm", "ss", "t" ], | ||
ie: "UTF-8", | ||
oe: "UTF-8", | ||
otf: 1, | ||
ssel: 0, | ||
tsel: 0, | ||
kc: 7, | ||
q: text, | ||
[token.name]: token.value | ||
}; | ||
// URL & query string required by Google Translate. | ||
let baseUrl = "https://translate.google.com/translate_a/single"; | ||
let data = { | ||
client: "gtx", | ||
sl: options.from, | ||
tl: options.to, | ||
hl: options.to, | ||
dt: [ "at", "bd", "ex", "ld", "md", "qca", "rw", "rm", "ss", "t" ], | ||
ie: "UTF-8", | ||
oe: "UTF-8", | ||
otf: 1, | ||
ssel: 0, | ||
tsel: 0, | ||
kc: 7, | ||
q: text, | ||
[token.name]: token.value | ||
}; | ||
// Append query string to the request URL. | ||
let url = `${baseUrl}?${querystring.stringify(data)}`; | ||
// Append query string to the request URL. | ||
let url = `${baseUrl}?${querystring.stringify(data)}`; | ||
let requestOptions; | ||
// If request URL is greater than 2048 characters, use POST method. | ||
if (url.length > 2048) { | ||
delete data.q; | ||
requestOptions = [ | ||
`${baseUrl}?${querystring.stringify(data)}`, | ||
{ | ||
method: "POST", | ||
form: { | ||
q: text | ||
} | ||
let requestOptions; | ||
// If request URL is greater than 2048 characters, use POST method. | ||
if (url.length > 2048) { | ||
delete data.q; | ||
requestOptions = [ | ||
`${baseUrl}?${querystring.stringify(data)}`, | ||
{ | ||
method: "POST", | ||
form: { | ||
q: text | ||
} | ||
]; | ||
} | ||
else { | ||
requestOptions = [ url ]; | ||
} | ||
} | ||
]; | ||
} | ||
else { | ||
requestOptions = [ url ]; | ||
} | ||
// Request translation from Google Translate. | ||
let response = await got(...requestOptions); | ||
// Request translation from Google Translate. | ||
let response = await request(...requestOptions); | ||
let body = await response.body.json(); | ||
let result = { | ||
text: "", | ||
from: { | ||
language: { | ||
didYouMean: false, | ||
iso: "" | ||
}, | ||
text: { | ||
autoCorrected: false, | ||
value: "", | ||
didYouMean: false | ||
} | ||
let result = { | ||
text: "", | ||
from: { | ||
language: { | ||
didYouMean: false, | ||
iso: "" | ||
}, | ||
raw: "" | ||
}; | ||
text: { | ||
autoCorrected: false, | ||
value: "", | ||
didYouMean: false | ||
} | ||
}, | ||
raw: "" | ||
}; | ||
// If user requested a raw output, add the raw response to the result | ||
if (options.raw) { | ||
result.raw = response.body; | ||
} | ||
// If user requested a raw output, add the raw response to the result | ||
if (options.raw) { | ||
result.raw = body; | ||
} | ||
// Parse string body to JSON and add it to result object. | ||
let body = JSON.parse(response.body); | ||
body[0].forEach((obj) => { | ||
if (obj[0]) { | ||
result.text += obj[0]; | ||
} | ||
}); | ||
if (body[2] === body[8][0][0]) { | ||
result.from.language.iso = body[2]; | ||
// Parse body and add it to the result object. | ||
body[0].forEach((obj) => { | ||
if (obj[0]) { | ||
result.text += obj[0]; | ||
} | ||
else { | ||
result.from.language.didYouMean = true; | ||
result.from.language.iso = body[8][0][0]; | ||
} | ||
}); | ||
if (body[7] && body[7][0]) { | ||
let str = body[7][0]; | ||
if (body[2] === body[8][0][0]) { | ||
result.from.language.iso = body[2]; | ||
} | ||
else { | ||
result.from.language.didYouMean = true; | ||
result.from.language.iso = body[8][0][0]; | ||
} | ||
str = str.replace(/<b><i>/g, "["); | ||
str = str.replace(/<\/i><\/b>/g, "]"); | ||
if (body[7] && body[7][0]) { | ||
let str = body[7][0]; | ||
result.from.text.value = str; | ||
str = str.replace(/<b><i>/g, "["); | ||
str = str.replace(/<\/i><\/b>/g, "]"); | ||
if (body[7][5] === true) { | ||
result.from.text.autoCorrected = true; | ||
} | ||
else { | ||
result.from.text.didYouMean = true; | ||
} | ||
} | ||
result.from.text.value = str; | ||
return result; | ||
} | ||
catch (e) { | ||
if (e.name === "HTTPError") { | ||
let error = new Error(); | ||
error.name = e.name; | ||
error.statusCode = e.statusCode; | ||
error.statusMessage = e.statusMessage; | ||
throw error; | ||
if (body[7][5] === true) { | ||
result.from.text.autoCorrected = true; | ||
} | ||
throw e; | ||
else { | ||
result.from.text.didYouMean = true; | ||
} | ||
} | ||
return result; | ||
} | ||
@@ -152,0 +140,0 @@ |
@@ -8,3 +8,3 @@ /** | ||
const got = require("got"); | ||
const { request } = require("undici"); | ||
@@ -70,30 +70,19 @@ /* eslint-disable */ | ||
async function updateTKK() { | ||
try { | ||
let now = Math.floor(Date.now() / 3600000); | ||
let now = Math.floor(Date.now() / 3600000); | ||
if (Number(window.TKK.split(".")[0]) !== now) { | ||
let res = await got("https://translate.google.com"); | ||
if (Number(window.TKK.split(".")[0]) !== now) { | ||
const response = await request("https://translate.google.com"); | ||
const body = await response.body.text(); | ||
// code will extract something like tkk:'1232135.131231321312', we need only value | ||
const code = res.body.match(/tkk:'\d+.\d+'/g); | ||
// code will extract something like tkk:'1232135.131231321312', we need only value | ||
const code = body.match(/tkk:'\d+.\d+'/g); | ||
if (code.length > 0) { | ||
// extracting value tkk:'1232135.131231321312', this will extract only token: 1232135.131231321312 | ||
const xt = code[0].split(":")[1].replace(/'/g, ""); | ||
if (code.length > 0) { | ||
// extracting value tkk:'1232135.131231321312', this will extract only token: 1232135.131231321312 | ||
const xt = code[0].split(":")[1].replace(/'/g, ""); | ||
window.TKK = xt; | ||
config.set("TKK", xt); | ||
} | ||
window.TKK = xt; | ||
config.set("TKK", xt); | ||
} | ||
} | ||
catch (e) { | ||
if (e.name === "HTTPError") { | ||
let error = new Error(); | ||
error.name = e.name; | ||
error.statusCode = e.statusCode; | ||
error.statusMessage = e.statusMessage; | ||
throw error; | ||
} | ||
throw e; | ||
} | ||
} | ||
@@ -100,0 +89,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
20803
389
+ Addedundici@^5.11.0
+ Added@fastify/busboy@2.1.1(transitive)
+ Addedundici@5.28.5(transitive)
- Removedgot@^11.8.1
- Removed@sindresorhus/is@4.6.0(transitive)
- Removed@szmarczak/http-timer@4.0.6(transitive)
- Removed@types/cacheable-request@6.0.3(transitive)
- Removed@types/http-cache-semantics@4.0.4(transitive)
- Removed@types/keyv@3.1.4(transitive)
- Removed@types/node@22.10.9(transitive)
- Removed@types/responselike@1.0.3(transitive)
- Removedcacheable-lookup@5.0.4(transitive)
- Removedcacheable-request@7.0.4(transitive)
- Removedclone-response@1.0.3(transitive)
- Removeddecompress-response@6.0.0(transitive)
- Removeddefer-to-connect@2.0.1(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedget-stream@5.2.0(transitive)
- Removedgot@11.8.6(transitive)
- Removedhttp-cache-semantics@4.1.1(transitive)
- Removedhttp2-wrapper@1.0.3(transitive)
- Removedjson-buffer@3.0.1(transitive)
- Removedkeyv@4.5.4(transitive)
- Removedlowercase-keys@2.0.0(transitive)
- Removedmimic-response@1.0.13.1.0(transitive)
- Removednormalize-url@6.1.0(transitive)
- Removedonce@1.4.0(transitive)
- Removedp-cancelable@2.1.1(transitive)
- Removedpump@3.0.2(transitive)
- Removedquick-lru@5.1.1(transitive)
- Removedresolve-alpn@1.2.1(transitive)
- Removedresponselike@2.0.1(transitive)
- Removedundici-types@6.20.0(transitive)
- Removedwrappy@1.0.2(transitive)