Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/languages (#206) #208

Merged
merged 5 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 31 additions & 57 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"eslint-plugin-import": "2.26.0",
"eslint-plugin-prettier": "4.2.1",
"mocha": "10.1.0",
"prettier": "2.7.1",
"nyc": "15.1.0"
"nyc": "15.1.0",
"prettier": "2.7.1"
}
}
19 changes: 13 additions & 6 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
const facts = require("./models/facts");
const { convert } = require("sst");
const localization = require("./models/localization");
console.log(localization);

const ISO_LANG = ["eng-us", "ukr-ua", "rus-ru", "esp-mx", "esp-es", "de-de"];
const SHORT_LANG = ["eng", "ukr", "rus", "esp", "ger"];
const VALID_LANGUAGES = SHORT_LANG.concat(ISO_LANG);
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);
}

const VALID_LANGUAGES = shortISO.concat(fullCode);

/**
* Check if user entered valid count parameter
Expand Down Expand Up @@ -44,9 +53,7 @@ function invalidLanguageMiddleware(request, response, next) {
// language specified, so continue
response
.status(400)
.send(
`Invalid language, valid languages are "eng", "ukr", "rus", "ger"`
);
.send(`Invalid language, valid languages are ${shortISO.join(", ")}`);
return;
}
}
Expand Down
43 changes: 13 additions & 30 deletions src/models/facts.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
const english = require("./localization/eng-US");
const russian = require("./localization/rus-RU");
const ukraine = require("./localization/ukr-UA");
const german = require("./localization/de-DE");
const spanishMexico = require("./localization/esp-MX");
const spanishSpain = require("./localization/esp-ES");
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) {
switch (langName) {
case "eng-us": // english
case "eng": // default english
return english.facts;
case "rus-ru": // russian
case "rus": // default russian
return russian.facts;
case "ukr-ua": // ukrainian
case "ukr": // default ukrainian
return ukraine.facts;
case "esp-es": // spanish Spain
return spanishSpain.facts;
case "esp-mx": // spanish Mexico
case "esp": // default spanish
return spanishMexico.facts;
case "de-de": // german Germany
case "ger": // default german
return german.facts;
case undefined: // no language specified
default:
return english.facts;
function getLanguageFacts(requestedLang) {
for (const language in localization) {
if (
localization[language].code === requestedLang ||
localization[language].langISO === requestedLang
) {
return localization[language].facts;
}
}

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

/**
Expand All @@ -42,7 +25,7 @@ function getLanguageFacts(langName) {
* @param {String} lang
* @returns {String} - a single fact about cats
**/
function getSingle(ID = null, lang) {
function getSingle(ID = null, lang = "eng-us") {
const facts = getLanguageFacts(lang);
if (ID) {
const id = ID - 1;
Expand Down

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

Loading