From 22415276c9984c6ebec2ba0ede76c34e31a2b400 Mon Sep 17 00:00:00 2001 From: victor-heliomar Date: Sat, 11 Nov 2023 09:41:04 -0400 Subject: [PATCH] added new validations to the code --- cli.js | 3 +- controllers/translateController.js | 10 +++--- services/translateService.js | 34 +++++++++++++------- utils/formatSupportedLanguages.js | 9 ++++++ utils/validateLanguageIsSupportedByEngine.js | 11 +++++++ utils/validateLanguageRequested.js | 23 +++++++++++++ 6 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 utils/formatSupportedLanguages.js create mode 100644 utils/validateLanguageIsSupportedByEngine.js create mode 100644 utils/validateLanguageRequested.js diff --git a/cli.js b/cli.js index 3a0ddca..999e08e 100755 --- a/cli.js +++ b/cli.js @@ -2,7 +2,6 @@ const { Command, Argument } = require('commander'); const program = new Command(); -const supportedLanguages = require('./SUPPORTED-LANGUAGE.json'); const { configPath } = require('./utils/getConfigPath'); const { translateController } = require('./controllers/translateController'); @@ -14,7 +13,7 @@ program program.command('translate') .description('Translate a text and put the result on the files in the output directory') .argument('', 'Text to translate') - .addArgument(new Argument('', 'Source language of the string').choices(supportedLanguages)) + .argument('', 'Source language of the string') .argument('', 'Name of your translation') .option('-e, --engine ', "Engine to use for the translation. In case you don't define it will be use by default all the translation engines that are free and doesn't requires API Key.") .option('-s, --settings-file ', 'Path to the settings file', configPath) diff --git a/controllers/translateController.js b/controllers/translateController.js index 11b94cf..b781813 100644 --- a/controllers/translateController.js +++ b/controllers/translateController.js @@ -4,17 +4,17 @@ const { validateSettingsFile } = require('../utils/validateSettingsFile'); const { dset: setDeepValue } = require('dset'); const { validateAndPromptUserJSONFiles } = require('../utils/validateAndPromptUserJSONFiles'); const { setTranslateWithFallbackEngines, isEngineValid } = require('../services/translateService'); +const { validateLanguageRequested } = require('../utils/validateLanguageRequested'); const translateController = async (text, sourceLanguage, nameOfTranslation, options) => { const settingsFilePath = parsePath(options.settingsFile); - const isValidSettingsFile = validateSettingsFile(settingsFilePath); - - if (!isValidSettingsFile) throw new Error('Invalid settings file'); + validateSettingsFile(settingsFilePath); + validateLanguageRequested(sourceLanguage, options.engine); + if (!text) throw new Error('No text to translate provided'); - if (!sourceLanguage) throw new Error('No source language provided'); if (!nameOfTranslation) throw new Error('No name of translation provided'); - const { languages, basePath, translationEngines: settingsTranslationEngines } = require(settingsFilePath); + const { languages, basePath, translationEngines: settingsTranslationEngines } = require(settingsFilePath); if (options.engine && !isEngineValid(options.engine)) throw new Error(`You've provided an invalid engine as arg on your CLI Command. Try with one of these: ${validEngines.join(', ')}`); diff --git a/services/translateService.js b/services/translateService.js index 14a911e..13b5b9b 100644 --- a/services/translateService.js +++ b/services/translateService.js @@ -6,6 +6,7 @@ const { translate: libreTranslate } = require("./libreTranslate"); const { getTranslationEnginesToUse, } = require("../utils/getTranslationEnginePreferences"); +const { validateLanguageIsSupportedByEngine } = require("../utils/validateLanguageIsSupportedByEngine"); const translateEngines = { google: googleTranslate, @@ -76,18 +77,27 @@ const setTranslateWithFallbackEngines = ({ ); for await (const engine of enginesFiltered) { - await translate(text, from, to, engine) - .then(({ text }) => { - result = text; - }) - .catch(() => { - console.log( - `Error translating with ${engine} engine. Trying next engine...` - ); - enginesFailed.push(engine); - }); - - if (result) break; + try { + // Validate that the language is supported by the engine to avoid unnecessary network requests + validateLanguageIsSupportedByEngine(from, engine); + validateLanguageIsSupportedByEngine(to, engine); + + await translate(text, from, to, engine) + .then(({ text }) => { + result = text; + console.log(`Translated successfully with ${engine} engine. Result: ${text}`) + }) + .catch(() => { + console.log( + `Error translating with ${engine} engine. Trying next engine...` + ); + enginesFailed.push(engine); + }); + + if (result) break; + } catch (error) { + console.log(error.message); + } } if (!result) { diff --git a/utils/formatSupportedLanguages.js b/utils/formatSupportedLanguages.js new file mode 100644 index 0000000..70fd2e4 --- /dev/null +++ b/utils/formatSupportedLanguages.js @@ -0,0 +1,9 @@ +const getSupportedLanguagesWithNames = (supportedLanguages) => { + return Object.entries(supportedLanguages).map(([language, data]) => { + return `${language} -> ${data.name}` + }) +} + +module.exports = { + getSupportedLanguagesWithNames +} diff --git a/utils/validateLanguageIsSupportedByEngine.js b/utils/validateLanguageIsSupportedByEngine.js new file mode 100644 index 0000000..bde3ed3 --- /dev/null +++ b/utils/validateLanguageIsSupportedByEngine.js @@ -0,0 +1,11 @@ +const supportedLanguages = require('../SUPPORTED-LANGUAGES.json'); + +const validateLanguageIsSupportedByEngine = (requestedLanguage, engine) => { + const isLanguageSupportedByEngine = Object.keys(supportedLanguages[requestedLanguage]).includes(engine); + + if (!isLanguageSupportedByEngine) throw new Error(`Language ${requestedLanguage} is not supported by this ${engine}.`); + + return true; +} + +module.exports = { validateLanguageIsSupportedByEngine } diff --git a/utils/validateLanguageRequested.js b/utils/validateLanguageRequested.js new file mode 100644 index 0000000..485b797 --- /dev/null +++ b/utils/validateLanguageRequested.js @@ -0,0 +1,23 @@ +const supportedLanguages = require('../SUPPORTED-LANGUAGES.json'); +const { validEngines } = require('../services/translateService'); +const { getSupportedLanguagesWithNames } = require('./formatSupportedLanguages'); + +const validateLanguageRequested = (requestedLanguage) => { + try { + if (!requestedLanguage) throw new Error('No language provided.'); + + const isLanguageSupported = supportedLanguages[requestedLanguage] !== undefined; + + if (!isLanguageSupported) throw new Error(`Language ${requestedLanguage} is not supported.`); + + const isLanguageSupportedByAlmostOneEngine = Object.keys(supportedLanguages[requestedLanguage]).some(value => validEngines.includes(value)); + + if (!isLanguageSupportedByAlmostOneEngine) throw new Error(`Language ${requestedLanguage} is not supported by any engine.`); + + return true; + } catch (error) { + throw new Error(`${error.message}. \n\nPlease use one of these:\n\n${getSupportedLanguagesWithNames(supportedLanguages).join('\n')}`) + } +} + +module.exports = { validateLanguageRequested }