From 09cd85ec810f60f0dcb4d857739e5a2412485a32 Mon Sep 17 00:00:00 2001 From: Starman <30315137+Starman3787@users.noreply.github.com> Date: Mon, 16 Sep 2024 15:07:58 +0100 Subject: [PATCH] change export --- example.js | 2 +- get.js | 96 ---------------------------------------------------- index.js | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 98 insertions(+), 98 deletions(-) delete mode 100644 get.js diff --git a/example.js b/example.js index 1739e17..d47db52 100644 --- a/example.js +++ b/example.js @@ -1,4 +1,4 @@ -import languages from "./get.js"; +import languages from "./index.js"; const language = "en_pr"; diff --git a/get.js b/get.js deleted file mode 100644 index 12cca85..0000000 --- a/get.js +++ /dev/null @@ -1,96 +0,0 @@ -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); -} diff --git a/index.js b/index.js index 0126505..fb1471d 100644 --- a/index.js +++ b/index.js @@ -1 +1,97 @@ -export { default as get } from "./get.js"; \ No newline at end of file +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); + } + \ No newline at end of file