From 060b366a068734632fa20ac82c1fa05f22239f5b Mon Sep 17 00:00:00 2001 From: Starman <30315137+Starman3787@users.noreply.github.com> Date: Fri, 18 Oct 2024 06:34:11 +0100 Subject: [PATCH] add database locale code --- bot/languages.json | 75 +++++++++++++++++++++++++++++++++++++++++----- index.js | 24 +++++++++++---- 2 files changed, 86 insertions(+), 13 deletions(-) diff --git a/bot/languages.json b/bot/languages.json index 69fbf55..ee88641 100644 --- a/bot/languages.json +++ b/bot/languages.json @@ -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 + } } diff --git a/index.js b/index.js index dedb35b..268f28d 100644 --- a/index.js +++ b/index.js @@ -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; }; @@ -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; };