@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
37 lines (35 loc) • 1.2 kB
JavaScript
import { getDictionary } from "./getDictionary.mjs";
import { getAppLogger } from "@intlayer/config/client";
import configuration from "@intlayer/config/built";
import { getDictionaries } from "@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(() => path, {
get: (_target, prop) => {
if (prop === "toJSON" || prop === Symbol.toPrimitive || prop === "toString") return () => path;
if (prop === "then") return;
return createSafeFallback(path ? `${path}.${String(prop)}` : String(prop));
},
apply: () => {
return path;
}
});
};
const getIntlayer = (key, locale, plugins) => {
const dictionary = getDictionaries()[key];
if (!dictionary) {
getAppLogger(configuration)(`Dictionary "${key}" was not found. Using fallback proxy.`, {
level: "warn",
isVerbose: true
});
return createSafeFallback(key);
}
return getDictionary(dictionary, locale, plugins);
};
//#endregion
export { getIntlayer };
//# sourceMappingURL=getIntlayer.mjs.map