feat: period subcommands

This commit is contained in:
TrèsAbhi committed 2024-03-05 10:51:14 -06:00
1 parent db7c865c76
commit 8a76558f01
10 files changed
+199 -100

No files matched your search

+11 -14
View File
@@ -9,18 +9,15 @@ export type TranslationFragmentTree = {
};
export type TranslationFragment = string | TranslationFragmentTree;
export const translations = Promise.all(
locales.map(async (locale) => ({
export const translations = locales
.map((locale) => ({
locale,
translations: await import(`../../lang/${locale}.json`),
})),
).then(
(translations) =>
translations.reduce(
(table, { locale, translations }) => ({
...table,
[locale]: translations,
}),
{},
) as Record<Locale, TranslationFragment>,
);
translations: require(`../../lang/${locale}.json`),
}))
.reduce(
(table, { locale, translations }) => ({
...table,
[locale]: translations,
}),
{},
) as Record<Locale, TranslationFragment>;
+15 -8
View File
@@ -1,26 +1,33 @@
import { Locale } from 'discord.js';
import { locales, translations } from './strings';
export async function translator(localeRaw: Locale) {
export function translator(localeRaw: Locale) {
const locale = locales.includes(localeRaw) ? localeRaw : Locale.EnglishUS;
const strings = (await translations)[locale];
const strings = translations[locale];
function translate(path: string) {
function translate(path: string): string {
const pathArray = path.split('.');
let fragment = strings;
for (const pathItem of pathArray) {
if (typeof fragment === 'string') {
throw new SyntaxError(
`Attempted to access sub-string ("${pathItem}" in "${path}") from a string instead of a tree`,
`Attempted to access string with key "${pathItem}" in "${path}" from within a string instead of an object in locale "${locale}"`,
);
} else {
fragment = fragment[pathItem];
if (typeof fragment === 'undefined') {
throw new Error(
`Undefined translation for key "${path}" (in "${path}") in language "${locale}"`,
);
if (locale === Locale.EnglishUS) {
throw new Error(
`Undefined translation at "${pathItem}" in "${path}" for locale "${locale}"`,
);
} else {
console.warn(
`Undefined translation at "${pathItem}" in "${path}" for locale "${locale}"; falling back to en-US`,
);
return translator(Locale.EnglishUS).translate(path);
}
}
}
}
@@ -31,7 +38,7 @@ export async function translator(localeRaw: Locale) {
return fragment.$!;
} else {
throw new Error(
`Unresolved tree ending for "${path}" in language "${locale}"`,
`Unresolved tree ending for "${path}" in locale "${locale}"`,
);
}
}