mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-04 14:38:49 +00:00
Use existing i18n instance
This commit is contained in:
2 files changed
+2
-28
No files matched your search
@@ -11,7 +11,7 @@ import i18n from 'i18next';
|
|||||||
import i18nextFsBackend, { FsBackendOptions } from 'i18next-fs-backend';
|
import i18nextFsBackend, { FsBackendOptions } from 'i18next-fs-backend';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
|
|
||||||
const translatorInstance = i18n.createInstance();
|
export const translatorInstance = i18n.createInstance();
|
||||||
const localeDir = join(__dirname, '..', '..', 'locales');
|
const localeDir = join(__dirname, '..', '..', 'locales');
|
||||||
translatorInstance.use(i18nextFsBackend).init<FsBackendOptions>({
|
translatorInstance.use(i18nextFsBackend).init<FsBackendOptions>({
|
||||||
initImmediate: false,
|
initImmediate: false,
|
||||||
|
|||||||
@@ -14,12 +14,8 @@ import {
|
|||||||
} from 'discord.js';
|
} from 'discord.js';
|
||||||
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
||||||
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
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 { 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 {
|
class HelpCommand extends BaseSlashCommandInteraction {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -104,30 +100,8 @@ class HelpCommand extends BaseSlashCommandInteraction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private getCommandString(command: BaseSlashCommandInteraction, interaction: ChatInputCommandInteraction): string {
|
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 commandName = command.data.name;
|
||||||
const metadataKey = `commands.${commandName}.metadata`;
|
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';
|
const locale = interaction.guildLocale ?? 'en';
|
||||||
let translatedData: CommandMetadata | undefined = undefined;
|
let translatedData: CommandMetadata | undefined = undefined;
|
||||||
translatedData = translatorInstance.getResource(locale, 'bot', metadataKey) as CommandMetadata | undefined;
|
translatedData = translatorInstance.getResource(locale, 'bot', metadataKey) as CommandMetadata | undefined;
|
||||||
|
|||||||
Reference in new issue
Block a user