@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
43 lines (41 loc) • 2.16 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
const require_interpreter_getDictionary = require('./getDictionary.cjs');
let _intlayer_config_built = require("@intlayer/config/built");
let _intlayer_config_logger = require("@intlayer/config/logger");
let _intlayer_dictionaries_entry = require("@intlayer/dictionaries-entry");
//#region src/interpreter/getIntlayer.ts
/**
* Creates a Recursive Proxy that returns the path of the accessed key
* stringified. This prevents the app from crashing on undefined access.
*/
const createSafeFallback = (path = "") => {
return new Proxy({}, { get: (_target, prop) => {
if (prop === "toJSON" || prop === Symbol.toPrimitive || prop === "toString" || prop === "valueOf") return () => path;
if (prop === "then") return;
if (prop === Symbol.iterator) return function* () {
yield path;
};
return createSafeFallback(path ? `${path}.${String(prop)}` : String(prop));
} });
};
const dictionaryCache = /* @__PURE__ */ new Map();
const warnedMissingDictionaries = /* @__PURE__ */ new Set();
const getIntlayer = (key, locale, plugins) => {
const dictionary = (0, _intlayer_dictionaries_entry.getDictionaries)()[key];
if (!dictionary && process.env.NODE_ENV === "development") {
if (!warnedMissingDictionaries.has(key)) {
(0, _intlayer_config_logger.getAppLogger)({ log: _intlayer_config_built.log })(typeof window === "undefined" ? `Dictionary ${(0, _intlayer_config_logger.colorizeKey)(key)} was not found. Using fallback proxy.` : `Dictionary ${key} was not found. Using fallback proxy.`, { level: "warn" });
warnedMissingDictionaries.add(key);
}
return createSafeFallback(key);
}
const cacheKey = `${key}_${locale ?? "default"}_${plugins ? "custom_plugins" : "default_plugins"}`;
if (dictionaryCache.has(cacheKey)) return dictionaryCache.get(cacheKey);
const result = require_interpreter_getDictionary.getDictionary(dictionary, locale, plugins);
dictionaryCache.set(cacheKey, result);
return result;
};
//#endregion
exports.getIntlayer = getIntlayer;
//# sourceMappingURL=getIntlayer.cjs.map