UNPKG

@necord/localization

Version:

Localize your Discord bot with Necord!

24 lines (23 loc) 1.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultLocalizationAdapter = void 0; const base_localization_adapter_1 = require("./base-localization.adapter"); class DefaultLocalizationAdapter extends base_localization_adapter_1.BaseLocalizationAdapter { getTranslation(key, locale, placeholders) { var _a; const translations = this.getTranslations(locale); const translation = (_a = translations[key]) !== null && _a !== void 0 ? _a : this.getFallbackTranslation(key); return translation.replace(/{{\s*([^}\s]+)\s*}}/g, (_, placeholder) => placeholders[placeholder]); } getTranslations(locale) { var _a, _b, _c; return (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.locales) === null || _b === void 0 ? void 0 : _b[locale]) !== null && _c !== void 0 ? _c : {}; } getFallbackTranslation(key) { var _a, _b, _c; const fallbackLocale = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.fallbackLocale) !== null && _b !== void 0 ? _b : 'en-US'; const translations = this.getTranslations(fallbackLocale); return (_c = translations[key]) !== null && _c !== void 0 ? _c : key; } } exports.DefaultLocalizationAdapter = DefaultLocalizationAdapter;