UNPKG

@globalart/nestcord

Version:

A module for creating Discord bots using NestJS, based on Discord.js

42 lines (41 loc) 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NestedLocalizationAdapter = 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 NestedLocalizationAdapter extends base_localization_adapter_1.BaseLocalizationAdapter { getTranslation(key, locale, placeholders) { const translations = this.getTranslations(locale); const translation = this.findTranslation(translations, key) || this.getFallbackTranslation(key); return (0, text_utils_1.replacePlaceholdersInObjectWithFlatten)(translation, placeholders); } translate(key, placeholders) { const currentTranslationFn = interceptors_1.LocalizationInterceptor.getCurrentTranslationFn(); return currentTranslationFn ? currentTranslationFn(key, placeholders) : this.getTranslation(key, this.options.fallbackLocale, placeholders); } 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]) || {}; } findTranslation(translations, key) { const keys = key.split('.'); let currentTranslation = translations; for (const k of keys) { if (!(typeof currentTranslation === 'object' && k in currentTranslation)) { return undefined; } currentTranslation = currentTranslation[k]; } return typeof currentTranslation === 'string' ? currentTranslation : undefined; } getFallbackTranslation(key) { var _a; const fallbackLocale = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.fallbackLocale) || 'en-US'; const translations = this.getTranslations(fallbackLocale); return this.findTranslation(translations, key) || key; } } exports.NestedLocalizationAdapter = NestedLocalizationAdapter;