mtengines
Version:
Machine Translation (MT) library written in TypeScript
139 lines • 4.85 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 GoogleTranslator {
apiKey;
srcLang = '';
tgtLang = '';
constructor(apiKey) {
// Google is not reporting language codes for base model, only for nmt
// Using the nmt model for now
this.apiKey = apiKey;
}
getName() {
return 'Google Cloud Translation';
}
getShortName() {
return 'Google';
}
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://www.googleapis.com/language/translate/v2?key=' + this.apiKey + '&q=' + encodeURIComponent(source)
+ "&source=" + this.srcLang + "&target=" + this.tgtLang + "&model=nmt";
return new Promise((resolve, reject) => {
fetch(url, {
method: 'GET'
}).then(async (response) => {
if (response.ok) {
let json = await response.json();
let data = json.data;
let translations = data.translations;
let transation = this.removeEntities(translations[0].translatedText);
resolve(transation);
}
else {
reject(new Error(response.statusText));
}
}).catch((error) => {
reject(error);
});
});
}
getLanguages() {
let url = 'https://translation.googleapis.com/language/translate/v2/languages?key=' + this.apiKey + "&model=nmt";
return new Promise((resolve, reject) => {
fetch(url, {
method: 'GET'
}).then(async (response) => {
if (response.ok) {
let json = await response.json();
let data = json.data;
let array = data.languages;
let languages = [];
for (let lang of array) {
languages.push(lang.language);
}
resolve(languages);
}
else {
reject(new Error(response.statusText));
}
}).catch((error) => {
reject(error);
});
});
}
removeEntities(text) {
let result = text;
const pattern = /&#[\d]+;/g;
let m = pattern.exec(result);
while (m !== null) {
const from = m.index;
const to = pattern.lastIndex;
const start = result.substring(0, from);
const entity = result.substring(from + 2, to - 1);
const rest = result.substring(to);
result = start + String.fromCodePoint(parseInt(entity)) + rest;
m = pattern.exec(result);
}
return result;
}
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 Google Cloud Translation'));
}
fixesTags() {
return false;
}
fixTags(source, target) {
return Promise.reject(new Error('fixTags not implemented for Google Cloud Translation'));
}
}
//# sourceMappingURL=GoogleTranslator.js.map