From 13aea262a67986ad4563e3430890e38c71721187 Mon Sep 17 00:00:00 2001 From: tacheometry <39647014+tacheometry@users.noreply.github.com> Date: Mon, 18 Dec 2023 02:06:36 +0200 Subject: [PATCH] Use existing i18n instance --- src/common/localeUtil.ts | 2 +- src/interactions/commands/info/help.ts | 28 +------------------------- 2 files changed, 2 insertions(+), 28 deletions(-) diff --git a/src/common/localeUtil.ts b/src/common/localeUtil.ts index 9717aac..fe055a8 100644 --- a/src/common/localeUtil.ts +++ b/src/common/localeUtil.ts @@ -11,7 +11,7 @@ import i18n from 'i18next'; import i18nextFsBackend, { FsBackendOptions } from 'i18next-fs-backend'; import { join } from 'path'; -const translatorInstance = i18n.createInstance(); +export const translatorInstance = i18n.createInstance(); const localeDir = join(__dirname, '..', '..', 'locales'); translatorInstance.use(i18nextFsBackend).init({ initImmediate: false, diff --git a/src/interactions/commands/info/help.ts b/src/interactions/commands/info/help.ts index 96cf21c..989a92b 100644 --- a/src/interactions/commands/info/help.ts +++ b/src/interactions/commands/info/help.ts @@ -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({ - 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;