UNPKG

nestjs-telegraf-i18n

Version:
56 lines (55 loc) 1.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TelegrafI18nContext = void 0; const telegraf_1 = require("telegraf"); const common_1 = require("@nestjs/common"); class TelegrafI18nContext extends telegraf_1.Context { constructor() { super(...arguments); this.logger = new common_1.Logger(this.constructor.name); } i18n() { return this._i18n; } setI18n(i18n) { this._i18n = i18n; } /** * Translates the given key using the current i18n context. * Falls back to returning the key as a string if i18n is not initialized. * * @param key - The translation key path. * @param options - Optional translation options. * @returns The translated value or the key itself. */ translate(key, options) { if (!this._i18n) { this.logger.warn(`i18n was not initialized for this Telegraf context. Cannot translate key='${key}'`); return key; // Fallback: Return the key itself } return this._i18n.translate(key, options); } /** * Shorthand for `translate()`. Translates the given key using the current i18n context. */ t(key, options) { return this.translate(key, options); } /** * Translates the given key and sends it as a reply message using `ctx.reply`. * * @param key - The translation key path. * @param options - Optional translation and reply options. */ async replyWithTranslation(key, options) { const message = this.t(key, options); await this.reply(String(message), options?.replyOptions); } /** * Shorthand for `replyWithTranslation()`. Translates the given key and sends it as a reply message using `ctx.reply`. */ async tReply(key, options) { return this.replyWithTranslation(key, options); } } exports.TelegrafI18nContext = TelegrafI18nContext;