Skip to content

Commit

Permalink
feat: added new language meta
Browse files Browse the repository at this point in the history
  • Loading branch information
BeauBouchard committed Oct 31, 2022
1 parent 756f588 commit a76c107
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 5,829 deletions.
5,815 changes: 30 additions & 5,785 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"chai-http": "4.3.0",
"express": "4.18.2",
"helmet": "6.0.0",
"require-dir": "^1.2.0",
"sst": "git+https://github.com/wh-iterabb-it/sst.git"
},
"devDependencies": {
Expand Down
18 changes: 8 additions & 10 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
const facts = require("./models/facts");
const { convert } = require("sst");
const requireDir = require("require-dir");
const localization = require("./models/localization");

const localization = requireDir("./models/localization");
let fullCode = [];
let shortISO = [];

let ISO_LANG = [];
let SHORT_LANG = [];

// Dynamically fill the ISO_LANG and SHORT_LANG list with the provided config information in the language files
// Dynamically fill the fullCode and shortISO list with the provided config information in the language files
for (const language in localization) {
ISO_LANG.push(localization[language].ISO_LANG);
SHORT_LANG.push(localization[language].SHORT_LANG);
fullCode.push(localization[language].code);
shortISO.push(localization[language].langISO);
}

const VALID_LANGUAGES = SHORT_LANG.concat(ISO_LANG);
const VALID_LANGUAGES = shortISO.concat(fullCode);

/**
* Check if user entered valid count parameter
Expand Down Expand Up @@ -54,7 +52,7 @@ function invalidLanguageMiddleware(request, response, next) {
// language specified, so continue
response
.status(400)
.send(`Invalid language, valid languages are ${SHORT_LANG.join(", ")}`);
.send(`Invalid language, valid languages are ${shortISO.join(", ")}`);
return;
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/models/facts.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
const requireDir = require("require-dir");

const localization = requireDir("./localization");
const localization = require("./localization");

/**
*
* @param {String} langName - ISO 639-2 Code
* @param {String} requestedLang - ISO 639-2 Code
* @returns {String} fact localized to the language
*/
function getLanguageFacts(langName) {
function getLanguageFacts(requestedLang) {
for (const language in localization) {
if (
localization[language].ISO_LANG === langName ||
localization[language].SHORT_LANG === langName
localization[language].code === requestedLang ||
localization[language].langISO === requestedLang
) {
return localization[language].facts;
}
}

return localization["eng-US"].facts;
return localization["eng-us"].facts;
}

/**
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,17 @@ const facts = [
"Cats can drink sea water in order to survive.",
];

const langName = "english",
langISO = "eng",
langLocale = "us",
langLocaleName = "United States";

module.exports = {
languageName: "english",
ISO_LANG: "eng-us",
SHORT_LANG: "eng",
langName,
langISO,
langLocale,
langLocaleName,
code: `${langISO}-${langLocale}`,
codeName: `${langName} (${langLocaleName})`,
facts: facts,
};
10 changes: 0 additions & 10 deletions src/models/localization/esp-ES.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/models/localization/esp-es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const facts = [
"En 1987, los gatos superaron a los perros como la mascota número uno en Estados Unidos.",
];
const langName = "spanish",
langISO = "esp",
langLocale = "es",
langLocaleName = "spain";

module.exports = {
langName,
langISO,
langLocale,
langLocaleName,
code: `${langISO}-${langLocale}`,
codeName: `${langName} (${langLocaleName})`,
facts: facts,
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ const facts = [
"¡Tienes que ser gatito me! ¿Estás seguro de que quieres darte de baja? envía SÍ o NO",
];

const langName = "spanish",
langISO = "esp",
langLocale = "mx",
langLocaleName = "mexican";

module.exports = {
languageName: "mexican-spanish",
ISO_LANG: "esp-mx",
SHORT_LANG: "esp",
langName,
langISO,
langLocale,
langLocaleName,
code: `${langISO}-${langLocale}`,
codeName: `${langName} (${langLocaleName})`,
facts: facts,
};
22 changes: 22 additions & 0 deletions src/models/localization/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require("path");
const fs = require("fs");

const localizations = {};
const basename = path.basename(module.filename);

// Do a barrel export
// Barrelling all the localization files into a single object
// any file in this folder will get a key in the localizations object
// the key will be the filename without the extension, lowercase
// Example: "eng-US.js" will be called as localizations["eng-us"]
fs.readdirSync(__dirname)
.filter(
(file) =>
file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
)
.forEach((file) => {
localizations[file.substring(0, file.length - 3).toLowerCase()] =
require(path.join(__dirname, file)).default;
});

module.exports = localizations;
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,17 @@ const facts = [
"Кошки могут пить морскую воду, чтобы выжить.",
];

const langName = "russian",
langISO = "rus",
langLocale = "ru",
langLocaleName = "russia";

module.exports = {
languageName: "russian",
ISO_LANG: "rus-ru",
SHORT_LANG: "rus",
langName,
langISO,
langLocale,
langLocaleName,
code: `${langISO}-${langLocale}`,
codeName: `${langName} (${langLocaleName})`,
facts: facts,
};
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,17 @@ const facts = [
"Кішки можуть пити морську воду, щоб вижити.",
];

const langName = "ukrainian",
langISO = "ukr",
langLocale = "ua",
langLocaleName = "ukrainian";

module.exports = {
languageName: "ukrainian",
ISO_LANG: "ukr-ua",
SHORT_LANG: "ukr",
langName,
langISO,
langLocale,
langLocaleName,
code: `${langISO}-${langLocale}`,
codeName: `${langName} (${langLocaleName})`,
facts: facts,
};

0 comments on commit a76c107

Please sign in to comment.