@intlayer/core 8.5.1 → 8.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/formatters/compact.cjs +1 -1
- package/dist/cjs/formatters/compact.cjs.map +1 -1
- package/dist/cjs/formatters/currency.cjs +1 -1
- package/dist/cjs/formatters/currency.cjs.map +1 -1
- package/dist/cjs/formatters/date.cjs +1 -1
- package/dist/cjs/formatters/date.cjs.map +1 -1
- package/dist/cjs/formatters/index.cjs +1 -1
- package/dist/cjs/formatters/list.cjs +1 -1
- package/dist/cjs/formatters/list.cjs.map +1 -1
- package/dist/cjs/formatters/number.cjs +1 -1
- package/dist/cjs/formatters/number.cjs.map +1 -1
- package/dist/cjs/formatters/percentage.cjs +2 -2
- package/dist/cjs/formatters/percentage.cjs.map +1 -1
- package/dist/cjs/formatters/relativeTime.cjs +1 -1
- package/dist/cjs/formatters/relativeTime.cjs.map +1 -1
- package/dist/cjs/formatters/units.cjs +1 -1
- package/dist/cjs/formatters/units.cjs.map +1 -1
- package/dist/cjs/index.cjs +16 -9
- package/dist/cjs/interpreter/getContent/getContent.cjs +8 -8
- package/dist/cjs/interpreter/getContent/getContent.cjs.map +1 -1
- package/dist/cjs/interpreter/splitAndJoinInsertion.cjs +10 -35
- package/dist/cjs/interpreter/splitAndJoinInsertion.cjs.map +1 -1
- package/dist/cjs/localization/generateSitemap.cjs +111 -0
- package/dist/cjs/localization/generateSitemap.cjs.map +1 -0
- package/dist/cjs/localization/getBrowserLocale.cjs +1 -1
- package/dist/cjs/localization/getBrowserLocale.cjs.map +1 -1
- package/dist/cjs/localization/getLocale.cjs +2 -2
- package/dist/cjs/localization/getLocale.cjs.map +1 -1
- package/dist/cjs/localization/getLocalizedUrl.cjs +1 -1
- package/dist/cjs/localization/getPrefix.cjs +1 -1
- package/dist/cjs/localization/index.cjs +8 -7
- package/dist/cjs/localization/localeMapper.cjs +1 -1
- package/dist/cjs/transpiler/html/getHTMLCustomComponents.cjs +2 -3
- package/dist/cjs/transpiler/html/getHTMLCustomComponents.cjs.map +1 -1
- package/dist/cjs/utils/index.cjs +9 -3
- package/dist/cjs/utils/intl.cjs +88 -72
- package/dist/cjs/utils/intl.cjs.map +1 -1
- package/dist/cjs/utils/localeStorage.cjs +191 -64
- package/dist/cjs/utils/localeStorage.cjs.map +1 -1
- package/dist/esm/formatters/compact.mjs +2 -2
- package/dist/esm/formatters/compact.mjs.map +1 -1
- package/dist/esm/formatters/currency.mjs +2 -2
- package/dist/esm/formatters/currency.mjs.map +1 -1
- package/dist/esm/formatters/date.mjs +2 -2
- package/dist/esm/formatters/date.mjs.map +1 -1
- package/dist/esm/formatters/index.mjs +2 -2
- package/dist/esm/formatters/list.mjs +2 -2
- package/dist/esm/formatters/list.mjs.map +1 -1
- package/dist/esm/formatters/number.mjs +2 -2
- package/dist/esm/formatters/number.mjs.map +1 -1
- package/dist/esm/formatters/percentage.mjs +3 -3
- package/dist/esm/formatters/percentage.mjs.map +1 -1
- package/dist/esm/formatters/relativeTime.mjs +2 -2
- package/dist/esm/formatters/relativeTime.mjs.map +1 -1
- package/dist/esm/formatters/units.mjs +2 -2
- package/dist/esm/formatters/units.mjs.map +1 -1
- package/dist/esm/index.mjs +10 -10
- package/dist/esm/interpreter/getContent/getContent.mjs +8 -8
- package/dist/esm/interpreter/getContent/getContent.mjs.map +1 -1
- package/dist/esm/interpreter/splitAndJoinInsertion.mjs +10 -35
- package/dist/esm/interpreter/splitAndJoinInsertion.mjs.map +1 -1
- package/dist/esm/localization/generateSitemap.mjs +109 -0
- package/dist/esm/localization/generateSitemap.mjs.map +1 -0
- package/dist/esm/localization/getBrowserLocale.mjs +2 -2
- package/dist/esm/localization/getBrowserLocale.mjs.map +1 -1
- package/dist/esm/localization/getLocale.mjs +3 -3
- package/dist/esm/localization/getLocale.mjs.map +1 -1
- package/dist/esm/localization/getLocalizedUrl.mjs +1 -1
- package/dist/esm/localization/getPrefix.mjs +1 -1
- package/dist/esm/localization/index.mjs +7 -7
- package/dist/esm/localization/localeMapper.mjs +1 -1
- package/dist/esm/transpiler/html/getHTMLCustomComponents.mjs +2 -4
- package/dist/esm/transpiler/html/getHTMLCustomComponents.mjs.map +1 -1
- package/dist/esm/utils/index.mjs +4 -4
- package/dist/esm/utils/intl.mjs +87 -72
- package/dist/esm/utils/intl.mjs.map +1 -1
- package/dist/esm/utils/localeStorage.mjs +186 -65
- package/dist/esm/utils/localeStorage.mjs.map +1 -1
- package/dist/types/deepTransformPlugins/getFilterMissingTranslationsContent.d.ts +10 -10
- package/dist/types/deepTransformPlugins/getFilterMissingTranslationsContent.d.ts.map +1 -1
- package/dist/types/deepTransformPlugins/getFilterTranslationsOnlyContent.d.ts +10 -10
- package/dist/types/deepTransformPlugins/getFilterTranslationsOnlyContent.d.ts.map +1 -1
- package/dist/types/deepTransformPlugins/getFilteredLocalesContent.d.ts +10 -10
- package/dist/types/deepTransformPlugins/getFilteredLocalesContent.d.ts.map +1 -1
- package/dist/types/dictionaryManipulator/orderDictionaries.d.ts +2 -2
- package/dist/types/dictionaryManipulator/orderDictionaries.d.ts.map +1 -1
- package/dist/types/formatters/index.d.ts +2 -2
- package/dist/types/formatters/number.d.ts +4 -1
- package/dist/types/formatters/number.d.ts.map +1 -1
- package/dist/types/formatters/percentage.d.ts +4 -1
- package/dist/types/formatters/percentage.d.ts.map +1 -1
- package/dist/types/index.d.ts +4 -4
- package/dist/types/interpreter/getContent/getContent.d.ts.map +1 -1
- package/dist/types/interpreter/splitAndJoinInsertion.d.ts +0 -17
- package/dist/types/interpreter/splitAndJoinInsertion.d.ts.map +1 -1
- package/dist/types/localization/generateSitemap.d.ts +93 -0
- package/dist/types/localization/generateSitemap.d.ts.map +1 -0
- package/dist/types/localization/getBrowserLocale.d.ts +2 -2
- package/dist/types/localization/getBrowserLocale.d.ts.map +1 -1
- package/dist/types/localization/index.d.ts +2 -2
- package/dist/types/transpiler/html/getHTMLCustomComponents.d.ts.map +1 -1
- package/dist/types/utils/index.d.ts +3 -3
- package/dist/types/utils/intl.d.ts +15 -11
- package/dist/types/utils/intl.d.ts.map +1 -1
- package/dist/types/utils/localeStorage.d.ts +85 -68
- package/dist/types/utils/localeStorage.d.ts.map +1 -1
- package/package.json +8 -8
- package/dist/cjs/getStorageAttributes.cjs +0 -135
- package/dist/cjs/getStorageAttributes.cjs.map +0 -1
- package/dist/esm/getStorageAttributes.mjs +0 -133
- package/dist/esm/getStorageAttributes.mjs.map +0 -1
- package/dist/types/getStorageAttributes.d.ts +0 -29
- package/dist/types/getStorageAttributes.d.ts.map +0 -1
|
@@ -3,15 +3,15 @@ const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
|
3
3
|
const require_utils_localeStorage = require('../utils/localeStorage.cjs');
|
|
4
4
|
const require_localization_localeResolver = require('./localeResolver.cjs');
|
|
5
5
|
const require_localization_localeDetector = require('./localeDetector.cjs');
|
|
6
|
-
let _intlayer_config_defaultValues = require("@intlayer/config/defaultValues");
|
|
7
6
|
let _intlayer_config_built = require("@intlayer/config/built");
|
|
8
7
|
_intlayer_config_built = require_runtime.__toESM(_intlayer_config_built);
|
|
8
|
+
let _intlayer_config_defaultValues = require("@intlayer/config/defaultValues");
|
|
9
9
|
|
|
10
10
|
//#region src/localization/getLocale.ts
|
|
11
11
|
const getLocale = async (ctx = {}) => {
|
|
12
12
|
const defaultLocale = _intlayer_config_built.default?.internationalization?.defaultLocale ?? _intlayer_config_defaultValues.DEFAULT_LOCALE;
|
|
13
13
|
const availableLocales = _intlayer_config_built.default?.internationalization?.locales ?? _intlayer_config_defaultValues.LOCALES;
|
|
14
|
-
const storedLocale = require_utils_localeStorage.
|
|
14
|
+
const storedLocale = require_utils_localeStorage.getLocaleFromStorageServer({
|
|
15
15
|
getCookie: ctx.getCookie,
|
|
16
16
|
getHeader: ctx.getHeader
|
|
17
17
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getLocale.cjs","names":["configuration","DEFAULT_LOCALE","LOCALES","
|
|
1
|
+
{"version":3,"file":"getLocale.cjs","names":["configuration","DEFAULT_LOCALE","LOCALES","getLocaleFromStorageServer","localeResolver","getPreferredLanguages"],"sources":["../../../src/localization/getLocale.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { DEFAULT_LOCALE, LOCALES } from '@intlayer/config/defaultValues';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport { getLocaleFromStorageServer } from '../utils/localeStorage';\nimport { getPreferredLanguages } from './localeDetector';\nimport { localeResolver } from './localeResolver';\n\nexport type RequestContext = {\n getHeader?: (name: string) => string | null | undefined;\n getCookie?: (name: string) => string | null | undefined;\n};\n\nexport const getLocale = async (ctx: RequestContext = {}): Promise<Locale> => {\n const defaultLocale =\n configuration?.internationalization?.defaultLocale ?? DEFAULT_LOCALE;\n const availableLocales =\n configuration?.internationalization?.locales ?? LOCALES;\n\n // Try locale from storage (cookie or header)\n const storedLocale = getLocaleFromStorageServer({\n getCookie: ctx.getCookie,\n getHeader: ctx.getHeader,\n });\n\n if (storedLocale) {\n return storedLocale;\n }\n\n // Fallback to Accept-Language negotiation\n const acceptLanguageHeader = ctx.getHeader?.('accept-language');\n\n if (!acceptLanguageHeader) {\n return defaultLocale;\n }\n\n const preferredLocaleStrings = getPreferredLanguages(\n acceptLanguageHeader,\n availableLocales\n );\n\n const userFallbackLocale = localeResolver(\n preferredLocaleStrings,\n availableLocales,\n defaultLocale\n );\n\n if (userFallbackLocale) {\n return userFallbackLocale;\n }\n\n // Default locale\n return defaultLocale;\n};\n"],"mappings":";;;;;;;;;;AAYA,MAAa,YAAY,OAAO,MAAsB,EAAE,KAAsB;CAC5E,MAAM,gBACJA,gCAAe,sBAAsB,iBAAiBC;CACxD,MAAM,mBACJD,gCAAe,sBAAsB,WAAWE;CAGlD,MAAM,eAAeC,uDAA2B;EAC9C,WAAW,IAAI;EACf,WAAW,IAAI;EAChB,CAAC;AAEF,KAAI,aACF,QAAO;CAIT,MAAM,uBAAuB,IAAI,YAAY,kBAAkB;AAE/D,KAAI,CAAC,qBACH,QAAO;CAQT,MAAM,qBAAqBC,mDALIC,0DAC7B,sBACA,iBACD,EAIC,kBACA,cACD;AAED,KAAI,mBACF,QAAO;AAIT,QAAO"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
3
3
|
const require_utils_checkIsURLAbsolute = require('../utils/checkIsURLAbsolute.cjs');
|
|
4
|
-
const require_localization_getPrefix = require('./getPrefix.cjs');
|
|
5
4
|
const require_localization_getPathWithoutLocale = require('./getPathWithoutLocale.cjs');
|
|
5
|
+
const require_localization_getPrefix = require('./getPrefix.cjs');
|
|
6
6
|
const require_localization_rewriteUtils = require('./rewriteUtils.cjs');
|
|
7
7
|
let _intlayer_config_built = require("@intlayer/config/built");
|
|
8
8
|
_intlayer_config_built = require_runtime.__toESM(_intlayer_config_built);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
3
|
-
let _intlayer_config_defaultValues = require("@intlayer/config/defaultValues");
|
|
4
3
|
let _intlayer_config_built = require("@intlayer/config/built");
|
|
5
4
|
_intlayer_config_built = require_runtime.__toESM(_intlayer_config_built);
|
|
5
|
+
let _intlayer_config_defaultValues = require("@intlayer/config/defaultValues");
|
|
6
6
|
|
|
7
7
|
//#region src/localization/getPrefix.ts
|
|
8
8
|
/**
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const
|
|
2
|
+
const require_localization_getPathWithoutLocale = require('./getPathWithoutLocale.cjs');
|
|
3
|
+
const require_localization_getPrefix = require('./getPrefix.cjs');
|
|
4
|
+
const require_localization_rewriteUtils = require('./rewriteUtils.cjs');
|
|
5
|
+
const require_localization_getLocalizedUrl = require('./getLocalizedUrl.cjs');
|
|
6
|
+
const require_localization_getMultilingualUrls = require('./getMultilingualUrls.cjs');
|
|
7
|
+
const require_localization_generateSitemap = require('./generateSitemap.cjs');
|
|
3
8
|
const require_localization_localeResolver = require('./localeResolver.cjs');
|
|
4
9
|
const require_localization_localeDetector = require('./localeDetector.cjs');
|
|
5
10
|
const require_localization_getBrowserLocale = require('./getBrowserLocale.cjs');
|
|
6
11
|
const require_localization_getHTMLTextDir = require('./getHTMLTextDir.cjs');
|
|
7
12
|
const require_localization_getLocale = require('./getLocale.cjs');
|
|
8
|
-
const require_localization_getPrefix = require('./getPrefix.cjs');
|
|
9
13
|
const require_localization_getLocaleFromPath = require('./getLocaleFromPath.cjs');
|
|
10
14
|
const require_localization_getLocaleLang = require('./getLocaleLang.cjs');
|
|
11
15
|
const require_localization_getLocaleName = require('./getLocaleName.cjs');
|
|
12
|
-
const require_localization_getPathWithoutLocale = require('./getPathWithoutLocale.cjs');
|
|
13
|
-
const require_localization_rewriteUtils = require('./rewriteUtils.cjs');
|
|
14
|
-
const require_localization_getLocalizedUrl = require('./getLocalizedUrl.cjs');
|
|
15
|
-
const require_localization_getMultilingualUrls = require('./getMultilingualUrls.cjs');
|
|
16
16
|
const require_localization_localeMapper = require('./localeMapper.cjs');
|
|
17
17
|
const require_localization_validatePrefix = require('./validatePrefix.cjs');
|
|
18
18
|
|
|
19
|
+
exports.generateSitemap = require_localization_generateSitemap.generateSitemap;
|
|
20
|
+
exports.generateSitemapUrl = require_localization_generateSitemap.generateSitemapUrl;
|
|
19
21
|
exports.getBrowserLocale = require_localization_getBrowserLocale.getBrowserLocale;
|
|
20
22
|
exports.getCanonicalPath = require_localization_rewriteUtils.getCanonicalPath;
|
|
21
23
|
exports.getHTMLTextDir = require_localization_getHTMLTextDir.getHTMLTextDir;
|
|
@@ -31,7 +33,6 @@ exports.getPathWithoutLocale = require_localization_getPathWithoutLocale.getPath
|
|
|
31
33
|
exports.getPrefix = require_localization_getPrefix.getPrefix;
|
|
32
34
|
exports.getRewritePath = require_localization_rewriteUtils.getRewritePath;
|
|
33
35
|
exports.getRewriteRules = require_localization_rewriteUtils.getRewriteRules;
|
|
34
|
-
exports.getStorageAttributes = require_getStorageAttributes.getStorageAttributes;
|
|
35
36
|
exports.localeDetector = require_localization_localeDetector.localeDetector;
|
|
36
37
|
exports.localeFlatMap = require_localization_localeMapper.localeFlatMap;
|
|
37
38
|
exports.localeMap = require_localization_localeMapper.localeMap;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
3
3
|
const require_localization_getPrefix = require('./getPrefix.cjs');
|
|
4
|
-
let _intlayer_config_defaultValues = require("@intlayer/config/defaultValues");
|
|
5
4
|
let _intlayer_config_built = require("@intlayer/config/built");
|
|
6
5
|
_intlayer_config_built = require_runtime.__toESM(_intlayer_config_built);
|
|
6
|
+
let _intlayer_config_defaultValues = require("@intlayer/config/defaultValues");
|
|
7
7
|
|
|
8
8
|
//#region src/localization/localeMapper.ts
|
|
9
9
|
/**
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_transpiler_html_htmlTags = require('./htmlTags.cjs');
|
|
3
2
|
|
|
4
3
|
//#region src/transpiler/html/getHTMLCustomComponents.ts
|
|
5
4
|
const parseAttributes = (attributesString) => {
|
|
6
5
|
const attributes = {};
|
|
7
|
-
if (!attributesString
|
|
6
|
+
if (!attributesString?.trim()) return attributes;
|
|
8
7
|
[...attributesString.matchAll(/([a-zA-Z0-9-:_@]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^>\s]+))?/g)].forEach((match) => {
|
|
9
8
|
const attrName = match[1];
|
|
10
9
|
attributes[attrName] = "string";
|
|
@@ -25,7 +24,7 @@ const getHTMLCustomComponents = (content) => {
|
|
|
25
24
|
const tagName = match[2];
|
|
26
25
|
const attributesString = match[3];
|
|
27
26
|
const isSelfClosing = !!match[4];
|
|
28
|
-
if (
|
|
27
|
+
if (/^[a-z][a-z0-9]*$/.test(tagName)) {
|
|
29
28
|
components[tagName] = true;
|
|
30
29
|
return;
|
|
31
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getHTMLCustomComponents.cjs","names":[
|
|
1
|
+
{"version":3,"file":"getHTMLCustomComponents.cjs","names":[],"sources":["../../../../src/transpiler/html/getHTMLCustomComponents.ts"],"sourcesContent":["const parseAttributes = (attributesString: string): Record<string, string> => {\n const attributes: Record<string, string> = {};\n\n if (!attributesString?.trim()) {\n return attributes;\n }\n\n // Regex to match attribute names\n // Matches: name=\"value\", name='value', name=value, or just name\n const attrRegex =\n /([a-zA-Z0-9-:_@]+)(?:\\s*=\\s*(?:\"[^\"]*\"|'[^']*'|[^>\\s]+))?/g;\n\n const matches = [...attributesString.matchAll(attrRegex)];\n\n matches.forEach((match) => {\n const attrName = match[1];\n attributes[attrName] = 'string';\n });\n\n return attributes;\n};\n\n/**\n * Extracts component names from an HTML string.\n * - Standard HTML tags are set to `true`.\n * - Custom components are parsed to extract their attributes/props.\n */\nexport const getHTMLCustomComponents = (\n content: string\n): Record<string, Record<string, string> | true> => {\n if (typeof content !== 'string') {\n throw new Error('content must be a string');\n }\n\n const tagRegex = /<(\\/)?([a-zA-Z0-9.-]+)\\s*([\\s\\S]*?)(\\/?)>/g;\n const matches = [...content.matchAll(tagRegex)];\n\n const components: Record<string, Record<string, string> | true> = {};\n\n matches.forEach((match) => {\n const isClosing = !!match[1];\n const tagName = match[2];\n const attributesString = match[3];\n const isSelfClosing = !!match[4];\n\n // Matches any tag that is entirely lowercase letters and numbers (e.g., div, h1)\n const isStandardHTMLTag = /^[a-z][a-z0-9]*$/.test(tagName);\n\n if (isStandardHTMLTag) {\n components[tagName] = true;\n return;\n }\n\n if (!components[tagName]) {\n components[tagName] = {};\n }\n\n if (components[tagName] === true) {\n return;\n }\n\n if (isClosing) {\n return;\n }\n\n const attributes = parseAttributes(attributesString);\n const componentDef = components[tagName] as Record<string, string>;\n Object.assign(componentDef, attributes);\n\n if (!isSelfClosing) {\n componentDef.children = 'string';\n }\n });\n\n return components;\n};\n"],"mappings":";;;AAAA,MAAM,mBAAmB,qBAAqD;CAC5E,MAAM,aAAqC,EAAE;AAE7C,KAAI,CAAC,kBAAkB,MAAM,CAC3B,QAAO;AAUT,CAFgB,CAAC,GAAG,iBAAiB,SAFnC,6DAEsD,CAAC,CAEjD,SAAS,UAAU;EACzB,MAAM,WAAW,MAAM;AACvB,aAAW,YAAY;GACvB;AAEF,QAAO;;;;;;;AAQT,MAAa,2BACX,YACkD;AAClD,KAAI,OAAO,YAAY,SACrB,OAAM,IAAI,MAAM,2BAA2B;CAI7C,MAAM,UAAU,CAAC,GAAG,QAAQ,SADX,6CAC6B,CAAC;CAE/C,MAAM,aAA4D,EAAE;AAEpE,SAAQ,SAAS,UAAU;EACzB,MAAM,YAAY,CAAC,CAAC,MAAM;EAC1B,MAAM,UAAU,MAAM;EACtB,MAAM,mBAAmB,MAAM;EAC/B,MAAM,gBAAgB,CAAC,CAAC,MAAM;AAK9B,MAF0B,mBAAmB,KAAK,QAAQ,EAEnC;AACrB,cAAW,WAAW;AACtB;;AAGF,MAAI,CAAC,WAAW,SACd,YAAW,WAAW,EAAE;AAG1B,MAAI,WAAW,aAAa,KAC1B;AAGF,MAAI,UACF;EAGF,MAAM,aAAa,gBAAgB,iBAAiB;EACpD,MAAM,eAAe,WAAW;AAChC,SAAO,OAAO,cAAc,WAAW;AAEvC,MAAI,CAAC,cACH,cAAa,WAAW;GAE1B;AAEF,QAAO"}
|
package/dist/cjs/utils/index.cjs
CHANGED
|
@@ -2,20 +2,26 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
2
2
|
const require_utils_parseYaml = require('./parseYaml.cjs');
|
|
3
3
|
const require_utils_isValidReactElement = require('./isValidReactElement.cjs');
|
|
4
4
|
const require_utils_intl = require('./intl.cjs');
|
|
5
|
+
const require_utils_checkIsURLAbsolute = require('./checkIsURLAbsolute.cjs');
|
|
5
6
|
const require_utils_getCookie = require('./getCookie.cjs');
|
|
6
7
|
const require_utils_localeStorage = require('./localeStorage.cjs');
|
|
7
|
-
const require_utils_checkIsURLAbsolute = require('./checkIsURLAbsolute.cjs');
|
|
8
8
|
const require_utils_isSameKeyPath = require('./isSameKeyPath.cjs');
|
|
9
9
|
|
|
10
10
|
exports.CachedIntl = require_utils_intl.CachedIntl;
|
|
11
11
|
exports.Intl = require_utils_intl.CachedIntl;
|
|
12
12
|
exports.LocaleStorage = require_utils_localeStorage.LocaleStorage;
|
|
13
|
+
exports.LocaleStorageClient = require_utils_localeStorage.LocaleStorageClient;
|
|
14
|
+
exports.LocaleStorageServer = require_utils_localeStorage.LocaleStorageServer;
|
|
13
15
|
exports.bindIntl = require_utils_intl.bindIntl;
|
|
14
16
|
exports.checkIsURLAbsolute = require_utils_checkIsURLAbsolute.checkIsURLAbsolute;
|
|
15
|
-
exports.
|
|
17
|
+
exports.getCachedIntl = require_utils_intl.getCachedIntl;
|
|
16
18
|
exports.getCookie = require_utils_getCookie.getCookie;
|
|
17
19
|
exports.getLocaleFromStorage = require_utils_localeStorage.getLocaleFromStorage;
|
|
20
|
+
exports.getLocaleFromStorageClient = require_utils_localeStorage.getLocaleFromStorageClient;
|
|
21
|
+
exports.getLocaleFromStorageServer = require_utils_localeStorage.getLocaleFromStorageServer;
|
|
18
22
|
exports.isSameKeyPath = require_utils_isSameKeyPath.isSameKeyPath;
|
|
19
23
|
exports.isValidElement = require_utils_isValidReactElement.isValidElement;
|
|
20
24
|
exports.parseYaml = require_utils_parseYaml.parseYaml;
|
|
21
|
-
exports.setLocaleInStorage = require_utils_localeStorage.setLocaleInStorage;
|
|
25
|
+
exports.setLocaleInStorage = require_utils_localeStorage.setLocaleInStorage;
|
|
26
|
+
exports.setLocaleInStorageClient = require_utils_localeStorage.setLocaleInStorageClient;
|
|
27
|
+
exports.setLocaleInStorageServer = require_utils_localeStorage.setLocaleInStorageServer;
|
package/dist/cjs/utils/intl.cjs
CHANGED
|
@@ -1,92 +1,108 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
3
|
-
let
|
|
3
|
+
let _intlayer_config_built = require("@intlayer/config/built");
|
|
4
|
+
_intlayer_config_built = require_runtime.__toESM(_intlayer_config_built);
|
|
4
5
|
|
|
5
6
|
//#region src/utils/intl.ts
|
|
6
7
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Cached Intl helper – drop‑in replacement for the global `Intl` object.
|
|
9
|
+
* ‑‑‑
|
|
10
|
+
* • Uses a `Proxy` to lazily wrap every *constructor* hanging off `Intl` (NumberFormat, DateTimeFormat, …).
|
|
11
|
+
* • Each wrapped constructor keeps an in‑memory cache keyed by `[locales, options]` so that identical requests
|
|
12
|
+
* reuse the same heavy instance instead of reparsing CLDR data every time.
|
|
13
|
+
* • A polyfill warning for `Intl.DisplayNames` is emitted only once and only in dev.
|
|
14
|
+
* • The public API is fully type‑safe and mirrors the native `Intl` surface exactly –
|
|
15
|
+
* you can treat `CachedIntl` just like the built‑in `Intl`.
|
|
16
|
+
*
|
|
17
|
+
* Usage @example:
|
|
18
|
+
* ---------------
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { CachedIntl } from "./cached-intl";
|
|
21
|
+
*
|
|
22
|
+
* const nf = CachedIntl.NumberFormat("en-US", { style: "currency", currency: "USD" });
|
|
23
|
+
* console.log(nf.format(1234));
|
|
24
|
+
*
|
|
25
|
+
* const dn = CachedIntl.DisplayNames(["fr"], { type: "language" });
|
|
26
|
+
* console.log(dn.of("en")); * → "anglais"
|
|
27
|
+
*
|
|
28
|
+
* You can also spin up an isolated instance with its own caches (handy in test suites):
|
|
29
|
+
* const TestIntl = createCachedIntl();
|
|
30
|
+
* ```
|
|
10
31
|
*/
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
if (!options) return localeKey;
|
|
14
|
-
return `${localeKey}|${JSON.stringify(options)}`;
|
|
15
|
-
};
|
|
32
|
+
const MAX_CACHE_SIZE = 50;
|
|
33
|
+
const cache = /* @__PURE__ */ new Map();
|
|
16
34
|
/**
|
|
17
|
-
* Generic
|
|
35
|
+
* Generic caching instantiator for Intl constructors.
|
|
18
36
|
*/
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
resolvedOptions = locales;
|
|
27
|
-
resolvedLocales = locales.locale;
|
|
28
|
-
}
|
|
29
|
-
if (Ctor.name === "DisplayNames" && typeof Intl?.DisplayNames !== "function") {}
|
|
30
|
-
const key = getCacheKey(resolvedLocales, resolvedOptions);
|
|
31
|
-
let instance = cache.get(key);
|
|
32
|
-
if (instance) return instance;
|
|
33
|
-
instance = new Ctor(resolvedLocales, resolvedOptions);
|
|
34
|
-
if (cache.size >= MAX_CACHE_SIZE) {
|
|
35
|
-
const oldestKey = cache.keys().next().value;
|
|
36
|
-
if (oldestKey) cache.delete(oldestKey);
|
|
37
|
-
}
|
|
38
|
-
cache.set(key, instance);
|
|
39
|
-
return instance;
|
|
37
|
+
const getCachedIntl = (Ctor, locale, options) => {
|
|
38
|
+
const resLoc = locale ?? _intlayer_config_built.default?.internationalization?.defaultLocale;
|
|
39
|
+
const key = `${resLoc}|${options ? JSON.stringify(options) : ""}`;
|
|
40
|
+
let ctorCache = cache.get(Ctor);
|
|
41
|
+
if (!ctorCache) {
|
|
42
|
+
ctorCache = /* @__PURE__ */ new Map();
|
|
43
|
+
cache.set(Ctor, ctorCache);
|
|
40
44
|
}
|
|
41
|
-
|
|
42
|
-
|
|
45
|
+
let instance = ctorCache.get(key);
|
|
46
|
+
if (!instance) {
|
|
47
|
+
if (ctorCache.size > MAX_CACHE_SIZE) ctorCache.clear();
|
|
48
|
+
instance = new Ctor(resLoc, options);
|
|
49
|
+
ctorCache.set(key, instance);
|
|
50
|
+
}
|
|
51
|
+
return instance;
|
|
43
52
|
};
|
|
44
53
|
/**
|
|
45
|
-
*
|
|
54
|
+
* Optional: Keep bindIntl if your library exports it publicly.
|
|
55
|
+
* It now uses the much smaller getCachedIntl under the hood.
|
|
46
56
|
*/
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
const bindIntl = (boundLocale) => {
|
|
58
|
+
const bindWrap = (Ctor) => function intlConstructor(locales, options) {
|
|
59
|
+
const isOptsFirst = locales !== null && typeof locales === "object" && !Array.isArray(locales);
|
|
60
|
+
const resOpts = isOptsFirst ? locales : options;
|
|
61
|
+
return getCachedIntl(Ctor, isOptsFirst ? resOpts.locale || boundLocale : locales || boundLocale, resOpts);
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
...Intl,
|
|
65
|
+
Collator: bindWrap(Intl.Collator),
|
|
66
|
+
DateTimeFormat: bindWrap(Intl.DateTimeFormat),
|
|
67
|
+
DisplayNames: bindWrap(Intl.DisplayNames),
|
|
68
|
+
ListFormat: bindWrap(Intl.ListFormat),
|
|
69
|
+
NumberFormat: bindWrap(Intl.NumberFormat),
|
|
70
|
+
PluralRules: bindWrap(Intl.PluralRules),
|
|
71
|
+
RelativeTimeFormat: bindWrap(Intl.RelativeTimeFormat),
|
|
72
|
+
Locale: bindWrap(Intl.Locale),
|
|
73
|
+
Segmenter: bindWrap(Intl.Segmenter)
|
|
74
|
+
};
|
|
59
75
|
};
|
|
60
|
-
const CachedIntl =
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return
|
|
84
|
-
}
|
|
76
|
+
const CachedIntl = {
|
|
77
|
+
Collator: function Collator(locales, options) {
|
|
78
|
+
return getCachedIntl(Intl.Collator, locales, options);
|
|
79
|
+
},
|
|
80
|
+
DateTimeFormat: function DateTimeFormat(locales, options) {
|
|
81
|
+
return getCachedIntl(Intl.DateTimeFormat, locales, options);
|
|
82
|
+
},
|
|
83
|
+
DisplayNames: function DisplayNames(locales, options) {
|
|
84
|
+
return getCachedIntl(Intl.DisplayNames, locales, options);
|
|
85
|
+
},
|
|
86
|
+
ListFormat: function ListFormat(locales, options) {
|
|
87
|
+
return getCachedIntl(Intl.ListFormat, locales, options);
|
|
88
|
+
},
|
|
89
|
+
NumberFormat: function NumberFormat(locales, options) {
|
|
90
|
+
return getCachedIntl(Intl.NumberFormat, locales, options);
|
|
91
|
+
},
|
|
92
|
+
PluralRules: function PluralRules(locales, options) {
|
|
93
|
+
return getCachedIntl(Intl.PluralRules, locales, options);
|
|
94
|
+
},
|
|
95
|
+
RelativeTimeFormat: function RelativeTimeFormat(locales, options) {
|
|
96
|
+
return getCachedIntl(Intl.RelativeTimeFormat, locales, options);
|
|
97
|
+
},
|
|
98
|
+
Segmenter: function Segmenter(locales, options) {
|
|
99
|
+
return getCachedIntl(Intl.Segmenter, locales, options);
|
|
100
|
+
}
|
|
85
101
|
};
|
|
86
102
|
|
|
87
103
|
//#endregion
|
|
88
104
|
exports.CachedIntl = CachedIntl;
|
|
89
105
|
exports.Intl = CachedIntl;
|
|
90
106
|
exports.bindIntl = bindIntl;
|
|
91
|
-
exports.
|
|
107
|
+
exports.getCachedIntl = getCachedIntl;
|
|
92
108
|
//# sourceMappingURL=intl.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"intl.cjs","names":["ENGLISH"],"sources":["../../../src/utils/intl.ts"],"sourcesContent":["// Cached Intl helper – drop‑in replacement for the global `Intl` object.\n// ‑‑‑\n// • Uses a `Proxy` to lazily wrap every *constructor* hanging off `Intl` (NumberFormat, DateTimeFormat, …).\n// • Each wrapped constructor keeps an in‑memory cache keyed by `[locales, options]` so that identical requests\n// reuse the same heavy instance instead of reparsing CLDR data every time.\n// • A polyfill warning for `Intl.DisplayNames` is emitted only once and only in dev.\n// • The public API is fully type‑safe and mirrors the native `Intl` surface exactly –\n// you can treat `CachedIntl` just like the built‑in `Intl`.\n//\n// Usage examples:\n// ---------------\n// import { CachedIntl } from \"./cached-intl\";\n//\n// const nf = CachedIntl.NumberFormat(\"en-US\", { style: \"currency\", currency: \"USD\" });\n// console.log(nf.format(1234));\n//\n// const dn = CachedIntl.DisplayNames([\"fr\"], { type: \"language\" });\n// console.log(dn.of(\"en\")); // → \"anglais\"\n//\n// You can also spin up an isolated instance with its own caches (handy in test suites):\n// const TestIntl = createCachedIntl();\n//\n// ---------------------------------------------------------------------\n\nimport { ENGLISH } from '@intlayer/types/locales';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\n\n// Helper type that picks just the constructor members off `typeof Intl`.\n// The \"capital‑letter\" heuristic is 100 % accurate today and keeps the\n// mapping short‑lived, so we don't have to manually list every constructor.\ntype IntlConstructors = {\n [K in keyof typeof Intl as (typeof Intl)[K] extends new (\n ...args: any\n ) => any\n ? K\n : never]: (typeof Intl)[K];\n};\n\n// Type wrapper to replace locale arguments with LocalesValues\ntype ReplaceLocaleWithLocalesValues<T> = T extends new (\n locales: any,\n options?: infer Options\n) => infer Instance\n ? {\n new (locales?: LocalesValues, options?: Options): Instance;\n new (options?: Options & { locale?: LocalesValues }): Instance;\n }\n : T extends new (\n locales: any\n ) => infer Instance\n ? {\n new (locales?: LocalesValues): Instance;\n new (options?: { locale?: LocalesValues }): Instance;\n }\n : T;\n\n// Wrapped Intl type with LocalesValues\nexport type WrappedIntl = {\n [K in keyof typeof Intl]: K extends keyof IntlConstructors\n ? ReplaceLocaleWithLocalesValues<(typeof Intl)[K]>\n : (typeof Intl)[K];\n};\n\n// ... (Keep your Type Helper definitions here: IntlConstructors, ReplaceLocaleWithLocalesValues, WrappedIntl) ...\n\n/**\n * Optimized Cache Key Generator\n * 1. Fast path: If no options, just use the locale string.\n * 2. Normal path: JSON.stringify for deterministic object comparison.\n */\nconst getCacheKey = (\n locales: LocalesValues | undefined,\n options: unknown\n): string => {\n const localeKey = locales ? String(locales) : ENGLISH;\n\n if (!options) return localeKey;\n\n // JSON.stringify is the most robust way to handle nested options objects\n // without a heavy custom hashing function.\n return `${localeKey}|${JSON.stringify(options)}`;\n};\n\n/**\n * Generic wrapper for any `new Intl.*()` constructor.\n */\nconst createCachedConstructor = <T extends new (...args: any[]) => any>(\n Ctor: T\n) => {\n // The cache lives here, inside the closure of the wrapped constructor.\n const cache = new Map<string, InstanceType<T>>();\n const MAX_CACHE_SIZE = 50;\n\n function Wrapped(locales?: LocalesValues | any, options?: any) {\n let resolvedLocales = locales;\n let resolvedOptions = options;\n\n // Handle case where first argument is an options object instead of locales\n if (\n typeof locales === 'object' &&\n !Array.isArray(locales) &&\n locales !== null\n ) {\n resolvedOptions = locales;\n resolvedLocales = locales.locale;\n }\n\n // Handle DisplayNames Polyfill warning\n if (\n Ctor.name === 'DisplayNames' &&\n typeof (Intl as any)?.DisplayNames !== 'function'\n ) {\n // ... (Existing polyfill logic would go here if needed, but let's keep it simple for now as it was empty in the read output)\n // Actually the read output had \"// ... (Your existing polyfill warning logic) ...\"\n // I should check what was there before or just preserve it.\n }\n\n // Generate Key\n const key = getCacheKey(resolvedLocales, resolvedOptions);\n\n // Check Cache\n let instance = cache.get(key);\n if (instance) return instance;\n\n // Create New Instance\n instance = new Ctor(resolvedLocales as never, resolvedOptions as never);\n\n // Smart Eviction (LRU-ish)\n // Map iterates in insertion order. Deleting the first key removes the \"oldest\".\n if (cache.size >= MAX_CACHE_SIZE) {\n const oldestKey = cache.keys().next().value;\n if (oldestKey) cache.delete(oldestKey);\n }\n\n cache.set(key, instance as InstanceType<T>);\n return instance as InstanceType<T>;\n }\n\n // Preserve prototype for `instanceof` checks\n (Wrapped as any).prototype = (Ctor as any).prototype;\n\n return Wrapped as unknown as ReplaceLocaleWithLocalesValues<T>;\n};\n\n/**\n * Factory that turns the global `Intl` into a cached clone.\n */\nexport const createCachedIntl = (): WrappedIntl => {\n // We must cache the *wrapped constructors* themselves.\n // Otherwise, the Proxy creates a new `Wrapped` function (and a new empty Map)\n // on every single property access.\n const constructorCache = new Map<string | symbol, any>();\n\n return new Proxy(Intl as IntlConstructors, {\n get: (target, prop, receiver) => {\n // Fast return if we already wrapped this constructor\n if (constructorCache.has(prop)) {\n return constructorCache.get(prop);\n }\n\n const value = Reflect.get(target, prop, receiver);\n\n // Wrap only Constructors (Heuristic: Function + starts with Uppercase)\n // This prevents wrapping static methods like `Intl.getCanonicalLocales`\n if (\n typeof value === 'function' &&\n typeof prop === 'string' &&\n /^[A-Z]/.test(prop)\n ) {\n const wrapped = createCachedConstructor(value);\n constructorCache.set(prop, wrapped);\n return wrapped;\n }\n\n // Pass through everything else (static methods, constants)\n return value;\n },\n }) as unknown as WrappedIntl;\n};\n\nexport const CachedIntl = createCachedIntl();\n\n/**\n * Creates a proxied Intl object with a preset locale.\n *\n * @example\n * const intl = bindIntl(Locales.FRENCH);\n * new intl.NumberFormat(undefined, { style: 'currency', currency: 'EUR' }).format(10);\n * // Uses 'fr' automatically\n */\nexport const bindIntl = (locale: LocalesValues): WrappedIntl => {\n return new Proxy(CachedIntl, {\n get: (target, prop) => {\n const value = Reflect.get(target, prop);\n\n // We only want to intercept Constructors (e.g., NumberFormat, DateTimeFormat)\n // to inject the locale into their arguments.\n if (\n typeof value === 'function' &&\n typeof prop === 'string' &&\n /^[A-Z]/.test(prop)\n ) {\n return new Proxy(value, {\n construct: (Ctor, args) => {\n let [locales, options] = args;\n\n // If the user provided a locale (args[0]), respect it.\n // If args[0] is undefined, inject the bound locale.\n // If args[0] is an object (not array), it's the options object.\n if (\n typeof locales === 'object' &&\n !Array.isArray(locales) &&\n locales !== null\n ) {\n options = locales;\n locales = options.locale ?? locale;\n } else if (locales === undefined) {\n locales = locale;\n }\n\n // We pass it to `CachedIntl` which handles caching logic.\n return new Ctor(locales, options);\n },\n // Ensure static methods (like supportedLocalesOf) still work\n get: (Ctor, key) => Reflect.get(Ctor, key),\n });\n }\n\n // Return constants or static methods as-is\n return value;\n },\n }) as unknown as WrappedIntl;\n};\n\n// new CachedIntl.DisplayNames(Locales.FRENCH, { type: 'language' });\n// new CachedIntl.DisplayNames('fr', { type: 'language' });\n// new CachedIntl.DateTimeFormat('fr', {\n// year: 'numeric',\n// month: 'long',\n// day: 'numeric',\n// });\n// new CachedIntl.NumberFormat('fr', {\n// style: 'currency',\n// currency: 'EUR',\n// });\n// new CachedIntl.Collator('fr', { sensitivity: 'base' });\n// new CachedIntl.PluralRules('fr');\n// new CachedIntl.RelativeTimeFormat('fr', { numeric: 'auto' });\n// new CachedIntl.ListFormat('fr', { type: 'conjunction' });\nexport { CachedIntl as Intl };\n"],"mappings":";;;;;;;;;;AAsEA,MAAM,eACJ,SACA,YACW;CACX,MAAM,YAAY,UAAU,OAAO,QAAQ,GAAGA;AAE9C,KAAI,CAAC,QAAS,QAAO;AAIrB,QAAO,GAAG,UAAU,GAAG,KAAK,UAAU,QAAQ;;;;;AAMhD,MAAM,2BACJ,SACG;CAEH,MAAM,wBAAQ,IAAI,KAA8B;CAChD,MAAM,iBAAiB;CAEvB,SAAS,QAAQ,SAA+B,SAAe;EAC7D,IAAI,kBAAkB;EACtB,IAAI,kBAAkB;AAGtB,MACE,OAAO,YAAY,YACnB,CAAC,MAAM,QAAQ,QAAQ,IACvB,YAAY,MACZ;AACA,qBAAkB;AAClB,qBAAkB,QAAQ;;AAI5B,MACE,KAAK,SAAS,kBACd,OAAQ,MAAc,iBAAiB,YACvC;EAOF,MAAM,MAAM,YAAY,iBAAiB,gBAAgB;EAGzD,IAAI,WAAW,MAAM,IAAI,IAAI;AAC7B,MAAI,SAAU,QAAO;AAGrB,aAAW,IAAI,KAAK,iBAA0B,gBAAyB;AAIvE,MAAI,MAAM,QAAQ,gBAAgB;GAChC,MAAM,YAAY,MAAM,MAAM,CAAC,MAAM,CAAC;AACtC,OAAI,UAAW,OAAM,OAAO,UAAU;;AAGxC,QAAM,IAAI,KAAK,SAA4B;AAC3C,SAAO;;AAIT,CAAC,QAAgB,YAAa,KAAa;AAE3C,QAAO;;;;;AAMT,MAAa,yBAAsC;CAIjD,MAAM,mCAAmB,IAAI,KAA2B;AAExD,QAAO,IAAI,MAAM,MAA0B,EACzC,MAAM,QAAQ,MAAM,aAAa;AAE/B,MAAI,iBAAiB,IAAI,KAAK,CAC5B,QAAO,iBAAiB,IAAI,KAAK;EAGnC,MAAM,QAAQ,QAAQ,IAAI,QAAQ,MAAM,SAAS;AAIjD,MACE,OAAO,UAAU,cACjB,OAAO,SAAS,YAChB,SAAS,KAAK,KAAK,EACnB;GACA,MAAM,UAAU,wBAAwB,MAAM;AAC9C,oBAAiB,IAAI,MAAM,QAAQ;AACnC,UAAO;;AAIT,SAAO;IAEV,CAAC;;AAGJ,MAAa,aAAa,kBAAkB;;;;;;;;;AAU5C,MAAa,YAAY,WAAuC;AAC9D,QAAO,IAAI,MAAM,YAAY,EAC3B,MAAM,QAAQ,SAAS;EACrB,MAAM,QAAQ,QAAQ,IAAI,QAAQ,KAAK;AAIvC,MACE,OAAO,UAAU,cACjB,OAAO,SAAS,YAChB,SAAS,KAAK,KAAK,CAEnB,QAAO,IAAI,MAAM,OAAO;GACtB,YAAY,MAAM,SAAS;IACzB,IAAI,CAAC,SAAS,WAAW;AAKzB,QACE,OAAO,YAAY,YACnB,CAAC,MAAM,QAAQ,QAAQ,IACvB,YAAY,MACZ;AACA,eAAU;AACV,eAAU,QAAQ,UAAU;eACnB,YAAY,OACrB,WAAU;AAIZ,WAAO,IAAI,KAAK,SAAS,QAAQ;;GAGnC,MAAM,MAAM,QAAQ,QAAQ,IAAI,MAAM,IAAI;GAC3C,CAAC;AAIJ,SAAO;IAEV,CAAC"}
|
|
1
|
+
{"version":3,"file":"intl.cjs","names":["configuration"],"sources":["../../../src/utils/intl.ts"],"sourcesContent":["/**\n * Cached Intl helper – drop‑in replacement for the global `Intl` object.\n * ‑‑‑\n * • Uses a `Proxy` to lazily wrap every *constructor* hanging off `Intl` (NumberFormat, DateTimeFormat, …).\n * • Each wrapped constructor keeps an in‑memory cache keyed by `[locales, options]` so that identical requests\n * reuse the same heavy instance instead of reparsing CLDR data every time.\n * • A polyfill warning for `Intl.DisplayNames` is emitted only once and only in dev.\n * • The public API is fully type‑safe and mirrors the native `Intl` surface exactly –\n * you can treat `CachedIntl` just like the built‑in `Intl`.\n *\n * Usage @example:\n * ---------------\n * ```ts\n * import { CachedIntl } from \"./cached-intl\";\n *\n * const nf = CachedIntl.NumberFormat(\"en-US\", { style: \"currency\", currency: \"USD\" });\n * console.log(nf.format(1234));\n *\n * const dn = CachedIntl.DisplayNames([\"fr\"], { type: \"language\" });\n * console.log(dn.of(\"en\")); * → \"anglais\"\n *\n * You can also spin up an isolated instance with its own caches (handy in test suites):\n * const TestIntl = createCachedIntl();\n * ```\n */\n\nimport configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\n\nconst MAX_CACHE_SIZE = 50;\nconst cache = new Map<any, Map<string, any>>();\n\ntype IntlConstructors = {\n [K in keyof typeof Intl as (typeof Intl)[K] extends new (\n ...args: any\n ) => any\n ? K\n : never]: (typeof Intl)[K];\n};\n\ntype ReplaceLocaleWithLocalesValues<T> = T extends new (\n locales: any,\n options?: infer Options\n) => infer Instance\n ? {\n new (locales?: LocalesValues, options?: Options): Instance;\n new (options?: Options & { locale?: LocalesValues }): Instance;\n (locales?: LocalesValues, options?: Options): Instance;\n (options?: Options & { locale?: LocalesValues }): Instance;\n }\n : T extends new (\n locales: any\n ) => infer Instance\n ? {\n new (locales?: LocalesValues): Instance;\n new (options?: { locale?: LocalesValues }): Instance;\n (locales?: LocalesValues): Instance;\n (options?: { locale?: LocalesValues }): Instance;\n }\n : T;\n\nexport type WrappedIntl = {\n [K in keyof typeof Intl]: K extends keyof IntlConstructors\n ? ReplaceLocaleWithLocalesValues<(typeof Intl)[K]>\n : (typeof Intl)[K];\n};\n\n/**\n * Generic caching instantiator for Intl constructors.\n */\nexport const getCachedIntl = <T extends new (...args: any[]) => any>(\n Ctor: T,\n locale?: LocalesValues | string,\n options?: any\n): InstanceType<T> => {\n const resLoc = locale ?? configuration?.internationalization?.defaultLocale;\n const optKey = options ? JSON.stringify(options) : '';\n const key = `${resLoc}|${optKey}`;\n\n let ctorCache = cache.get(Ctor);\n\n if (!ctorCache) {\n ctorCache = new Map();\n cache.set(Ctor, ctorCache);\n }\n\n let instance = ctorCache.get(key);\n\n if (!instance) {\n if (ctorCache.size > MAX_CACHE_SIZE) ctorCache.clear();\n instance = new Ctor(resLoc, options);\n ctorCache.set(key, instance);\n }\n return instance;\n};\n\n/**\n * Optional: Keep bindIntl if your library exports it publicly.\n * It now uses the much smaller getCachedIntl under the hood.\n */\nexport const bindIntl = (boundLocale: LocalesValues): WrappedIntl => {\n const bindWrap = (Ctor: any) =>\n // function is used as a constructor, do not change in arrow function\n function intlConstructor(locales?: any, options?: any) {\n const isOptsFirst =\n locales !== null &&\n typeof locales === 'object' &&\n !Array.isArray(locales);\n const resOpts = isOptsFirst ? locales : options;\n const resLoc = isOptsFirst\n ? (resOpts as any).locale || boundLocale\n : locales || boundLocale;\n\n return getCachedIntl(Ctor, resLoc, resOpts);\n };\n\n return {\n ...Intl,\n Collator: bindWrap(Intl.Collator),\n DateTimeFormat: bindWrap(Intl.DateTimeFormat),\n DisplayNames: bindWrap(Intl.DisplayNames),\n ListFormat: bindWrap(Intl.ListFormat),\n NumberFormat: bindWrap(Intl.NumberFormat),\n PluralRules: bindWrap(Intl.PluralRules),\n RelativeTimeFormat: bindWrap(Intl.RelativeTimeFormat),\n Locale: bindWrap(Intl.Locale),\n Segmenter: bindWrap((Intl as any).Segmenter),\n } as unknown as WrappedIntl;\n};\n\n// Add this to the bottom of utils/intl.ts ONLY if required for public API compatibility.\nexport const CachedIntl = {\n // function is used as a constructor, do not change in arrow function\n Collator: function Collator(locales?: any, options?: any) {\n return getCachedIntl(Intl.Collator, locales, options);\n },\n DateTimeFormat: function DateTimeFormat(locales?: any, options?: any) {\n return getCachedIntl(Intl.DateTimeFormat, locales, options);\n },\n DisplayNames: function DisplayNames(locales?: any, options?: any) {\n return getCachedIntl(Intl.DisplayNames, locales, options);\n },\n ListFormat: function ListFormat(locales?: any, options?: any) {\n return getCachedIntl(Intl.ListFormat as any, locales, options);\n },\n NumberFormat: function NumberFormat(locales?: any, options?: any) {\n return getCachedIntl(Intl.NumberFormat, locales, options);\n },\n PluralRules: function PluralRules(locales?: any, options?: any) {\n return getCachedIntl(Intl.PluralRules, locales, options);\n },\n RelativeTimeFormat: function RelativeTimeFormat(\n locales?: any,\n options?: any\n ) {\n return getCachedIntl(Intl.RelativeTimeFormat, locales, options);\n },\n Segmenter: function Segmenter(locales?: any, options?: any) {\n return getCachedIntl((Intl as any).Segmenter, locales, options);\n },\n} as any; // Cast to 'any' internally to avoid TS readonly errors\n\nexport { CachedIntl as Intl };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAM,iBAAiB;AACvB,MAAM,wBAAQ,IAAI,KAA4B;;;;AAwC9C,MAAa,iBACX,MACA,QACA,YACoB;CACpB,MAAM,SAAS,UAAUA,gCAAe,sBAAsB;CAE9D,MAAM,MAAM,GAAG,OAAO,GADP,UAAU,KAAK,UAAU,QAAQ,GAAG;CAGnD,IAAI,YAAY,MAAM,IAAI,KAAK;AAE/B,KAAI,CAAC,WAAW;AACd,8BAAY,IAAI,KAAK;AACrB,QAAM,IAAI,MAAM,UAAU;;CAG5B,IAAI,WAAW,UAAU,IAAI,IAAI;AAEjC,KAAI,CAAC,UAAU;AACb,MAAI,UAAU,OAAO,eAAgB,WAAU,OAAO;AACtD,aAAW,IAAI,KAAK,QAAQ,QAAQ;AACpC,YAAU,IAAI,KAAK,SAAS;;AAE9B,QAAO;;;;;;AAOT,MAAa,YAAY,gBAA4C;CACnE,MAAM,YAAY,SAEhB,SAAS,gBAAgB,SAAe,SAAe;EACrD,MAAM,cACJ,YAAY,QACZ,OAAO,YAAY,YACnB,CAAC,MAAM,QAAQ,QAAQ;EACzB,MAAM,UAAU,cAAc,UAAU;AAKxC,SAAO,cAAc,MAJN,cACV,QAAgB,UAAU,cAC3B,WAAW,aAEoB,QAAQ;;AAG/C,QAAO;EACL,GAAG;EACH,UAAU,SAAS,KAAK,SAAS;EACjC,gBAAgB,SAAS,KAAK,eAAe;EAC7C,cAAc,SAAS,KAAK,aAAa;EACzC,YAAY,SAAS,KAAK,WAAW;EACrC,cAAc,SAAS,KAAK,aAAa;EACzC,aAAa,SAAS,KAAK,YAAY;EACvC,oBAAoB,SAAS,KAAK,mBAAmB;EACrD,QAAQ,SAAS,KAAK,OAAO;EAC7B,WAAW,SAAU,KAAa,UAAU;EAC7C;;AAIH,MAAa,aAAa;CAExB,UAAU,SAAS,SAAS,SAAe,SAAe;AACxD,SAAO,cAAc,KAAK,UAAU,SAAS,QAAQ;;CAEvD,gBAAgB,SAAS,eAAe,SAAe,SAAe;AACpE,SAAO,cAAc,KAAK,gBAAgB,SAAS,QAAQ;;CAE7D,cAAc,SAAS,aAAa,SAAe,SAAe;AAChE,SAAO,cAAc,KAAK,cAAc,SAAS,QAAQ;;CAE3D,YAAY,SAAS,WAAW,SAAe,SAAe;AAC5D,SAAO,cAAc,KAAK,YAAmB,SAAS,QAAQ;;CAEhE,cAAc,SAAS,aAAa,SAAe,SAAe;AAChE,SAAO,cAAc,KAAK,cAAc,SAAS,QAAQ;;CAE3D,aAAa,SAAS,YAAY,SAAe,SAAe;AAC9D,SAAO,cAAc,KAAK,aAAa,SAAS,QAAQ;;CAE1D,oBAAoB,SAAS,mBAC3B,SACA,SACA;AACA,SAAO,cAAc,KAAK,oBAAoB,SAAS,QAAQ;;CAEjE,WAAW,SAAS,UAAU,SAAe,SAAe;AAC1D,SAAO,cAAe,KAAa,WAAW,SAAS,QAAQ;;CAElE"}
|