@globalart/nestcord
Version:
A module for creating Discord bots using NestJS, based on Discord.js
32 lines (31 loc) • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultLocalizationAdapter = void 0;
const text_utils_1 = require("@globalart/text-utils");
const base_localization_adapter_1 = require("./base-localization.adapter");
const interceptors_1 = require("../interceptors");
class DefaultLocalizationAdapter extends base_localization_adapter_1.BaseLocalizationAdapter {
getTranslation(key, locale, placeholders) {
const translation = this.getTranslations(locale)[key] || this.getFallbackTranslation(key);
return placeholders ? (0, text_utils_1.replacePlaceholdersInObjectWithFlatten)(translation, placeholders) : translation;
}
translate(key, placeholders) {
const currentTranslationFn = interceptors_1.LocalizationInterceptor.getCurrentTranslationFn();
return currentTranslationFn
? currentTranslationFn(key, placeholders)
: this.getTranslation(key, this.options.fallbackLocale, placeholders);
}
updateLocales(locales) {
this.options.locales = locales;
}
getTranslations(locale) {
var _a, _b;
return ((_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.locales) === null || _b === void 0 ? void 0 : _b[locale]) || {};
}
getFallbackTranslation(key) {
var _a;
const fallbackLocale = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.fallbackLocale) || 'en-US';
return this.getTranslations(fallbackLocale)[key] || key;
}
}
exports.DefaultLocalizationAdapter = DefaultLocalizationAdapter;