mtengines
Version:
Machine Translation (MT) library written in TypeScript
134 lines • 4.4 kB
JavaScript
/*******************************************************************************
* 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 { MTMatch } from "./MTMatch.js";
import { MTUtils } from "./MTUtils.js";
export 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) {
if (this.srcLang === '' || this.tgtLang === '') {
return Promise.reject(new Error('Source and Target languages must be set before translation.'));
}
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));
}
}
else {
reject(new Error(response.statusText));
}
}).catch((error) => {
reject(error);
});
});
}
getMTMatch(source, terms) {
return new Promise((resolve, reject) => {
this.translate(MTUtils.getElementContent(source)).then((translation) => {
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) => {
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'));
}
}
//# sourceMappingURL=ModernMTTranslator.js.map