mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 10:47:04 +02: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,
|
||||
SlashCommandSubcommandsOnlyBuilder,
|
||||
} from 'discord.js';
|
||||
import { translator } from '../core/localization/translator';
|
||||
import { COMMANDS_RAW, CommandRegistry } from '../events/interactionCreate';
|
||||
|
||||
const RAW_PATH = `https://raw.githubusercontent.com/tresabhi/blitzkrieg/main/docs/`;
|
||||
@@ -28,40 +29,46 @@ function addDocsSubcommands(option: SlashCommandSubcommandsOnlyBuilder) {
|
||||
return option;
|
||||
}
|
||||
|
||||
export const aboutCommand: CommandRegistry = {
|
||||
inProduction: true,
|
||||
inPublic: true,
|
||||
export const aboutCommand = new Promise<CommandRegistry>(async (resolve) => {
|
||||
resolve({
|
||||
inProduction: true,
|
||||
inPublic: true,
|
||||
|
||||
command: addDocsSubcommands(
|
||||
new SlashCommandBuilder()
|
||||
.setName('about')
|
||||
.setDescription('All the info you need about the bot')
|
||||
.addSubcommand((option) =>
|
||||
option
|
||||
.setName('commands')
|
||||
.setDescription('List of all commands and their descriptions'),
|
||||
),
|
||||
),
|
||||
command: addDocsSubcommands(
|
||||
new SlashCommandBuilder()
|
||||
.setName('about')
|
||||
.setNameLocalizations({
|
||||
// "es-ES":
|
||||
})
|
||||
.setDescription('All the info you need about the bot')
|
||||
.addSubcommand((option) =>
|
||||
option
|
||||
.setName('commands')
|
||||
.setDescription('List of all commands and their descriptions'),
|
||||
),
|
||||
),
|
||||
|
||||
async handler(interaction) {
|
||||
const subcommand = interaction.options.getSubcommand();
|
||||
async handler(interaction) {
|
||||
const subcommand = interaction.options.getSubcommand();
|
||||
const { t } = await translator(interaction.locale);
|
||||
|
||||
if (subcommand === 'commands') {
|
||||
return `# Commands\n\nBlitzkrieg offers the following commands:\n\n${(
|
||||
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}`,
|
||||
if (subcommand === 'commands') {
|
||||
return t`bot.commands.about.subcommands.commands.body${(
|
||||
await Promise.all(COMMANDS_RAW)
|
||||
)
|
||||
.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 content = fetch(url).then((response) => response.text());
|
||||
const url = `${RAW_PATH}${DOCS[subcommand as keyof typeof DOCS]}.md`;
|
||||
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 = {
|
||||
[key: string]: TranslationFragment;
|
||||
} & {
|
||||
@@ -8,10 +9,18 @@ export type TranslationFragmentTree = {
|
||||
};
|
||||
export type TranslationFragment = string | TranslationFragmentTree;
|
||||
|
||||
export const translations = LANGUAGES.reduce(
|
||||
(table, language) => ({
|
||||
...table,
|
||||
[language]: import(`../../lang/${language}.json`),
|
||||
}),
|
||||
{},
|
||||
) as Record<Language, Promise<TranslationFragment>>;
|
||||
export const translations = Promise.all(
|
||||
LOCALES.map(async (locale) => ({
|
||||
locale,
|
||||
translations: await import(`../../lang/${locale}.json`),
|
||||
})),
|
||||
).then(
|
||||
(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) {
|
||||
const strings = await translations[language];
|
||||
export async function translator(locale: Locale) {
|
||||
const strings = (await translations)[locale];
|
||||
|
||||
function translate(key: string) {
|
||||
const path = key.split('.');
|
||||
function translate(path: string) {
|
||||
const pathArray = path.split('.');
|
||||
let fragment = strings;
|
||||
|
||||
for (const pathItem in path) {
|
||||
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`,
|
||||
@@ -17,7 +18,7 @@ export async function translator(language: Language) {
|
||||
|
||||
if (typeof fragment === 'undefined') {
|
||||
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.$!;
|
||||
} else {
|
||||
throw new Error(
|
||||
`Unresolved tree ending for path "${path}" in language "${language}"`,
|
||||
`Unresolved tree ending for "${path}" in language "${locale}"`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function t(strings: TemplateStringsArray) {
|
||||
return translate(strings[0]);
|
||||
function t(paths: TemplateStringsArray, ...embeds: string[]) {
|
||||
return paths
|
||||
.map((path, index) =>
|
||||
path.length === 0
|
||||
? ''
|
||||
: `${translate(path)}${index === embeds.length ? '' : embeds[index]}`,
|
||||
)
|
||||
.join('');
|
||||
}
|
||||
|
||||
return { translate, t };
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@
|
||||
"commands": {
|
||||
"$": "commands",
|
||||
"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",
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@
|
||||
"commands": {
|
||||
"$": "comandos",
|
||||
"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",
|
||||
|
||||
@@ -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