UNPKG

mtengines

Version:

Machine Translation (MT) library written in TypeScript

150 lines 5.35 kB
"use strict"; /******************************************************************************* * Copyright (c) 2023 - 2025 Maxprograms. * * This program and the accompanying materials * are made available under the terms of the Eclipse License 1.0 * which accompanies this distribution, and is available at * https://www.eclipse.org/org/documents/epl-v10.html * * Contributors: * Maxprograms - initial API and implementation *******************************************************************************/ Object.defineProperty(exports, "__esModule", { value: true }); exports.DeepLTranslator = void 0; const typesbcp47_1 = require("typesbcp47"); const Constants_1 = require("./Constants"); const MTMatch_1 = require("./MTMatch"); const MTUtils_1 = require("./MTUtils"); class DeepLTranslator { srcLang; tgtLang; apiKey; proPlan; translateUrl; languageUrl; constructor(apiKey) { this.apiKey = apiKey; this.proPlan = !apiKey.endsWith(':fx'); this.translateUrl = this.proPlan ? "https://api.deepl.com/v1/translate" : "https://api-free.deepl.com/v2/translate"; this.languageUrl = this.proPlan ? "https://api.deepl.com/v1/languages?type=" : "https://api-free.deepl.com/v2/languages?type="; } getName() { return 'DeepL API'; } getShortName() { return 'DeepL'; } getSourceLanguages() { return this.getLanguages('source'); } getTargetLanguages() { return this.getLanguages('target'); } setSourceLanguage(lang) { this.srcLang = lang; } getSourceLanguage() { return this.srcLang; } setTargetLanguage(lang) { this.tgtLang = lang; } getTargetLanguage() { return this.tgtLang; } translate(source) { let params = "&text=" + encodeURIComponent(source) + "&source_lang=" + this.srcLang.toUpperCase() + "&target_lang=" + this.tgtLang.toUpperCase() + "&tag_handling=xml&split_sentences=nonewlines"; return new Promise((resolve, reject) => { fetch(this.translateUrl, { method: 'POST', headers: [ ['Authorization', 'DeepL-Auth-Key ' + this.apiKey], ['User-Agent', Constants_1.Constants.TOOL + ' ' + Constants_1.Constants.VERSION], ['Content-Type', 'application/x-www-form-urlencoded'], ['Accept', 'application/json'], ['Content-Length', params.length.toString()] ], body: params }).then(async (response) => { if (response.ok) { let json = await response.json(); resolve(json.translations[0].text); } else { reject(new Error(response.statusText)); } }).catch((error) => { reject(error); }); }); } getLanguages(type) { return new Promise((resolve, reject) => { fetch(this.languageUrl + type, { method: 'GET', headers: [ ['Authorization', 'DeepL-Auth-Key ' + this.apiKey] ] }).then(async (response) => { if (response.status === 200) { let json = await response.json(); let languages = []; for (let language of json) { languages.push(typesbcp47_1.LanguageUtils.normalizeCode(language.language)); } resolve(languages); } else { reject(new Error(response.status + ': ' + response.statusText)); } }).catch((error) => { reject(error); }); }); } getMTMatch(source, terms) { return new Promise((resolve, reject) => { let content = MTUtils_1.MTUtils.getElementContent(source); this.translate(content).then((translation) => { try { let target = MTUtils_1.MTUtils.toXMLElement('<target>' + translation + '</target>'); if (source.hasAttribute('xml:space')) { target.setAttribute(source.getAttribute('xml:space')); } resolve(new MTMatch_1.MTMatch(source, target, this.getShortName())); } catch (error) { if (error instanceof Error) { reject(error); return; } reject(error); } }).catch((error) => { reject(error); }); }); } handlesTags() { return true; } fixesMatches() { return false; } fixMatch(originalSource, matchSource, matchTarget) { return Promise.reject(new Error('fixMatch not implemented for DeepL API')); } fixesTags() { return false; } fixTags(source, target) { return Promise.reject(new Error('fixTags not implemented for DeepL API')); } } exports.DeepLTranslator = DeepLTranslator; //# sourceMappingURL=DeepLTranslator.js.map