UNPKG

@translated/lara

Version:

Official Lara SDK for JavaScript and Node.js

64 lines (63 loc) • 3.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ImageTranslator = void 0; const to_snake_case_1 = __importDefault(require("./utils/to-snake-case")); class ImageTranslator { constructor(client) { this.client = client; } async translate(file, source, target, options) { var _a; const headers = (options === null || options === void 0 ? void 0 : options.noTrace) ? { "X-No-Trace": "true" } : {}; headers["Content-Type"] = "multipart/form-data"; return this.client.post(`/v2/images/translate`, { source, target, adapt_to: JSON.stringify(options === null || options === void 0 ? void 0 : options.adaptTo), glossaries: JSON.stringify(options === null || options === void 0 ? void 0 : options.glossaries), style: options === null || options === void 0 ? void 0 : options.style, model: (_a = options === null || options === void 0 ? void 0 : options.model) !== null && _a !== void 0 ? _a : options === null || options === void 0 ? void 0 : options.textRemoval }, { image: file }, headers, true); } async translateText(file, source, target, options) { const headers = (options === null || options === void 0 ? void 0 : options.noTrace) ? { "X-No-Trace": "true" } : {}; headers["Content-Type"] = "multipart/form-data"; return this.client.post(`/v2/images/translate-text`, { source, target, adapt_to: JSON.stringify(options === null || options === void 0 ? void 0 : options.adaptTo), glossaries: JSON.stringify(options === null || options === void 0 ? void 0 : options.glossaries), style: options === null || options === void 0 ? void 0 : options.style, verbose: JSON.stringify(options === null || options === void 0 ? void 0 : options.verbose), include_layout: JSON.stringify(options === null || options === void 0 ? void 0 : options.includeLayout) }, { image: file }, headers); } async renderTranslated(file, source, target, paragraphs, model, noTrace) { // Select rendering fields so verbose translation matches are not sent back to the API. const renderParagraphs = paragraphs.map((paragraph) => ({ text: paragraph.text, translation: paragraph.translation, bbox: (0, to_snake_case_1.default)(paragraph.bbox), lines_bboxes: (0, to_snake_case_1.default)(paragraph.linesBboxes), text_info: (0, to_snake_case_1.default)(paragraph.textInfo), alignment: paragraph.alignment })); return this.client.post("/v2/images/render-translated", { source, target, paragraphs: JSON.stringify(renderParagraphs), model }, { image: file }, { "Content-Type": "multipart/form-data", ...(noTrace ? { "X-No-Trace": "true" } : {}) }, true); } } exports.ImageTranslator = ImageTranslator;