UNPKG

@scaleway/use-i18n

Version:
47 lines (46 loc) 1.33 kB
import { memoize, strategies } from "@formatjs/fast-memoize"; import IntlTranslationFormat from "intl-messageformat"; //#region src/formatters.ts function createFastMemoizeCache() { const store = /* @__PURE__ */ new Map(); return { create() { return { get(key) { return store.get(key); }, set(key, value) { return store.set(key, value); } }; } }; } const baseFormatters = { getDateTimeFormat: memoize((...args) => new Intl.DateTimeFormat(...args), { cache: createFastMemoizeCache(), strategy: strategies.variadic }), getListFormat: memoize((...args) => new Intl.ListFormat(...args), { cache: createFastMemoizeCache(), strategy: strategies.variadic }), getNumberFormat: memoize((...args) => new Intl.NumberFormat(...args), { cache: createFastMemoizeCache(), strategy: strategies.variadic }), getPluralRules: memoize((...args) => new Intl.PluralRules(...args), { cache: createFastMemoizeCache(), strategy: strategies.variadic }) }; const formatters = { ...baseFormatters, getTranslationFormat: memoize((message, locales, overrideFormats, opts) => new IntlTranslationFormat(message, locales, overrideFormats, { formatters: baseFormatters, ...opts }), { cache: createFastMemoizeCache(), strategy: strategies.variadic }) }; //#endregion export { formatters as default };