UNPKG

mtengines

Version:

Machine Translation (MT) library written in TypeScript

140 lines 4.84 kB
"use strict"; /******************************************************************************* * 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.GoogleTranslator = void 0; const typesxml_1 = require("typesxml"); const MTMatch_1 = require("./MTMatch"); const MTUtils_1 = require("./MTUtils"); 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) { 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_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 Google Cloud Translation')); } fixesTags() { return false; } fixTags(source, target) { return Promise.reject(new Error('fixTags not implemented for Google Cloud Translation')); } } exports.GoogleTranslator = GoogleTranslator; //# sourceMappingURL=GoogleTranslator.js.map