@scaleway/use-i18n
Version:
A small hook to handle i18n
66 lines (65 loc) • 1.62 kB
JavaScript
import { memoize, strategies } from "@formatjs/fast-memoize";
import IntlTranslationFormat from "intl-messageformat";
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(
// @ts-expect-error we assume Intl.ListFormat exists in our current context
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
(...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
}
)
};
export {
formatters as default
};