@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
107 lines (105 loc) • 4.09 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
let _intlayer_config_built = require("@intlayer/config/built");
//#region src/utils/intl.ts
/**
* Cached Intl helper – drop‑in replacement for the global `Intl` object.
* ‑‑‑
* • Uses a `Proxy` to lazily wrap every *constructor* hanging off `Intl` (NumberFormat, DateTimeFormat, …).
* • Each wrapped constructor keeps an in‑memory cache keyed by `[locales, options]` so that identical requests
* reuse the same heavy instance instead of reparsing CLDR data every time.
* • A polyfill warning for `Intl.DisplayNames` is emitted only once and only in dev.
* • The public API is fully type‑safe and mirrors the native `Intl` surface exactly –
* you can treat `CachedIntl` just like the built‑in `Intl`.
*
* Usage @example:
* ---------------
* ```ts
* import { CachedIntl } from "./cached-intl";
*
* const nf = CachedIntl.NumberFormat("en-US", { style: "currency", currency: "USD" });
* console.log(nf.format(1234));
*
* const dn = CachedIntl.DisplayNames(["fr"], { type: "language" });
* console.log(dn.of("en")); * → "anglais"
*
* You can also spin up an isolated instance with its own caches (handy in test suites):
* const TestIntl = createCachedIntl();
* ```
*/
const MAX_CACHE_SIZE = 50;
const cache = /* @__PURE__ */ new Map();
/**
* Generic caching instantiator for Intl constructors.
*/
const getCachedIntl = (Ctor, locale, options) => {
const resLoc = locale ?? _intlayer_config_built.internationalization?.defaultLocale;
const key = `${resLoc}|${options ? JSON.stringify(options) : ""}`;
let ctorCache = cache.get(Ctor);
if (!ctorCache) {
ctorCache = /* @__PURE__ */ new Map();
cache.set(Ctor, ctorCache);
}
let instance = ctorCache.get(key);
if (!instance) {
if (ctorCache.size > MAX_CACHE_SIZE) ctorCache.clear();
instance = new Ctor(resLoc, options);
ctorCache.set(key, instance);
}
return instance;
};
/**
* Optional: Keep bindIntl if your library exports it publicly.
* It now uses the much smaller getCachedIntl under the hood.
*/
const bindIntl = (boundLocale) => {
const bindWrap = (Ctor) => function intlConstructor(locales, options) {
const isOptsFirst = locales !== null && typeof locales === "object" && !Array.isArray(locales);
const resOpts = isOptsFirst ? locales : options;
return getCachedIntl(Ctor, isOptsFirst ? resOpts.locale || boundLocale : locales || boundLocale, resOpts);
};
return {
...Intl,
Collator: bindWrap(Intl.Collator),
DateTimeFormat: bindWrap(Intl.DateTimeFormat),
DisplayNames: bindWrap(Intl.DisplayNames),
ListFormat: bindWrap(Intl.ListFormat),
NumberFormat: bindWrap(Intl.NumberFormat),
PluralRules: bindWrap(Intl.PluralRules),
RelativeTimeFormat: bindWrap(Intl.RelativeTimeFormat),
Locale: bindWrap(Intl.Locale),
Segmenter: bindWrap(Intl.Segmenter)
};
};
const CachedIntl = {
Collator: function Collator(locales, options) {
return getCachedIntl(Intl.Collator, locales, options);
},
DateTimeFormat: function DateTimeFormat(locales, options) {
return getCachedIntl(Intl.DateTimeFormat, locales, options);
},
DisplayNames: function DisplayNames(locales, options) {
return getCachedIntl(Intl.DisplayNames, locales, options);
},
ListFormat: function ListFormat(locales, options) {
return getCachedIntl(Intl.ListFormat, locales, options);
},
NumberFormat: function NumberFormat(locales, options) {
return getCachedIntl(Intl.NumberFormat, locales, options);
},
PluralRules: function PluralRules(locales, options) {
return getCachedIntl(Intl.PluralRules, locales, options);
},
RelativeTimeFormat: function RelativeTimeFormat(locales, options) {
return getCachedIntl(Intl.RelativeTimeFormat, locales, options);
},
Segmenter: function Segmenter(locales, options) {
return getCachedIntl(Intl.Segmenter, locales, options);
}
};
//#endregion
exports.CachedIntl = CachedIntl;
exports.Intl = CachedIntl;
exports.bindIntl = bindIntl;
exports.getCachedIntl = getCachedIntl;
//# sourceMappingURL=intl.cjs.map