mirror of
https://github.com/JonnyBro/JaBa.git
synced 2025-04-01 03:32:00 +05:00
feat: moved translate command to i18n service
This commit is contained in:
parent
f399e5b38d
commit
26d94bed0c
2 changed files with 6 additions and 17 deletions
|
@ -26,7 +26,7 @@ export default class InternationalizationService {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.options = {
|
this.options = {
|
||||||
localesPath: resolve(this.client.configService.get("paths.locales")),
|
localesPath: resolve(this.client.configService.get("paths.locales")),
|
||||||
defaultLanguage: options.defaultLanguage || "en-US",
|
defaultLanguage: options.defaultLanguage || this.client.configService.get("defaultLang"),
|
||||||
};
|
};
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
@ -54,6 +54,11 @@ export default class InternationalizationService {
|
||||||
return { namespaces: [...new Set(namespaces)], languages };
|
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() {
|
private async init() {
|
||||||
const { namespaces, languages } = await this.walkDirectory(this.options.localesPath);
|
const { namespaces, languages } = await this.walkDirectory(this.options.localesPath);
|
||||||
|
|
||||||
|
@ -71,12 +76,6 @@ export default class InternationalizationService {
|
||||||
initImmediate: false,
|
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");
|
logger.log("Internationalization initialized");
|
||||||
|
|
||||||
return i18nInstance;
|
return i18nInstance;
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { Client, ClientOptions } from "discord.js";
|
import { Client, ClientOptions } from "discord.js";
|
||||||
import { GiveawaysManager } from "discord-giveaways";
|
import { GiveawaysManager } from "discord-giveaways";
|
||||||
import { TOptionsBase } from "i18next";
|
|
||||||
import { Handlers } from "@/handlers/index.js";
|
import { Handlers } from "@/handlers/index.js";
|
||||||
import MongooseAdapter from "@/adapters/database/MongooseAdapter.js";
|
import MongooseAdapter from "@/adapters/database/MongooseAdapter.js";
|
||||||
import logger from "@/helpers/logger.js";
|
import logger from "@/helpers/logger.js";
|
||||||
|
@ -16,15 +15,6 @@ export class ExtendedClient extends Client<true> {
|
||||||
cacheReminds = new Map<string, cacheRemindsData>();
|
cacheReminds = new Map<string, cacheRemindsData>();
|
||||||
i18n = new InternationalizationService(this);
|
i18n = new InternationalizationService(this);
|
||||||
|
|
||||||
translate!: (
|
|
||||||
_key: string,
|
|
||||||
_options?:
|
|
||||||
| TOptionsBase
|
|
||||||
| {
|
|
||||||
[key: string]: string;
|
|
||||||
},
|
|
||||||
) => string;
|
|
||||||
|
|
||||||
// @ts-ignore - because ExtendedClient != Client<boolean> from discord.js
|
// @ts-ignore - because ExtendedClient != Client<boolean> from discord.js
|
||||||
giveaways = new GiveawaysManager(this, {
|
giveaways = new GiveawaysManager(this, {
|
||||||
storage: "./giveaways.json",
|
storage: "./giveaways.json",
|
||||||
|
|
Loading…
Add table
Reference in a new issue