mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-22 11:17:05 +02:00
feat: partially translated /about
This commit is contained in:
1 parent
8e35e0d6d9
commit
db7c865c76
5 files changed
+104
-43
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import { Locale, SlashCommandBuilder } from 'discord.js';
|
||||
import { translator } from '../localization/translator';
|
||||
import { localizationObject } from './localizationObject';
|
||||
|
||||
interface CreateLocalizedCommandSubcommand {
|
||||
subcommand: string;
|
||||
}
|
||||
|
||||
export async function createLocalizedCommand(
|
||||
command: string,
|
||||
subcommands?: CreateLocalizedCommandSubcommand[],
|
||||
) {
|
||||
const { translate } = await translator(Locale.EnglishUS);
|
||||
const slashCommand = new SlashCommandBuilder()
|
||||
.setName(command)
|
||||
.setNameLocalizations(await localizationObject(`bot.commands.${command}`))
|
||||
.setDescription(translate(`bot.commands.${command}.description`))
|
||||
.setDescriptionLocalizations(
|
||||
await localizationObject(`bot.commands.${command}.description`),
|
||||
);
|
||||
|
||||
if (subcommands) {
|
||||
await Promise.all(
|
||||
subcommands.map(async ({ subcommand }) => {
|
||||
const subcommandNameLocalizations = await localizationObject(
|
||||
`bot.commands.${command}.subcommands.${subcommand}`,
|
||||
);
|
||||
const subcommandDescriptionLocalizations = await localizationObject(
|
||||
`bot.commands.${command}.subcommands.${subcommand}.description`,
|
||||
);
|
||||
|
||||
slashCommand.addSubcommand((option) =>
|
||||
option
|
||||
.setName(subcommand)
|
||||
.setNameLocalizations(subcommandNameLocalizations)
|
||||
.setDescription(
|
||||
translate(
|
||||
`bot.commands.${command}.subcommands.${subcommand}.description`,
|
||||
),
|
||||
)
|
||||
.setDescriptionLocalizations(subcommandDescriptionLocalizations),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return slashCommand;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Locale } from 'discord.js';
|
||||
import { locales } from '../localization/strings';
|
||||
import { translator } from '../localization/translator';
|
||||
|
||||
export async function localizationObject(path: string) {
|
||||
return (
|
||||
await Promise.all(locales.map(async (locale) => await translator(locale)))
|
||||
).reduce<Partial<Record<Locale, string>>>(
|
||||
(localizations, { locale, translate }) => {
|
||||
return {
|
||||
...localizations,
|
||||
[locale]: translate(path),
|
||||
};
|
||||
},
|
||||
{},
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Locale } from 'discord.js';
|
||||
|
||||
const LOCALES: Locale[number][] = ['en-US', 'es-ES'];
|
||||
export const locales: Locale[] = [Locale.EnglishUS, Locale.SpanishES];
|
||||
|
||||
export type TranslationFragmentTree = {
|
||||
[key: string]: TranslationFragment;
|
||||
@@ -10,7 +10,7 @@ export type TranslationFragmentTree = {
|
||||
export type TranslationFragment = string | TranslationFragmentTree;
|
||||
|
||||
export const translations = Promise.all(
|
||||
LOCALES.map(async (locale) => ({
|
||||
locales.map(async (locale) => ({
|
||||
locale,
|
||||
translations: await import(`../../lang/${locale}.json`),
|
||||
})),
|
||||
@@ -22,5 +22,5 @@ export const translations = Promise.all(
|
||||
[locale]: translations,
|
||||
}),
|
||||
{},
|
||||
) as Record<Locale[number], TranslationFragment>,
|
||||
) as Record<Locale, TranslationFragment>,
|
||||
);
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Locale } from 'discord.js';
|
||||
import { translations } from './strings';
|
||||
import { locales, translations } from './strings';
|
||||
|
||||
export async function translator(locale: Locale) {
|
||||
export async function translator(localeRaw: Locale) {
|
||||
const locale = locales.includes(localeRaw) ? localeRaw : Locale.EnglishUS;
|
||||
const strings = (await translations)[locale];
|
||||
|
||||
function translate(path: string) {
|
||||
@@ -45,5 +46,5 @@ export async function translator(locale: Locale) {
|
||||
.join('');
|
||||
}
|
||||
|
||||
return { translate, t };
|
||||
return { locale, translate, t };
|
||||
}
|
||||
Reference in new issue
Block a user