-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|