@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
68 lines (67 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TranslationService = exports.defaultMissingTranslationHandler = void 0;
const pMap_1 = require("../../promise/pMap");
const defaultMissingTranslationHandler = key => {
console.warn(`[tr] missing: ${key}`);
return `[${key}]`;
};
exports.defaultMissingTranslationHandler = defaultMissingTranslationHandler;
class TranslationService {
constructor(cfg, preloadedLocales = {}) {
this.cfg = {
...cfg,
missingTranslationHandler: exports.defaultMissingTranslationHandler,
};
this.locales = {
...preloadedLocales,
};
this.currentLocale = cfg.currentLocale || cfg.defaultLocale;
}
cfg;
/**
* Cache of loaded locales
*/
locales;
currentLocale;
/**
* Manually set locale data, bypassing the TranslationLoader.
*/
setLocale(localeName, locale) {
this.locales[localeName] = locale;
}
getLocale(locale) {
return this.locales[locale];
}
/**
* Loads locale(s) (if not already cached) via configured TranslationLoader.
* Resolves promise when done (ready to be used).
*/
async loadLocale(locale) {
const locales = Array.isArray(locale) ? locale : [locale];
await (0, pMap_1.pMap)(locales, async (locale) => {
if (this.locales[locale])
return; // already loaded
this.locales[locale] = await this.cfg.translationLoader.load(locale);
// console.log(`[tr] locale loaded: ${locale}`)
});
}
/**
* Will invoke `missingTranslationHandler` on missing tranlation.
*
* Does NOT do any locale loading. The locale needs to be loaded beforehand:
* either pre-loaded and passed to the constructor,
* or `await loadLocale(locale)`.
*/
translate(key, params) {
return this.translateIfExists(key, params) || this.cfg.missingTranslationHandler(key, params);
}
/**
* Does NOT invoke `missingTranslationHandler`, returns `undefined` instead.
*/
translateIfExists(key, _params) {
// todo: support params
return this.locales[this.currentLocale]?.[key] || this.locales[this.cfg.defaultLocale]?.[key];
}
}
exports.TranslationService = TranslationService;