Skip to content

Commit

Permalink
add database locale code
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Oct 18, 2024
1 parent 214e879 commit 060b366
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 13 deletions.
75 changes: 67 additions & 8 deletions bot/languages.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
{
"en-US": "en_us",
"en-GB": "en_gb",
"tr": "tr",
"vi": "vi",
"en-PR": "en_pr",
"pl": "pl",
"nl": "nl",
"es-ES": "es_es"
"en-US": {
"code": "en_us",
"id": 0,
"name": "English, US",
"default": true,
"active": true
},
"en-GB": {
"code": "en_gb",
"id": 1,
"name": "English, UK",
"active": true
},
"tr": {
"code": "tr",
"id": 2,
"name": "Türkçe",
"active": true
},
"vi": {
"code": "vi",
"id": 3,
"name": "Tiếng Việt",
"active": true
},
"en-PR": {
"code": "en_pr",
"id": 4,
"name": "Pirate Speak, The Seven Seas",
"active": true
},
"pl": {
"code": "pl",
"id": 5,
"name": "Polski",
"active": true
},
"nl": {
"code": "nl",
"id": 6,
"name": "Nederlands",
"active": true
},
"es-ES": {
"code": "es_es",
"id": 7,
"name": "Español, España",
"active": true
},
"it": {
"code": "it",
"id": 8,
"name": "Italiano",
"active": false
},
"de": {
"code": "de",
"id": 9,
"name": "Deutsch",
"active": false
},
"fr": {
"code": "fr",
"id": 10,
"name": "Français",
"active": false
}
}
24 changes: 19 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@ export const locales = (
await import("./bot/languages.json", { with: { type: "json" } })
).default;

export const validLanguages = Object.values(locales).flat();
export const validLanguages = Object.values(locales)
.map((locale) => (locale.active === true ? locale.code : null))
.filter((locale) => locale !== null);

export const getDiscordLocaleCode = (language) => {
for (const [key, value] of Object.entries(locales))
if (value === language) return key;
if (typeof language === "string") {
for (const [key, value] of Object.entries(locales))
if (value.code === language) return key;
} else if (typeof language === "number") {
for (const [key, value] of Object.entries(locales))
if (value.id === language) return key;
}
throw new Error(`Language ${language} not found`);
};

export const getQuarkLocaleCode = (language) => {
const toReturn = locales[language];
const toReturn = locales[language]?.code;
if (!toReturn) throw new Error(`Language ${language} not found`);
return toReturn;
};

export const getDatabaseLocaleCode = (language) => {
const toReturn = locales[language]?.id;
if (!toReturn) throw new Error(`Language ${language} not found`);
return toReturn;
};
Expand All @@ -20,7 +33,8 @@ const readObject = (obj, cursor = "") => {
if (!obj) return undefined;
const cursorPath = cursor.split(".");
for (let i = 0; i < cursorPath.length; i++)
if (cursorPath[i] && Object.keys(obj).includes(cursorPath[i])) obj = obj[cursorPath[i]];
if (cursorPath[i] && Object.keys(obj).includes(cursorPath[i]))
obj = obj[cursorPath[i]];
return obj;
};

Expand Down

0 comments on commit 060b366

Please sign in to comment.