Use existing i18n instance

This commit is contained in:
tacheometry committed 2023-12-18 02:06:36 +02:00
1 parent 9778020f2b
commit 13aea262a6
2 files changed
+2 -28

No files matched your search

+1 -27
View File
@@ -14,12 +14,8 @@ import {
} from 'discord.js';
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { CommandMetadata, localizeCommand, useServerTranslator } from '../../../common/localeUtil';
import { CommandMetadata, localizeCommand, translatorInstance, useServerTranslator } from '../../../common/localeUtil';
import { ExtendedClient } from '../../../types/clientTypes';
import i18n from 'i18next';
import i18nextFsBackend, { FsBackendOptions } from 'i18next-fs-backend';
import { join } from 'path';
import { lstatSync, readdirSync } from 'fs';
class HelpCommand extends BaseSlashCommandInteraction {
constructor() {
@@ -104,30 +100,8 @@ class HelpCommand extends BaseSlashCommandInteraction {
}
private getCommandString(command: BaseSlashCommandInteraction, interaction: ChatInputCommandInteraction): string {
// TODO: Clean up this mess and create a common factory for translator instances.
const commandName = command.data.name;
const metadataKey = `commands.${commandName}.metadata`;
const translatorInstance = i18n.createInstance();
const localeDir = join(__dirname, '..', '..', '..', '..', 'locales');
translatorInstance.use(i18nextFsBackend).init<FsBackendOptions>({
initImmediate: false,
fallbackLng: 'en',
preload: readdirSync(localeDir).filter((fileName) => {
const joinedPath = join(localeDir, fileName);
const isDirectory = lstatSync(joinedPath).isDirectory();
return isDirectory;
}),
ns: 'bot',
defaultNS: 'bot',
backend: {
loadPath: join(localeDir, '{{lng}}', '{{ns}}.json'),
addPath: join(localeDir, '{{lng}}', '{{ns}}.missing.json')
},
interpolation: {
escapeValue: false
}
});
const locale = interaction.guildLocale ?? 'en';
let translatedData: CommandMetadata | undefined = undefined;
translatedData = translatorInstance.getResource(locale, 'bot', metadataKey) as CommandMetadata | undefined;