-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d535786
commit 2241527
Showing
6 changed files
with
71 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const getSupportedLanguagesWithNames = (supportedLanguages) => { | ||
return Object.entries(supportedLanguages).map(([language, data]) => { | ||
return `${language} -> ${data.name}` | ||
}) | ||
} | ||
|
||
module.exports = { | ||
getSupportedLanguagesWithNames | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |