feat: some period translations

This commit is contained in:
TrèsAbhi committed 2024-03-06 15:34:10 -06:00
1 parent 267f23cad8
commit adad668157
24 files changed
+1309 -1216

No files matched your search

+59 -21
View File
@@ -1,14 +1,27 @@
import { Locale, SlashCommandBuilder } from 'discord.js';
import {
Locale,
SlashCommandBuilder,
SlashCommandSubcommandBuilder,
SlashCommandSubcommandGroupBuilder,
} from 'discord.js';
import { translator } from '../localization/translator';
import { localizationObject } from './localizationObject';
interface CreateLocalizedCommandSubcommand {
subcommand: string;
modify?: (option: SlashCommandSubcommandBuilder) => void;
}
interface CreateLocalizedCommandSubcommandGroup {
group: string;
modify?: (option: SlashCommandSubcommandGroupBuilder) => void;
}
export function createLocalizedCommand(
command: string,
subcommands?: CreateLocalizedCommandSubcommand[],
sub?:
| CreateLocalizedCommandSubcommand[]
| CreateLocalizedCommandSubcommandGroup[],
) {
const { translate } = translator(Locale.EnglishUS);
const slashCommand = new SlashCommandBuilder()
@@ -19,26 +32,51 @@ export function createLocalizedCommand(
localizationObject(`bot.commands.${command}.description`),
);
if (subcommands) {
subcommands.map(({ subcommand }) => {
const subcommandNameLocalizations = localizationObject(
`bot.commands.${command}.subcommands.${subcommand}`,
);
const subcommandDescriptionLocalizations = localizationObject(
`bot.commands.${command}.subcommands.${subcommand}.description`,
);
if (sub) {
sub.map((subItem) => {
if ('subcommand' in subItem) {
const subcommandNameLocalizations = localizationObject(
`bot.commands.${command}.subcommands.${subItem.subcommand}`,
);
const subcommandDescriptionLocalizations = localizationObject(
`bot.commands.${command}.subcommands.${subItem.subcommand}.description`,
);
slashCommand.addSubcommand((option) =>
option
.setName(subcommand)
.setNameLocalizations(subcommandNameLocalizations)
.setDescription(
translate(
`bot.commands.${command}.subcommands.${subcommand}.description`,
),
)
.setDescriptionLocalizations(subcommandDescriptionLocalizations),
);
slashCommand.addSubcommand((option) => {
if (subItem.modify) subItem.modify(option);
return option
.setName(subItem.subcommand)
.setNameLocalizations(subcommandNameLocalizations)
.setDescription(
translate(
`bot.commands.${command}.subcommands.${subItem.subcommand}.description`,
),
)
.setDescriptionLocalizations(subcommandDescriptionLocalizations);
});
} else {
const subcommandNameLocalizations = localizationObject(
`bot.commands.${command}.groups.${subItem.group}`,
);
const subcommandDescriptionLocalizations = localizationObject(
`bot.commands.${command}.groups.${subItem.group}.description`,
);
slashCommand.addSubcommandGroup((option) => {
if (subItem.modify) subItem.modify(option);
return option
.setName(subItem.group)
.setNameLocalizations(subcommandNameLocalizations)
.setDescription(
translate(
`bot.commands.${command}.groups.${subItem.group}.description`,
),
)
.setDescriptionLocalizations(subcommandDescriptionLocalizations);
});
}
});
}
+1 -1
View File
@@ -30,7 +30,7 @@ export default function resolvePeriodFromButton(
start = getTimeDaysAgo(region, endDaysAgoMax);
end = getTimeDaysAgo(region, startDaysAgoMin);
} else {
name = getPeriodOptionName(periodSubcommand);
name = getPeriodOptionName(periodSubcommand, interaction.locale);
start = getPeriodStart(region, periodSubcommand);
end = getPeriodNow();
}
+9 -6
View File
@@ -1,14 +1,17 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import { CacheType, ChatInputCommandInteraction, Locale } from 'discord.js';
import { Region } from '../../constants/regions';
import getPeriodNow from '../blitzkrieg/getPeriodNow';
import getPeriodStart from '../blitzkrieg/getPeriodStart';
import getTimeDaysAgo from '../blitzkrieg/getTimeDaysAgo';
import { translator } from '../localization/translator';
import { PeriodSize, PeriodType } from './addPeriodSubCommands';
export function getPeriodOptionName(period: PeriodSize) {
if (period === 'career') return 'Career';
if (period === 'today') return 'Today';
return `${period} days`;
export function getPeriodOptionName(period: PeriodSize, locale: Locale) {
const { translate } = translator(locale);
if (period === 'career') return translate('bot.common.periods.career');
if (period === 'today') return translate('bot.common.periods.today');
return translate('bot.common.periods.days', [period]);
}
export interface ResolvedPeriod {
@@ -39,7 +42,7 @@ export default function resolvePeriodFromCommand(
start = getTimeDaysAgo(region, endDaysAgoMax);
end = getTimeDaysAgo(region, startDaysAgoMin);
} else {
name = getPeriodOptionName(periodSubcommand);
name = getPeriodOptionName(periodSubcommand, interaction.locale);
start = getPeriodStart(region, periodSubcommand);
end = getPeriodNow();
}
+16 -3
View File
@@ -5,9 +5,10 @@ export function translator(localeRaw: Locale) {
const locale = locales.includes(localeRaw) ? localeRaw : Locale.EnglishUS;
const strings = translations[locale];
function translate(path: string): string {
function translate(path: string, literals?: string[]): string {
const pathArray = path.split('.');
let fragment = strings;
let resolvedString: string;
for (const pathItem of pathArray) {
if (typeof fragment === 'string') {
@@ -33,14 +34,26 @@ export function translator(localeRaw: Locale) {
}
if (typeof fragment === 'string') {
return fragment;
resolvedString = fragment;
} else if (fragment.$) {
return fragment.$!;
resolvedString = fragment.$!;
} else {
throw new Error(
`Unresolved tree ending for "${path}" in locale "${locale}"`,
);
}
if (literals) {
const chunks = resolvedString.split('%s');
let embeddedString = '';
chunks.forEach((chunk, index) => {
if (index !== 0) embeddedString += literals[index - 1];
embeddedString += chunk;
});
}
return resolvedString;
}
function t(paths: TemplateStringsArray, ...embeds: string[]) {