mtengines
Version:
Machine Translation (MT) library written in TypeScript
122 lines • 4.06 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.AzureTranslator = void 0;
const typesxml_1 = require("typesxml");
const MTMatch_1 = require("./MTMatch");
const MTUtils_1 = require("./MTUtils");
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) {
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_1.MTUtils.plainText(source)).then((translation) => {
let target = new typesxml_1.XMLElement('target');
target.addString(translation);
resolve(new MTMatch_1.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'));
}
}
exports.AzureTranslator = AzureTranslator;
//# sourceMappingURL=AzureTranslator.js.map