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

@iamtraction/google-translate

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@iamtraction/google-translate - npm Package Compare versions

Comparing version 1.1.2 to 2.0.0

10

package.json
{
"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"
}
}

@@ -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

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