mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-25 03:29:51 +01:00
feat: started implementation of translations
This commit is contained in:
1 parent
2ba9f0f4c8
commit
ac60a436f1
6 files changed
+75
-118
No files matched your search
+38
-31
@@ -2,6 +2,7 @@ import {
|
|||||||
SlashCommandBuilder,
|
SlashCommandBuilder,
|
||||||
SlashCommandSubcommandsOnlyBuilder,
|
SlashCommandSubcommandsOnlyBuilder,
|
||||||
} from 'discord.js';
|
} from 'discord.js';
|
||||||
|
import { translator } from '../core/localization/translator';
|
||||||
import { COMMANDS_RAW, CommandRegistry } from '../events/interactionCreate';
|
import { COMMANDS_RAW, CommandRegistry } from '../events/interactionCreate';
|
||||||
|
|
||||||
const RAW_PATH = `https://raw.githubusercontent.com/tresabhi/blitzkrieg/main/docs/`;
|
const RAW_PATH = `https://raw.githubusercontent.com/tresabhi/blitzkrieg/main/docs/`;
|
||||||
@@ -28,40 +29,46 @@ function addDocsSubcommands(option: SlashCommandSubcommandsOnlyBuilder) {
|
|||||||
return option;
|
return option;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const aboutCommand: CommandRegistry = {
|
export const aboutCommand = new Promise<CommandRegistry>(async (resolve) => {
|
||||||
inProduction: true,
|
resolve({
|
||||||
inPublic: true,
|
inProduction: true,
|
||||||
|
inPublic: true,
|
||||||
|
|
||||||
command: addDocsSubcommands(
|
command: addDocsSubcommands(
|
||||||
new SlashCommandBuilder()
|
new SlashCommandBuilder()
|
||||||
.setName('about')
|
.setName('about')
|
||||||
.setDescription('All the info you need about the bot')
|
.setNameLocalizations({
|
||||||
.addSubcommand((option) =>
|
// "es-ES":
|
||||||
option
|
})
|
||||||
.setName('commands')
|
.setDescription('All the info you need about the bot')
|
||||||
.setDescription('List of all commands and their descriptions'),
|
.addSubcommand((option) =>
|
||||||
),
|
option
|
||||||
),
|
.setName('commands')
|
||||||
|
.setDescription('List of all commands and their descriptions'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
async handler(interaction) {
|
async handler(interaction) {
|
||||||
const subcommand = interaction.options.getSubcommand();
|
const subcommand = interaction.options.getSubcommand();
|
||||||
|
const { t } = await translator(interaction.locale);
|
||||||
|
|
||||||
if (subcommand === 'commands') {
|
if (subcommand === 'commands') {
|
||||||
return `# Commands\n\nBlitzkrieg offers the following commands:\n\n${(
|
return t`bot.commands.about.subcommands.commands.body${(
|
||||||
await Promise.all(COMMANDS_RAW)
|
await Promise.all(COMMANDS_RAW)
|
||||||
)
|
|
||||||
.filter((registry) => registry.inPublic && registry.inProduction)
|
|
||||||
.sort((a, b) => (a.command.name < b.command.name ? -1 : 1))
|
|
||||||
.map(
|
|
||||||
(registry) =>
|
|
||||||
`- \`/${registry.command.name}\`: ${registry.command.description}`,
|
|
||||||
)
|
)
|
||||||
.join('\n')}`;
|
.filter((registry) => registry.inPublic && registry.inProduction)
|
||||||
}
|
.sort((a, b) => (a.command.name < b.command.name ? -1 : 1))
|
||||||
|
.map(
|
||||||
|
(registry) =>
|
||||||
|
`- \`/${registry.command.name}\`: ${registry.command.description}`,
|
||||||
|
)
|
||||||
|
.join('\n')}`;
|
||||||
|
}
|
||||||
|
|
||||||
const url = `${RAW_PATH}${DOCS[subcommand as keyof typeof DOCS]}.md`;
|
const url = `${RAW_PATH}${DOCS[subcommand as keyof typeof DOCS]}.md`;
|
||||||
const content = fetch(url).then((response) => response.text());
|
const content = fetch(url).then((response) => response.text());
|
||||||
|
|
||||||
return content;
|
return content;
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
});
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
const LANGUAGES = ['en-US', 'es-ES', 'ja-JP'] as const;
|
import { Locale } from 'discord.js';
|
||||||
|
|
||||||
|
const LOCALES: Locale[number][] = ['en-US', 'es-ES'];
|
||||||
|
|
||||||
export type Language = (typeof LANGUAGES)[number];
|
|
||||||
export type TranslationFragmentTree = {
|
export type TranslationFragmentTree = {
|
||||||
[key: string]: TranslationFragment;
|
[key: string]: TranslationFragment;
|
||||||
} & {
|
} & {
|
||||||
@@ -8,10 +9,18 @@ export type TranslationFragmentTree = {
|
|||||||
};
|
};
|
||||||
export type TranslationFragment = string | TranslationFragmentTree;
|
export type TranslationFragment = string | TranslationFragmentTree;
|
||||||
|
|
||||||
export const translations = LANGUAGES.reduce(
|
export const translations = Promise.all(
|
||||||
(table, language) => ({
|
LOCALES.map(async (locale) => ({
|
||||||
...table,
|
locale,
|
||||||
[language]: import(`../../lang/${language}.json`),
|
translations: await import(`../../lang/${locale}.json`),
|
||||||
}),
|
})),
|
||||||
{},
|
).then(
|
||||||
) as Record<Language, Promise<TranslationFragment>>;
|
(translations) =>
|
||||||
|
translations.reduce(
|
||||||
|
(table, { locale, translations }) => ({
|
||||||
|
...table,
|
||||||
|
[locale]: translations,
|
||||||
|
}),
|
||||||
|
{},
|
||||||
|
) as Record<Locale[number], TranslationFragment>,
|
||||||
|
);
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
import { Language, translations } from './strings';
|
import { Locale } from 'discord.js';
|
||||||
|
import { translations } from './strings';
|
||||||
|
|
||||||
export async function translator(language: Language) {
|
export async function translator(locale: Locale) {
|
||||||
const strings = await translations[language];
|
const strings = (await translations)[locale];
|
||||||
|
|
||||||
function translate(key: string) {
|
function translate(path: string) {
|
||||||
const path = key.split('.');
|
const pathArray = path.split('.');
|
||||||
let fragment = strings;
|
let fragment = strings;
|
||||||
|
|
||||||
for (const pathItem in path) {
|
for (const pathItem of pathArray) {
|
||||||
if (typeof fragment === 'string') {
|
if (typeof fragment === 'string') {
|
||||||
throw new SyntaxError(
|
throw new SyntaxError(
|
||||||
`Attempted to access sub-string ("${pathItem}" in "${path}") from a string instead of a tree`,
|
`Attempted to access sub-string ("${pathItem}" in "${path}") from a string instead of a tree`,
|
||||||
@@ -17,7 +18,7 @@ export async function translator(language: Language) {
|
|||||||
|
|
||||||
if (typeof fragment === 'undefined') {
|
if (typeof fragment === 'undefined') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Undefined translation for key "${key}" (in "${path}") in language "${language}"`,
|
`Undefined translation for key "${path}" (in "${path}") in language "${locale}"`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,13 +30,19 @@ export async function translator(language: Language) {
|
|||||||
return fragment.$!;
|
return fragment.$!;
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Unresolved tree ending for path "${path}" in language "${language}"`,
|
`Unresolved tree ending for "${path}" in language "${locale}"`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function t(strings: TemplateStringsArray) {
|
function t(paths: TemplateStringsArray, ...embeds: string[]) {
|
||||||
return translate(strings[0]);
|
return paths
|
||||||
|
.map((path, index) =>
|
||||||
|
path.length === 0
|
||||||
|
? ''
|
||||||
|
: `${translate(path)}${index === embeds.length ? '' : embeds[index]}`,
|
||||||
|
)
|
||||||
|
.join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
return { translate, t };
|
return { translate, t };
|
||||||
|
|||||||
+1
-1
@@ -24,7 +24,7 @@
|
|||||||
"commands": {
|
"commands": {
|
||||||
"$": "commands",
|
"$": "commands",
|
||||||
"description": "List of all commands and their descriptions",
|
"description": "List of all commands and their descriptions",
|
||||||
"body": "# Commands\n\nBlitzkrieg offers the following commands:"
|
"body": "# Commands\n\nBlitzkrieg offers the following commands:\n\n"
|
||||||
},
|
},
|
||||||
"embeds": {
|
"embeds": {
|
||||||
"$": "embeds",
|
"$": "embeds",
|
||||||
|
|||||||
+1
-1
@@ -23,7 +23,7 @@
|
|||||||
"commands": {
|
"commands": {
|
||||||
"$": "comandos",
|
"$": "comandos",
|
||||||
"description": "Lista de todos los comandos y sus descripciones",
|
"description": "Lista de todos los comandos y sus descripciones",
|
||||||
"body": "# Comandos\n\nBlitzkrieg ofrece los siguientes comandos:"
|
"body": "# Comandos\n\nBlitzkrieg ofrece los siguientes comandos:\n\n"
|
||||||
},
|
},
|
||||||
"embeds": {
|
"embeds": {
|
||||||
"$": "embeds",
|
"$": "embeds",
|
||||||
|
|||||||
@@ -1,66 +0,0 @@
|
|||||||
{
|
|
||||||
"bot": {
|
|
||||||
"common": {
|
|
||||||
"wn8": {
|
|
||||||
"$": "WN8",
|
|
||||||
"very_bad": "Very bad",
|
|
||||||
"bad": "Bad",
|
|
||||||
"below_average": "Below average",
|
|
||||||
"average": "Average",
|
|
||||||
"above_average": "Above average",
|
|
||||||
"good": "Good",
|
|
||||||
"very_good": "Very good",
|
|
||||||
"great": "Great",
|
|
||||||
"unicum": "Unicum",
|
|
||||||
"super_unicum": "Super unicum"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"commands": {
|
|
||||||
"about": {
|
|
||||||
"$": "about",
|
|
||||||
"description": "All the info you need about the bot",
|
|
||||||
"subcommands": {
|
|
||||||
"commands": {
|
|
||||||
"$": "commands",
|
|
||||||
"description": "List of all commands and their descriptions",
|
|
||||||
"body": "# Commands\n\nBlitzkrieg offers the following commands:"
|
|
||||||
},
|
|
||||||
"embeds": {
|
|
||||||
"$": "embeds",
|
|
||||||
"description": "How embeds work"
|
|
||||||
},
|
|
||||||
"introduction": {
|
|
||||||
"$": "introduction",
|
|
||||||
"description": "About the bot"
|
|
||||||
},
|
|
||||||
"invite": {
|
|
||||||
"$": "invite",
|
|
||||||
"description": "How to invite the bot"
|
|
||||||
},
|
|
||||||
"timezones": {
|
|
||||||
"$": "timezones",
|
|
||||||
"description": "How Blitzkrieg infers and uses your timezone"
|
|
||||||
},
|
|
||||||
"creators": {
|
|
||||||
"$": "creators",
|
|
||||||
"description": "People who have worked on the bot"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"breakdown": {
|
|
||||||
"$": "breakdown",
|
|
||||||
"description": "A period's breakdown by tanks played",
|
|
||||||
"body": {
|
|
||||||
"total": "Total",
|
|
||||||
"battles": "Battles",
|
|
||||||
"winrate": "Winrate",
|
|
||||||
"damage": "Damage"
|
|
||||||
},
|
|
||||||
"buttons": {
|
|
||||||
"refresh": "Refresh"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"website": {}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user