diff --git a/src/middleware.js b/src/middleware.js index 112b600..1c9383c 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -1,14 +1,14 @@ const facts = require("./models/facts"); const { convert } = require("sst"); -const localization = require("./models/localization"); +const { localizations } = require("./models/localizations"); let fullCode = []; let shortISO = []; // Dynamically fill the fullCode and shortISO list with the provided config information in the language files -for (const language in localization) { - fullCode.push(localization[language].code); - shortISO.push(localization[language].langISO); +for (const language in localizations) { + fullCode.push(localizations[language].code); + shortISO.push(localizations[language].langISO); } const VALID_LANGUAGES = shortISO.concat(fullCode); diff --git a/src/models/facts.js b/src/models/facts.js index 99f2585..89b8d87 100644 --- a/src/models/facts.js +++ b/src/models/facts.js @@ -1,4 +1,4 @@ -const localization = require("./localization"); +const { localizations, defaultLanguages } = require("./localizations"); /** * @@ -6,16 +6,20 @@ const localization = require("./localization"); * @returns {String} fact localized to the language */ function getLanguageFacts(requestedLang) { - for (const language in localization) { + if (requestedLang in defaultLanguages) { + return defaultLanguages[requestedLang].facts; + } + + for (const language in localizations) { if ( - localization[language].code === requestedLang || - localization[language].langISO === requestedLang + localizations[language].code === requestedLang || + localizations[language].langISO === requestedLang ) { - return localization[language].facts; + return localizations[language].facts; } } - return localization["eng-us"].facts; + return localizations["eng-us"].facts; } /** diff --git a/src/models/localization/ces-cz.js b/src/models/localizations/ces-cz.js similarity index 100% rename from src/models/localization/ces-cz.js rename to src/models/localizations/ces-cz.js diff --git a/src/models/localizations/defaultLanguages.js b/src/models/localizations/defaultLanguages.js new file mode 100644 index 0000000..bfc6e0e --- /dev/null +++ b/src/models/localizations/defaultLanguages.js @@ -0,0 +1,7 @@ +const espDefault = require("./esp-es"); + +const defaultLanguages = { + esp: espDefault, +}; + +module.exports = defaultLanguages; diff --git a/src/models/localization/eng-us.js b/src/models/localizations/eng-us.js similarity index 100% rename from src/models/localization/eng-us.js rename to src/models/localizations/eng-us.js diff --git a/src/models/localization/esp-es.js b/src/models/localizations/esp-es.js similarity index 100% rename from src/models/localization/esp-es.js rename to src/models/localizations/esp-es.js diff --git a/src/models/localization/esp-mx.js b/src/models/localizations/esp-mx.js similarity index 100% rename from src/models/localization/esp-mx.js rename to src/models/localizations/esp-mx.js diff --git a/src/models/localization/ger-de.js b/src/models/localizations/ger-de.js similarity index 100% rename from src/models/localization/ger-de.js rename to src/models/localizations/ger-de.js diff --git a/src/models/localization/index.js b/src/models/localizations/index.js similarity index 67% rename from src/models/localization/index.js rename to src/models/localizations/index.js index 9f01e7a..c7a8755 100644 --- a/src/models/localization/index.js +++ b/src/models/localizations/index.js @@ -1,8 +1,11 @@ const path = require("path"); const fs = require("fs"); +const defaultLanguages = require("./defaultLanguages"); + const localizations = {}; const basename = path.basename(module.filename); +const defaultLanguagesName = "defaultLanguages.js"; // Do a barrel export // Barrelling all the localization files into a single object @@ -12,7 +15,10 @@ const basename = path.basename(module.filename); fs.readdirSync(__dirname) .filter( (file) => - file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js" + file.indexOf(".") !== 0 && + file !== basename && + file !== defaultLanguagesName && + file.slice(-3) === ".js" ) .forEach((file) => { localizations[ @@ -20,4 +26,4 @@ fs.readdirSync(__dirname) ] = require(path.join(__dirname, file)); }); -module.exports = localizations; +module.exports = { localizations, defaultLanguages }; diff --git a/src/models/localization/rus-ru.js b/src/models/localizations/rus-ru.js similarity index 100% rename from src/models/localization/rus-ru.js rename to src/models/localizations/rus-ru.js diff --git a/src/models/localization/ukr-ua.js b/src/models/localizations/ukr-ua.js similarity index 100% rename from src/models/localization/ukr-ua.js rename to src/models/localizations/ukr-ua.js