mtengines
Version:
Machine Translation (MT) library written in TypeScript
135 lines • 4.45 kB
JavaScript
;
/*******************************************************************************
* 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.ModernMTTranslator = void 0;
const MTMatch_1 = require("./MTMatch");
const MTUtils_1 = require("./MTUtils");
class ModernMTTranslator {
apiKey;
srcLang;
tgtLang;
constructor(apiKey) {
this.apiKey = apiKey;
}
getName() {
return 'ModernMT';
}
getShortName() {
return 'ModernMT';
}
getLanguages() {
return new Promise((resolve, reject) => {
fetch('https://api.modernmt.com/translate/languages').then(async (response) => {
if (response.ok) {
let json = await response.json();
if (json.status == 200) {
let data = json.data;
data.sort(new Intl.Collator('en').compare);
resolve(data);
}
else {
reject(new Error(json.error.message));
}
}
else {
reject(new Error(response.statusText));
}
}).catch((error) => {
reject(error);
});
});
}
getSourceLanguages() {
return this.getLanguages();
}
getTargetLanguages() {
return this.getLanguages();
}
setSourceLanguage(lang) {
this.srcLang = lang;
}
getSourceLanguage() {
return this.srcLang;
}
setTargetLanguage(lang) {
this.tgtLang = lang;
}
getTargetLanguage() {
return this.tgtLang;
}
translate(source) {
let json = {
"source": this.srcLang,
"target": this.tgtLang,
"q": source
};
let params = JSON.stringify(json);
return new Promise((resolve, reject) => {
fetch('https://api.modernmt.com/translate', {
method: 'POST',
headers: [
['MMT-ApiKey', this.apiKey],
['X-HTTP-Method-Override', 'GET'],
['Content-Type', 'application/json']
],
body: params
}).then(async (response) => {
if (response.ok) {
let json = await response.json();
if (json.status === 200) {
resolve(json.data.translation);
}
else {
reject(new Error(json.error.message));
}
resolve(json.translations[0].text);
}
else {
reject(new Error(response.statusText));
}
}).catch((error) => {
reject(error);
});
});
}
getMTMatch(source, terms) {
return new Promise((resolve, reject) => {
this.translate(MTUtils_1.MTUtils.getElementContent(source)).then((translation) => {
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) => {
reject(error);
});
});
}
handlesTags() {
return true;
}
fixesMatches() {
return false;
}
fixMatch(originalSource, matchSource, matchTarget) {
return Promise.reject(new Error('fixMatch not implemented for ModernMT'));
}
fixesTags() {
return false;
}
fixTags(source, target) {
return Promise.reject(new Error('fixTags not implemented for ModernMT'));
}
}
exports.ModernMTTranslator = ModernMTTranslator;
//# sourceMappingURL=ModernMTTranslator.js.map