UNPKG

mtengines

Version:

Machine Translation (MT) library written in TypeScript

159 lines 5.8 kB
/******************************************************************************* * Copyright (c) 2023-2026 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 *******************************************************************************/ import { LanguageUtils } from "typesbcp47"; import { Constants } from "./Constants.js"; import { MTMatch } from "./MTMatch.js"; import { MTUtils } from "./MTUtils.js"; export 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) { if (this.srcLang === '' || this.tgtLang === '') { return Promise.reject(new Error('Source and Target languages must be set before translation.')); } 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.TOOL + ' ' + 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 { let json = await response.json().catch(() => null); let message = json?.message ?? response.statusText; reject(new Error(message)); } }).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) { if (language.language) { const code = LanguageUtils.normalizeCode(language.language); if (code) { languages.push(code); } } } resolve(languages); } else { let json = await response.json().catch(() => null); let message = json?.message ?? (response.status + ': ' + response.statusText); reject(new Error(message)); } }).catch((error) => { reject(error); }); }); } getMTMatch(source, terms) { return new Promise((resolve, reject) => { let content = MTUtils.getElementContent(source); this.translate(content).then((translation) => { try { let target = MTUtils.toXMLElement('<target>' + translation + '</target>'); let space = source.getAttribute('xml:space'); if (space) { target.setAttribute(space); } resolve(new 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')); } } //# sourceMappingURL=DeepLTranslator.js.map