@siamf/google-translate
Version:
A nodejs package for translating text using google translate for free! It also support batching translation!
82 lines • 4.02 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
//Constructing translate
const translate = {};
//Translating Batch
translate.batch = (text, targets, options) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const translationPromises = targets.map((target) => __awaiter(void 0, void 0, void 0, function* () {
var _b;
const response = yield fetch(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${(_b = options === null || options === void 0 ? void 0 : options.from) !== null && _b !== void 0 ? _b : "auto"}&tl=${target}&dt=t&q=${encodeURIComponent(text)}`);
const data = yield response.json();
return {
lan: target,
text: data[0][0][0]
};
}));
const translations = yield Promise.all(translationPromises);
return {
data: {
source: {
lan: (_a = options === null || options === void 0 ? void 0 : options.from) !== null && _a !== void 0 ? _a : "auto",
text: text
},
target: translations
}
};
});
//Translating Single
translate.single = (text, target, options) => __awaiter(void 0, void 0, void 0, function* () {
var _c, _d;
const response = yield fetch(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${(_c = options === null || options === void 0 ? void 0 : options.from) !== null && _c !== void 0 ? _c : "auto"}&tl=${target}&dt=t&q=${encodeURIComponent(text)}`);
const data = yield response.json();
return {
data: {
source: {
lan: (_d = options === null || options === void 0 ? void 0 : options.from) !== null && _d !== void 0 ? _d : "auto",
text: text
},
target: {
lan: target,
text: data[0][0][0]
}
}
};
});
//Translating Multiple
translate.multi = (text, targets, options) => __awaiter(void 0, void 0, void 0, function* () {
var _e;
const translationPromises = targets.map((target) => __awaiter(void 0, void 0, void 0, function* () {
const translatedFieldsPromises = Object.entries(text).map(([fieldName, fieldValue]) => __awaiter(void 0, void 0, void 0, function* () {
var _f;
const response = yield fetch(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${(_f = options === null || options === void 0 ? void 0 : options.from) !== null && _f !== void 0 ? _f : "auto"}&tl=${target}&dt=t&q=${encodeURIComponent(fieldValue)}`);
const data = yield response.json();
return {
[fieldName]: data[0][0][0]
};
}));
const translatedFields = yield Promise.all(translatedFieldsPromises);
const translatedObject = Object.assign({}, ...translatedFields);
return Object.assign({ languageCode: target }, translatedObject);
}));
const translations = yield Promise.all(translationPromises);
return {
data: {
source: {
lan: (_e = options === null || options === void 0 ? void 0 : options.from) !== null && _e !== void 0 ? _e : "auto"
},
target: translations
}
};
});
exports.default = translate;
//# sourceMappingURL=translate.js.map