@translated/lara
Version:
Official Lara SDK for JavaScript and Node.js
136 lines (135 loc) • 6.61 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Translator = void 0;
const audioTranslator_1 = require("./audioTranslator");
const documents_1 = require("./documents");
const glossaries_1 = require("./glossaries");
const imageTranslator_1 = require("./imageTranslator");
const memories_1 = require("./memories");
const lara_1 = __importStar(require("./net/lara"));
const styleguides_1 = require("./styleguides");
const defaultBaseUrl_1 = require("./utils/defaultBaseUrl");
const sdk_version_1 = require("./utils/sdk-version");
class Translator {
constructor(auth, options) {
this.client = (0, lara_1.default)(auth, options === null || options === void 0 ? void 0 : options.serverUrl, options === null || options === void 0 ? void 0 : options.keepAlive, options === null || options === void 0 ? void 0 : options.connectionTimeoutMs);
this.memories = new memories_1.Memories(this.client);
this.documents = new documents_1.Documents(this.client);
this.glossaries = new glossaries_1.Glossaries(this.client);
this.styleguides = new styleguides_1.Styleguides(this.client);
this.audio = new audioTranslator_1.AudioTranslator(this.client);
this.images = new imageTranslator_1.ImageTranslator(this.client);
}
get version() {
return sdk_version_1.version;
}
async getLanguages() {
return await this.client.get("/v2/languages");
}
async translate(text, source, target, options, callback) {
const headers = {};
if (options === null || options === void 0 ? void 0 : options.headers) {
for (const [name, value] of Object.entries(options.headers)) {
headers[name] = value;
}
}
if (options === null || options === void 0 ? void 0 : options.noTrace) {
headers["X-No-Trace"] = "true";
}
const response = this.client.postAndGetStream("/v2/translate", {
q: text,
source,
target,
source_hint: options === null || options === void 0 ? void 0 : options.sourceHint,
content_type: options === null || options === void 0 ? void 0 : options.contentType,
multiline: (options === null || options === void 0 ? void 0 : options.multiline) !== false,
adapt_to: options === null || options === void 0 ? void 0 : options.adaptTo,
glossaries: options === null || options === void 0 ? void 0 : options.glossaries,
instructions: options === null || options === void 0 ? void 0 : options.instructions,
timeout: options === null || options === void 0 ? void 0 : options.timeoutInMillis,
priority: options === null || options === void 0 ? void 0 : options.priority,
use_cache: options === null || options === void 0 ? void 0 : options.useCache,
cache_ttl: options === null || options === void 0 ? void 0 : options.cacheTTLSeconds,
verbose: options === null || options === void 0 ? void 0 : options.verbose,
style: options === null || options === void 0 ? void 0 : options.style,
reasoning: options === null || options === void 0 ? void 0 : options.reasoning,
metadata: options === null || options === void 0 ? void 0 : options.metadata,
profanities_detect: options === null || options === void 0 ? void 0 : options.profanitiesDetect,
profanities_handling: options === null || options === void 0 ? void 0 : options.profanitiesHandling,
styleguide_id: options === null || options === void 0 ? void 0 : options.styleguideId,
styleguide_reasoning: options === null || options === void 0 ? void 0 : options.styleguideReasoning,
styleguide_explanation_language: options === null || options === void 0 ? void 0 : options.styleguideExplanationLanguage
}, undefined, headers);
let lastResult;
for await (const partial of response) {
if ((options === null || options === void 0 ? void 0 : options.reasoning) && callback)
callback(partial);
lastResult = partial;
}
if (!lastResult)
throw new Error("No translation result received.");
return lastResult;
}
async detect(text, hint, passlist) {
return await this.client.post("/v2/detect/language", {
q: text,
hint,
passlist
});
}
async detectProfanities(text, language, contentType) {
return await this.client.post("/v2/detect/profanities", {
text,
language,
content_type: contentType
});
}
async qualityEstimation(source, target, sentence, translation) {
return await this.client.post("/v2/detect/quality-estimation", {
source,
target,
sentence,
translation
});
}
static async getLoginUrl(serverUrl) {
if (!serverUrl)
serverUrl = defaultBaseUrl_1.DEFAULT_BASE_URL;
const { body } = await lara_1.HttpClient.get(`${serverUrl.replace(/\/+$/, "")}/v2/auth/login-page`);
return body;
}
}
exports.Translator = Translator;