UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

49 lines (47 loc) 1.95 kB
import { Locales } from "@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") { 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 ?? 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 export { CachedIntl, CachedIntl as Intl, createCachedIntl }; //# sourceMappingURL=intl.mjs.map