Skip to content

Commit

Permalink
Fix default multi language locales #220 (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
furkanmutlu authored Jan 10, 2023
1 parent c42f01d commit d32394d
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
16 changes: 10 additions & 6 deletions src/models/facts.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
const localization = require("./localization");
const { localizations, defaultLanguages } = require("./localizations");

/**
*
* @param {String} requestedLang - ISO 639-2 Code
* @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;
}

/**
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions src/models/localizations/defaultLanguages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const espDefault = require("./esp-es");

const defaultLanguages = {
esp: espDefault,
};

module.exports = defaultLanguages;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,12 +15,15 @@ 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[
file.substring(0, file.length - 3).toLowerCase()
] = require(path.join(__dirname, file));
});

module.exports = localizations;
module.exports = { localizations, defaultLanguages };
File renamed without changes.
File renamed without changes.

0 comments on commit d32394d

Please sign in to comment.