Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Sep 16, 2024
2 parents 1bda10a + 09cd85e commit b8ce065
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 98 deletions.
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import languages from "./get.js";
import languages from "./index.js";

const language = "en_pr";

Expand Down
96 changes: 0 additions & 96 deletions get.js

This file was deleted.

98 changes: 97 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,97 @@
export { default as get } from "./get.js";
const validLanguages = [
"en_gb",
"en_pr",
"en_us",
"nl",
"pl",
"tr",
"vi",
"es_es",
];

const readObject = (obj, cursor = "") => {
const cursorPath = cursor.split(".");
for (let i = 0; i < cursorPath.length; i++) obj = obj[cursorPath[i]];
return obj;
};

const returnNextProperty = (
languagesStringsToUse,
fallbackLanguagesStrings,
cursor = ""
) => {
return new Proxy(
{},
{
get(target, prop1) {
if (prop1 === "then")
return returnNextProperty(
languagesStringsToUse,
fallbackLanguagesStrings
);
const currentCursor = cursor ? `${cursor}.${prop1}` : prop1;
const toReturn = readObject(languagesStringsToUse, currentCursor)
? readObject(languagesStringsToUse, currentCursor)
: readObject(fallbackLanguagesStrings.default, currentCursor);
switch (typeof toReturn) {
case "string":
return toReturn;
case "object":
return returnNextProperty(
languagesStringsToUse,
fallbackLanguagesStrings,
currentCursor
);
default:
return toReturn;
}
},
}
);
};

const languageTypeProxy = (language, type) => {
return new Proxy(
{},
{
async get(target, prop) {
const selectedLanguagesStrings = await import(
`./bot/${language}/${type}/${prop}.json`,
{ with: { type: "json" } }
).catch(() => null);
const fallbackLanguagesStrings = await import(
`./bot/en_us/${type}/${prop}.json`,
{ with: { type: "json" } }
);
const languagesStringsToUse = selectedLanguagesStrings?.default
? selectedLanguagesStrings.default
: fallbackLanguagesStrings.default;
return returnNextProperty(
languagesStringsToUse,
fallbackLanguagesStrings
);
},
}
);
};

const languageProxy = (language) => {
return new Proxy(
{},
{
get(target, prop) {
if (validLanguages.includes(language)) {
return languageTypeProxy(language, prop);
} else {
// default to en_us
return languageTypeProxy("en_us", prop);
}
},
}
);
};

export default function (lang) {
return languageProxy(lang);
}

0 comments on commit b8ce065

Please sign in to comment.