forked from AverageHelper/Gamgee
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.ts
More file actions
307 lines (253 loc) · 9.2 KB
/
i18n.ts
File metadata and controls
307 lines (253 loc) · 9.2 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import de from "./locales/de.json";
import enGB from "./locales/en-GB.json";
import enUS from "./locales/en-US.json";
import esES from "./locales/es-ES.json";
import fr from "./locales/fr.json";
import hu from "./locales/hu.json";
import ptBR from "./locales/pt-BR.json";
// ** Install language files here **
const vocabulary = {
de,
"en-GB": enGB,
"en-US": enUS,
"es-ES": esES,
fr,
hu,
"pt-BR": ptBR, // TODO: Get these keys from the JSON (?)
} as const;
export const DEFAULT_LOCALE = "en-US";
export type SupportedLocale = keyof typeof vocabulary;
export interface LocaleMetadata {
code: string;
name: string;
nickname: string;
}
export const locales = Object.keys(vocabulary) as ReadonlyArray<SupportedLocale>;
/** Returns `true` if the given string matches a supported locale code. */
export function isSupportedLocale(tbd: unknown): tbd is SupportedLocale {
return locales.includes(tbd as SupportedLocale);
}
/** Returns the given locale if we support that locale. Returns `null` otherwise. */
export function localeIfSupported(tbd: unknown): SupportedLocale | null {
if (!isSupportedLocale(tbd)) return null;
return tbd;
}
export function randomSupportedLocale(): SupportedLocale {
return randomElementOfArray(locales.slice()) ?? DEFAULT_LOCALE;
}
export function metadataForLocale(locale: SupportedLocale): LocaleMetadata {
return vocabulary[locale].meta;
}
import type { Guild } from "discord.js";
/** Returns the unit's preferred locale, if supported, or the default locale if not. */
export function preferredLocale(guild: Pick<Guild, "preferredLocale">): SupportedLocale {
return localeIfSupported(guild.preferredLocale) ?? DEFAULT_LOCALE;
}
// TypeScript ensures here that DEFAULT_LOCALE is a valid locale:
type MessageSchema = (typeof vocabulary)[typeof DEFAULT_LOCALE];
// ** I18N Utilities **
interface RecursiveStringRecord extends Record<string, string | RecursiveStringRecord> {}
// for testing i18n over generic vocabularies
type Vocabulary = {
[DEFAULT_LOCALE]: RecursiveStringRecord;
} & {
[P in Exclude<SupportedLocale, typeof DEFAULT_LOCALE>]?: RecursiveStringRecord;
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _: Vocabulary = vocabulary; // ensures our language types are formatted right
import type { Get } from "./helpers/get.js";
import { get } from "./helpers/get.js";
import { isString } from "./helpers/guards.js";
import { split } from "./helpers/split.js";
const DOT = ".";
/**
* Returns the given `locale`'s translation for the given `keypath`.
*
* If the given `locale` does not contain a matching translation,
* but the default locale does, then that translation is returned.
*
* Returns `keypath` if no matching translation exists.
*/
export function t<K extends string>(
keypath: K,
locale: SupportedLocale,
): Get<MessageSchema, K> extends string ? Get<MessageSchema, K> : undefined;
/**
* Returns the given `locale`'s translation for the given `keypath`.
*
* If the given `locale` does not contain a matching translation,
* but the default locale does, then that translation is returned.
*
* Returns `undefined` if no matching translation exists.
*/
export function t<K extends string, V extends Vocabulary>(
keypath: K,
locale: SupportedLocale,
messages: V,
): Get<V[typeof DEFAULT_LOCALE], K> extends string ? Get<V[typeof DEFAULT_LOCALE], K> : undefined;
// FIXME: We shouldn't need to overload this
export function t<K extends string>(
keypath: K,
locale: SupportedLocale,
data = vocabulary,
): string | undefined {
if (keypath === "") return undefined;
const result = get(data[locale], split(keypath, DOT));
if (isString(result) && result) return result; // found a result in the given locale!
if (locale !== DEFAULT_LOCALE) {
// recurse, try the default locale:
return t(keypath, DEFAULT_LOCALE, data);
}
return undefined; // we're stumped, return nothing
// Most implementations might default to returning the keypath,
// but I think we should let TypeScript yell at us if we
// missed translating a key.
}
import { composed, createPartialString, push } from "./helpers/composeStrings.js";
import { randomElementOfArray } from "./helpers/randomElementOfArray.js";
/**
* Returns the given `locale`'s translation for the given `keypath`,
* while interpolating the given `values` into their respective placeholders.
*
* If the given `locale` does not contain a matching translation,
* but the default locale does, then that translation is returned.
*
* Returns `undefined` if no matching translation exists.
*/
export function ti<K extends string>(
keypath: K,
values: Readonly<Record<string, string>>,
locale: SupportedLocale,
): Get<MessageSchema, K> extends string ? string : undefined;
// FIXME: We shouldn't need to overload this
export function ti<K extends string>(
keypath: K,
values: Readonly<Record<string, string>>,
locale: SupportedLocale,
): string | undefined {
const rawText: string | undefined = t(keypath, locale);
if (rawText === undefined || rawText === "") return rawText;
// Parse out text and variable names
interface SlotItem {
/** `true` if the item should render some slotted data. */
isVar: true;
/** The name of the variable. */
name: string;
}
interface TextItem {
/** `true` if the item should render some slotted data. */
isVar: false;
/** Text to render. */
text: string;
}
type Item = SlotItem | TextItem;
const items: Array<Item> = [];
let mode: "discovery" | "text" | "slot" = "discovery";
let text = "";
for (const char of rawText) {
if (char === "{" && mode !== "slot") {
if (mode === "text") {
// Finish text node
items.push({ isVar: false, text });
}
// Start variable name
text = "";
mode = "slot";
} else if (char === "}" && mode === "slot") {
// We've hit the end of a variable name
if (text === "") {
// but the brackets were empty. Treat that as a text node
items.push({ isVar: false, text: "{}" });
} else {
items.push({ isVar: true, name: text });
}
text = "";
mode = "discovery";
} else if (mode === "slot") {
// Continue variable name
text += char;
} else {
// Continue text
text += char;
mode = "text";
}
}
if (text !== "") {
if (mode === "text") {
// Finished, but there's some string left
items.push({ isVar: false, text });
} else if (mode === "slot") {
// Finished, but we ended with an incomplete variable. Push it as text
text = `{${text}`; // make sure to include the variable starter
items.push({ isVar: false, text });
}
}
// Combine items, mixing given values for variables if given
const partial = createPartialString();
for (const item of items) {
let str: string;
if (item.isVar) {
str = values[item.name] ?? `{${item.name}}`; // default to the raw variable name
} else {
str = item.text;
}
push(str, partial);
}
return composed(partial);
}
export { ti as translateInterpolating };
/**
* Returns an object with every translation we have for the given `keypath`,
* or `undefined` if no translations exist.
*/
export function localizations<K extends string>(
keypath: K,
): Get<MessageSchema, K> extends string ? Partial<Record<SupportedLocale, string>> : undefined;
// FIXME: We shouldn't need to overload this
export function localizations<K extends string>(
keypath: K,
): Partial<Record<SupportedLocale, string>> | undefined {
// Get all localizations for the given keypath
const result: Partial<Record<SupportedLocale, string>> = {};
for (const locale of locales) {
const translation = t(keypath, locale);
if (translation !== undefined) {
// only add the translation if there is one to add
result[locale] = translation;
}
}
// Return undefined if there were no results
if (Object.keys(result).length === 0) return undefined;
return result;
}
// For science:
/* eslint-disable @typescript-eslint/no-unused-vars */
// const enUS = vocabulary["en-US"];
/*
import type { Join } from "type-fest";
// We start with this at dev time, typing into a function:
const given = "commands.sr.name";
// Goal: Confirm that a given string makes up a valid key-path in a given object.
// Split given by DOT:
const p: ["commands", "sr", "name"] = split(given, DOT);
// const testBad: string = get(enUS, "commands.sr."); // Error
const testGood: string = get(enUS, "commands.sr.name");
const testAlsoBad: string = get(enUS, ["commands", "sr"]);
const testAlsoGood: string = get(enUS, p);
// If we can split `given` by a DOT "." we get these values:
const p0: "commands" = p[0];
const p1: "sr" = p[1];
const p2: "name" = p[2];
// We can recombine segments with this:
type Path<L extends string, R extends string, Sep extends string = typeof DOT> = Join<[L, R], Sep>;
const partial: Path<typeof p0, typeof p1> = `${p0}.${p1}`;
// these values are equivalent to `given`:
const path: Path<Path<typeof p0, typeof p1>, typeof p2> = given;
const alsoPath: Join<typeof p, typeof DOT> = given;
const commands: MessageSchema[typeof p0] = vocabulary["en-US"][p0];
const sr: MessageSchema[typeof p0][typeof p1] = vocabulary["en-US"][p0][p1];
const name: MessageSchema[typeof p0][typeof p1][typeof p2] = t(given, "en-US");
type IsKeyPath<O, K extends string> = Get<O, K> extends string ? true : false;
const good: IsKeyPath<MessageSchema, typeof given> = true;
const bad: IsKeyPath<MessageSchema, "typeof given"> = false;
*/