@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
52 lines (50 loc) • 2.14 kB
JavaScript
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
let __intlayer_types = require("@intlayer/types");
//#region src/utils/intl.ts
const cacheKey = (locales, options) => JSON.stringify([locales, options]);
const createCachedConstructor = (Ctor) => {
const cache = /* @__PURE__ */ new Map();
function Wrapped(locales, options) {
if (Ctor.name === "DisplayNames" && typeof Intl?.DisplayNames !== "function") {
if (process.env.NODE_ENV === "development") console.warn([
`// Intl.DisplayNames is not supported; falling back to raw locale (${locales}). `,
`// Consider adding a polyfill as https://formatjs.io/docs/polyfills/intl-displaynames/`,
``,
`import 'intl';`,
`import '@formatjs/intl-getcanonicallocales/polyfill';`,
`import '@formatjs/intl-locale/polyfill';`,
`import '@formatjs/intl-pluralrules/polyfill';`,
`import '@formatjs/intl-displaynames/polyfill';`,
`import '@formatjs/intl-listformat/polyfill';`,
`import '@formatjs/intl-numberformat/polyfill';`,
`import '@formatjs/intl-relativetimeformat/polyfill';`,
`import '@formatjs/intl-datetimeformat/polyfill';`,
``,
`// Optionally add locale data`,
`import '@formatjs/intl-pluralrules/locale-data/fr';`,
`import '@formatjs/intl-numberformat/locale-data/fr';`,
`import '@formatjs/intl-datetimeformat/locale-data/fr';`
].join("\n"));
return locales;
}
const key = cacheKey(locales ?? __intlayer_types.Locales.ENGLISH, options);
let instance = cache.get(key);
if (!instance) {
instance = new Ctor(locales, options);
cache.set(key, instance);
}
return instance;
}
Wrapped.prototype = Ctor.prototype;
return Wrapped;
};
const createCachedIntl = () => new Proxy(Intl, { get: (target, prop, receiver) => {
const value = Reflect.get(target, prop, receiver);
return typeof value === "function" && /^[A-Z]/.test(String(prop)) ? createCachedConstructor(value) : value;
} });
const CachedIntl = createCachedIntl();
//#endregion
exports.CachedIntl = CachedIntl;
exports.Intl = CachedIntl;
exports.createCachedIntl = createCachedIntl;
//# sourceMappingURL=intl.cjs.map