mtengines
Version:
Machine Translation (MT) library written in TypeScript
121 lines • 4.07 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 { XMLElement } from "typesxml";
import { MTMatch } from "./MTMatch.js";
import { MTUtils } from "./MTUtils.js";
export class AzureTranslator {
srcLang = '';
tgtLang = '';
apiKey;
constructor(apiKey) {
this.apiKey = apiKey;
}
getName() {
return 'Azure Translator Text';
}
getShortName() {
return 'Azure';
}
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 url = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=' + this.srcLang + '&to=' + this.tgtLang;
let params = [{
"Text": source
}];
let data = JSON.stringify(params);
return new Promise((resolve, reject) => {
fetch(url, {
method: 'POST',
headers: [
['Ocp-Apim-Subscription-Key', this.apiKey],
['Content-Type', 'application/json; charset=UTF-8']
],
body: data
}).then(async (response) => {
if (response.ok) {
let json = await response.json();
let array = json[0].translations;
let translation = array[0].text;
resolve(translation);
}
else {
reject(new Error(response.statusText));
}
}).catch((error) => {
reject(error);
});
});
}
getLanguages() {
return new Promise((resolve, reject) => {
fetch('https://api.cognitive.microsofttranslator.com/languages?api-version=3.0&scope=translation').then(async (response) => {
if (response.ok) {
let json = await response.json();
let translation = json.translation;
resolve(Object.keys(translation));
}
else {
reject(new Error(response.statusText));
}
}).catch((error) => {
reject(error);
});
});
}
getMTMatch(source, terms) {
return new Promise((resolve, reject) => {
this.translate(MTUtils.plainText(source)).then((translation) => {
let target = new XMLElement('target');
target.addString(translation);
resolve(new MTMatch(source, target, this.getShortName()));
}).catch((error) => {
reject(error);
});
});
}
handlesTags() {
return false;
}
fixesMatches() {
return false;
}
fixMatch(originalSource, matchSource, matchTarget) {
return Promise.reject(new Error('fixMatch not implemented for Azure Translator'));
}
fixesTags() {
return false;
}
fixTags(source, target) {
return Promise.reject(new Error('fixTags not implemented for Azure Translator'));
}
}
//# sourceMappingURL=AzureTranslator.js.map