-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
169 lines (155 loc) · 4.95 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import type { LanguageStructure } from "./languages.d.ts";
export type DiscordLocaleKeys = keyof typeof locales;
export interface Locale {
code: string;
id: number;
name: string;
active: boolean;
}
export type Languages = {
[key: string]: Locale;
};
import _locales from "./bot/languages.json" with { type: "json" };
export const locales: Languages = _locales;
export const validLanguages: Array<string> = Object.values(locales)
.map((locale) => (locale.active === true ? locale.code : null))
.filter((locale) => locale !== null);
export const getDiscordLocaleCode = (language: string): DiscordLocaleKeys => {
if (typeof language === "string") {
for (const [key, value] of Object.entries(locales))
if (value.code === language) return key as DiscordLocaleKeys;
} else if (typeof language === "number") {
for (const [key, value] of Object.entries(locales))
if (value.id === language) return key as DiscordLocaleKeys;
}
throw new Error(`Language ${language} not found`);
};
export const getQuarkLocaleCode = (language: DiscordLocaleKeys): string => {
const toReturn = locales[language]?.code;
if (!toReturn) throw new Error(`Language ${language} not found`);
return toReturn;
};
export const getDatabaseLocaleCode = (language: string): number => {
const toReturn = locales[language]?.id;
if (typeof toReturn !== "number")
throw new Error(`Language ${language} not found`);
return toReturn;
};
export const getLocaleFromDatabaseCode = (databaseCode: number): string => {
for (const [key, value] of Object.entries(locales))
if (value.id === databaseCode) return key;
throw new Error(`Language ${databaseCode} not found`);
};
const readObject = (obj: { [key: string]: any }, 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])) {
if (typeof obj[cursorPath[i]] !== "object") return obj[cursorPath[i]];
else obj = (obj as { [key: string]: object })[cursorPath[i]];
}
return obj;
};
const returnNextProperty = (
languagesStringsToUse: object,
fallbackLanguagesStrings: object,
cursor = ""
) => {
return new Proxy(
{},
{
ownKeys(target) {
return [
...new Set([
...(readObject(languagesStringsToUse, cursor)
? Object.keys(readObject(languagesStringsToUse, cursor) || {})
: []),
...(readObject(fallbackLanguagesStrings, cursor)
? Object.keys(readObject(fallbackLanguagesStrings, cursor) || {})
: []),
]),
];
},
get(target, prop1) {
if (prop1 === "then")
return returnNextProperty(
languagesStringsToUse,
fallbackLanguagesStrings
);
const currentCursor = cursor
? `${cursor}.${String(prop1)}`
: String(prop1);
const toReturn = readObject(languagesStringsToUse, currentCursor)
? readObject(languagesStringsToUse, currentCursor)
: readObject(fallbackLanguagesStrings, currentCursor);
switch (typeof toReturn) {
case "string":
return toReturn;
case "object":
return returnNextProperty(
languagesStringsToUse,
fallbackLanguagesStrings,
currentCursor
);
default:
return toReturn;
}
},
getOwnPropertyDescriptor(target, prop) {
return {
enumerable: true,
configurable: true,
};
},
}
);
};
const languageTypeProxy = (
language: string,
type: string,
noFallback: boolean
) => {
return new Proxy(
{},
{
async get(target, prop) {
const selectedLanguagesStrings = await import(
`./bot/${language}/${type}/${String(prop)}.json`,
{ with: { type: "json" } }
).catch(() => null);
const fallbackLanguagesStrings = !noFallback
? await import(`./bot/en_us/${type}/${String(prop)}.json`, {
with: { type: "json" },
})
: null;
const languagesStringsToUse = selectedLanguagesStrings?.default
? selectedLanguagesStrings.default
: fallbackLanguagesStrings?.default;
return returnNextProperty(
languagesStringsToUse,
fallbackLanguagesStrings?.default
);
},
}
);
};
export default function languageProxy(
language: string,
noFallback: boolean = false
): LanguageStructure {
return new Proxy(
{},
{
get(target, prop) {
if (validLanguages.includes(language)) {
return languageTypeProxy(language, String(prop), noFallback);
} else if (!noFallback) {
// default to en_us
return languageTypeProxy("en_us", String(prop), noFallback);
} else {
throw new Error(`Language ${language} not found`);
}
},
}
) as LanguageStructure;
}