feat: moved translate command to i18n service

This commit is contained in:
Slincnik 2025-03-04 18:02:39 +03:00
parent f399e5b38d
commit 26d94bed0c
No known key found for this signature in database
2 changed files with 6 additions and 17 deletions

View file

@ -26,7 +26,7 @@ export default class InternationalizationService {
this.client = client;
this.options = {
localesPath: resolve(this.client.configService.get("paths.locales")),
defaultLanguage: options.defaultLanguage || "en-US",
defaultLanguage: options.defaultLanguage || this.client.configService.get("defaultLang"),
};
this.init();
}
@ -54,6 +54,11 @@ export default class InternationalizationService {
return { namespaces: [...new Set(namespaces)], languages };
}
public translate(key: string, options: TOptionsBase = {}) {
const lng = options.lng || this.options.defaultLanguage;
return i18next.t(key, { lng, ...options });
}
private async init() {
const { namespaces, languages } = await this.walkDirectory(this.options.localesPath);
@ -71,12 +76,6 @@ export default class InternationalizationService {
initImmediate: false,
});
// Типизированная функция translate
this.client.translate = (key: string, options: TOptionsBase = {}): string => {
const lng = options.lng || this.options.defaultLanguage;
return i18next.t(key, { lng, ...options });
};
logger.log("Internationalization initialized");
return i18nInstance;

View file

@ -1,6 +1,5 @@
import { Client, ClientOptions } from "discord.js";
import { GiveawaysManager } from "discord-giveaways";
import { TOptionsBase } from "i18next";
import { Handlers } from "@/handlers/index.js";
import MongooseAdapter from "@/adapters/database/MongooseAdapter.js";
import logger from "@/helpers/logger.js";
@ -16,15 +15,6 @@ export class ExtendedClient extends Client<true> {
cacheReminds = new Map<string, cacheRemindsData>();
i18n = new InternationalizationService(this);
translate!: (
_key: string,
_options?:
| TOptionsBase
| {
[key: string]: string;
},
) => string;
// @ts-ignore - because ExtendedClient != Client<boolean> from discord.js
giveaways = new GiveawaysManager(this, {
storage: "./giveaways.json",