@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
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
3
|
-
const require_getStorageAttributes = require('../getStorageAttributes.cjs');
|
|
4
3
|
const require_utils_getCookie = require('./getCookie.cjs');
|
|
5
4
|
let _intlayer_config_built = require("@intlayer/config/built");
|
|
6
5
|
_intlayer_config_built = require_runtime.__toESM(_intlayer_config_built);
|
|
@@ -16,78 +15,210 @@ const buildCookieString = (name, value, attributes) => {
|
|
|
16
15
|
return parts.join("; ");
|
|
17
16
|
};
|
|
18
17
|
/**
|
|
19
|
-
* Retrieves the locale from
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* @returns The locale if found in any storage, or undefined if not found
|
|
18
|
+
* Retrieves the locale from browser storage mechanisms
|
|
19
|
+
* (cookies, localStorage, sessionStorage).
|
|
20
|
+
* Does not read from headers — use `getLocaleFromStorageServer` for that.
|
|
23
21
|
*/
|
|
24
|
-
const
|
|
22
|
+
const getLocaleFromStorageClient = (options) => {
|
|
25
23
|
const { routing, internationalization } = _intlayer_config_built.default;
|
|
26
24
|
const { locales } = internationalization;
|
|
27
|
-
const
|
|
28
|
-
if (
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return locales.includes(value);
|
|
33
|
-
};
|
|
34
|
-
const readCookie = (name) => {
|
|
35
|
-
try {
|
|
36
|
-
const fromOption = options?.getCookie?.(name);
|
|
37
|
-
if (fromOption !== null && fromOption !== void 0) return fromOption;
|
|
38
|
-
} catch {}
|
|
39
|
-
return require_utils_getCookie.getCookie(name);
|
|
40
|
-
};
|
|
41
|
-
for (let i = 0; i < storageAttributes.cookies.length; i++) {
|
|
42
|
-
const { name } = storageAttributes.cookies[i];
|
|
43
|
-
const value = readCookie(name);
|
|
25
|
+
const storageAttributes = routing.storage;
|
|
26
|
+
if (options?.isCookieEnabled === false) return void 0;
|
|
27
|
+
const isValidLocale = (value) => !!value && locales.includes(value);
|
|
28
|
+
for (let i = 0; i < storageAttributes.cookies.length; i++) try {
|
|
29
|
+
const value = options?.getCookie?.(storageAttributes.cookies[i].name);
|
|
44
30
|
if (isValidLocale(value)) return value;
|
|
31
|
+
} catch {}
|
|
32
|
+
for (let i = 0; i < storageAttributes.localStorage.length; i++) try {
|
|
33
|
+
const value = options?.getLocaleStorage?.(storageAttributes.localStorage[i].name);
|
|
34
|
+
if (isValidLocale(value)) return value;
|
|
35
|
+
} catch {}
|
|
36
|
+
for (let i = 0; i < storageAttributes.sessionStorage.length; i++) try {
|
|
37
|
+
const value = options?.getSessionStorage?.(storageAttributes.sessionStorage[i].name);
|
|
38
|
+
if (isValidLocale(value)) return value;
|
|
39
|
+
} catch {}
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Stores the locale in browser storage mechanisms
|
|
43
|
+
* (cookies, localStorage, sessionStorage).
|
|
44
|
+
* Does not write to headers — use `setLocaleInStorageServer` for that.
|
|
45
|
+
*/
|
|
46
|
+
const setLocaleInStorageClient = (locale, options) => {
|
|
47
|
+
const { routing } = _intlayer_config_built.default;
|
|
48
|
+
const storageAttributes = routing.storage;
|
|
49
|
+
if (options?.isCookieEnabled === false) return;
|
|
50
|
+
for (let i = 0; i < storageAttributes.cookies.length; i++) {
|
|
51
|
+
const { name, attributes } = storageAttributes.cookies[i];
|
|
52
|
+
try {
|
|
53
|
+
if (options?.setCookieStore) options.setCookieStore(name, locale, {
|
|
54
|
+
...attributes,
|
|
55
|
+
expires: attributes.expires instanceof Date ? attributes.expires.getTime() : attributes.expires
|
|
56
|
+
});
|
|
57
|
+
} catch {
|
|
58
|
+
try {
|
|
59
|
+
if (options?.setCookieString) options.setCookieString(name, buildCookieString(name, locale, attributes));
|
|
60
|
+
} catch {}
|
|
61
|
+
}
|
|
45
62
|
}
|
|
46
|
-
for (let i = 0; i < storageAttributes.localStorage.length; i++) {
|
|
63
|
+
if (options?.setLocaleStorage) for (let i = 0; i < storageAttributes.localStorage.length; i++) {
|
|
47
64
|
const { name } = storageAttributes.localStorage[i];
|
|
48
65
|
try {
|
|
49
|
-
|
|
50
|
-
|
|
66
|
+
if (!(options?.overwrite ?? true) && options?.getLocaleStorage) {
|
|
67
|
+
if (options.getLocaleStorage(name)) continue;
|
|
68
|
+
}
|
|
69
|
+
options.setLocaleStorage(name, locale);
|
|
51
70
|
} catch {}
|
|
52
71
|
}
|
|
53
|
-
for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {
|
|
72
|
+
if (options?.setSessionStorage) for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {
|
|
54
73
|
const { name } = storageAttributes.sessionStorage[i];
|
|
55
74
|
try {
|
|
56
|
-
|
|
57
|
-
|
|
75
|
+
if (!(options?.overwrite ?? true) && options?.getSessionStorage) {
|
|
76
|
+
if (options.getSessionStorage(name)) continue;
|
|
77
|
+
}
|
|
78
|
+
options.setSessionStorage(name, locale);
|
|
58
79
|
} catch {}
|
|
59
80
|
}
|
|
60
|
-
|
|
61
|
-
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Client-side locale storage utility.
|
|
84
|
+
* Handles cookies (browser), localStorage and sessionStorage.
|
|
85
|
+
* Does not access headers.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const storage = LocaleStorageClient(localeStorageOptions);
|
|
90
|
+
* const locale = storage.getLocale();
|
|
91
|
+
* storage.setLocale('fr');
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
const LocaleStorageClient = (options) => ({
|
|
95
|
+
getLocale: () => getLocaleFromStorageClient(options),
|
|
96
|
+
setLocale: (locale) => setLocaleInStorageClient(locale, options)
|
|
97
|
+
});
|
|
98
|
+
/**
|
|
99
|
+
* Retrieves the locale from server-side storage mechanisms (cookies, headers).
|
|
100
|
+
* Does not access localStorage or sessionStorage.
|
|
101
|
+
* No browser cookie fallback — the caller must provide `getCookie`.
|
|
102
|
+
*/
|
|
103
|
+
const getLocaleFromStorageServer = (options) => {
|
|
104
|
+
const { routing, internationalization } = _intlayer_config_built.default;
|
|
105
|
+
const { locales } = internationalization;
|
|
106
|
+
const storageAttributes = routing.storage;
|
|
107
|
+
if (options?.isCookieEnabled === false) return void 0;
|
|
108
|
+
const isValidLocale = (value) => !!value && locales.includes(value);
|
|
109
|
+
for (let i = 0; i < storageAttributes.cookies.length; i++) try {
|
|
110
|
+
const value = options?.getCookie?.(storageAttributes.cookies[i].name);
|
|
111
|
+
if (isValidLocale(value)) return value;
|
|
112
|
+
} catch {}
|
|
113
|
+
for (let i = 0; i < storageAttributes.headers.length; i++) try {
|
|
114
|
+
const value = options?.getHeader?.(storageAttributes.headers[i].name);
|
|
115
|
+
if (isValidLocale(value)) return value;
|
|
116
|
+
} catch {}
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Stores the locale in server-side storage mechanisms (cookies, headers).
|
|
120
|
+
* Does not write to localStorage or sessionStorage.
|
|
121
|
+
*/
|
|
122
|
+
const setLocaleInStorageServer = (locale, options) => {
|
|
123
|
+
const { routing } = _intlayer_config_built.default;
|
|
124
|
+
const storageAttributes = routing.storage;
|
|
125
|
+
if (options?.isCookieEnabled === false) return;
|
|
126
|
+
for (let i = 0; i < storageAttributes.cookies.length; i++) {
|
|
127
|
+
const { name, attributes } = storageAttributes.cookies[i];
|
|
62
128
|
try {
|
|
63
|
-
|
|
64
|
-
|
|
129
|
+
if (options?.setCookieStore) options.setCookieStore(name, locale, {
|
|
130
|
+
...attributes,
|
|
131
|
+
expires: attributes.expires instanceof Date ? attributes.expires.getTime() : attributes.expires
|
|
132
|
+
});
|
|
133
|
+
} catch {
|
|
134
|
+
try {
|
|
135
|
+
if (options?.setCookieString) options.setCookieString(name, buildCookieString(name, locale, attributes));
|
|
136
|
+
} catch {}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (options?.setHeader) for (let i = 0; i < storageAttributes.headers.length; i++) try {
|
|
140
|
+
options.setHeader(storageAttributes.headers[i].name, locale);
|
|
141
|
+
} catch {}
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* Server-side locale storage utility.
|
|
145
|
+
* Handles cookies (via injected getter/setter) and headers.
|
|
146
|
+
* Does not access localStorage or sessionStorage.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* const storage = LocaleStorageServer({
|
|
151
|
+
* getCookie: (name) => req.cookies[name],
|
|
152
|
+
* setCookieStore: (name, value, attrs) => res.cookie(name, value, attrs),
|
|
153
|
+
* getHeader: (name) => req.headers[name],
|
|
154
|
+
* setHeader: (name, value) => res.setHeader(name, value),
|
|
155
|
+
* });
|
|
156
|
+
* const locale = storage.getLocale();
|
|
157
|
+
* storage.setLocale('fr');
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
const LocaleStorageServer = (options) => ({
|
|
161
|
+
getLocale: () => getLocaleFromStorageServer(options),
|
|
162
|
+
setLocale: (locale) => setLocaleInStorageServer(locale, options)
|
|
163
|
+
});
|
|
164
|
+
/**
|
|
165
|
+
* Retrieves the locale from all storage mechanisms
|
|
166
|
+
* (cookies, localStorage, sessionStorage, headers).
|
|
167
|
+
*
|
|
168
|
+
* @deprecated Use {@link getLocaleFromStorageClient} (browser) or
|
|
169
|
+
* {@link getLocaleFromStorageServer} (server) instead.
|
|
170
|
+
*/
|
|
171
|
+
const getLocaleFromStorage = (options) => {
|
|
172
|
+
const { routing, internationalization } = _intlayer_config_built.default;
|
|
173
|
+
const { locales } = internationalization;
|
|
174
|
+
const storageAttributes = routing.storage;
|
|
175
|
+
if (options?.isCookieEnabled === false) return void 0;
|
|
176
|
+
const isValidLocale = (value) => !!value && locales.includes(value);
|
|
177
|
+
const readCookie = (name) => {
|
|
178
|
+
try {
|
|
179
|
+
const fromOption = options?.getCookie?.(name);
|
|
180
|
+
if (fromOption !== null && fromOption !== void 0) return fromOption;
|
|
65
181
|
} catch {}
|
|
182
|
+
return require_utils_getCookie.getCookie(name);
|
|
183
|
+
};
|
|
184
|
+
for (let i = 0; i < storageAttributes.cookies.length; i++) {
|
|
185
|
+
const value = readCookie(storageAttributes.cookies[i].name);
|
|
186
|
+
if (isValidLocale(value)) return value;
|
|
66
187
|
}
|
|
188
|
+
for (let i = 0; i < storageAttributes.localStorage.length; i++) try {
|
|
189
|
+
const value = options?.getLocaleStorage?.(storageAttributes.localStorage[i].name);
|
|
190
|
+
if (isValidLocale(value)) return value;
|
|
191
|
+
} catch {}
|
|
192
|
+
for (let i = 0; i < storageAttributes.sessionStorage.length; i++) try {
|
|
193
|
+
const value = options?.getSessionStorage?.(storageAttributes.sessionStorage[i].name);
|
|
194
|
+
if (isValidLocale(value)) return value;
|
|
195
|
+
} catch {}
|
|
196
|
+
for (let i = 0; i < storageAttributes.headers.length; i++) try {
|
|
197
|
+
const value = options?.getHeader?.(storageAttributes.headers[i].name);
|
|
198
|
+
if (isValidLocale(value)) return value;
|
|
199
|
+
} catch {}
|
|
67
200
|
};
|
|
68
201
|
/**
|
|
69
|
-
* Stores the locale in
|
|
70
|
-
*
|
|
71
|
-
* Respects overwrite flags for localStorage and sessionStorage.
|
|
202
|
+
* Stores the locale in all configured storage mechanisms
|
|
203
|
+
* (cookies, localStorage, sessionStorage, headers).
|
|
72
204
|
*
|
|
73
|
-
* @
|
|
205
|
+
* @deprecated Use {@link setLocaleInStorageClient} (browser) or
|
|
206
|
+
* {@link setLocaleInStorageServer} (server) instead.
|
|
74
207
|
*/
|
|
75
208
|
const setLocaleInStorage = (locale, options) => {
|
|
76
|
-
|
|
77
|
-
const storageAttributes =
|
|
209
|
+
const { routing } = _intlayer_config_built.default;
|
|
210
|
+
const storageAttributes = routing.storage;
|
|
211
|
+
if (options?.isCookieEnabled === false) return;
|
|
78
212
|
for (let i = 0; i < storageAttributes.cookies.length; i++) {
|
|
79
213
|
const { name, attributes } = storageAttributes.cookies[i];
|
|
80
214
|
try {
|
|
81
|
-
if (options?.setCookieStore) options
|
|
215
|
+
if (options?.setCookieStore) options.setCookieStore(name, locale, {
|
|
82
216
|
...attributes,
|
|
83
217
|
expires: attributes.expires instanceof Date ? attributes.expires.getTime() : attributes.expires
|
|
84
218
|
});
|
|
85
219
|
} catch {
|
|
86
220
|
try {
|
|
87
|
-
if (options?.setCookieString)
|
|
88
|
-
const cookieString = buildCookieString(name, locale, attributes);
|
|
89
|
-
options?.setCookieString?.(name, cookieString);
|
|
90
|
-
}
|
|
221
|
+
if (options?.setCookieString) options.setCookieString(name, buildCookieString(name, locale, attributes));
|
|
91
222
|
} catch {}
|
|
92
223
|
}
|
|
93
224
|
}
|
|
@@ -95,39 +226,29 @@ const setLocaleInStorage = (locale, options) => {
|
|
|
95
226
|
const { name } = storageAttributes.localStorage[i];
|
|
96
227
|
try {
|
|
97
228
|
if (!(options?.overwrite ?? true) && options?.getLocaleStorage) {
|
|
98
|
-
if (options
|
|
229
|
+
if (options.getLocaleStorage(name)) continue;
|
|
99
230
|
}
|
|
100
|
-
options
|
|
231
|
+
options.setLocaleStorage(name, locale);
|
|
101
232
|
} catch {}
|
|
102
233
|
}
|
|
103
234
|
if (options?.setSessionStorage) for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {
|
|
104
235
|
const { name } = storageAttributes.sessionStorage[i];
|
|
105
236
|
try {
|
|
106
237
|
if (!(options?.overwrite ?? true) && options?.getSessionStorage) {
|
|
107
|
-
if (options
|
|
238
|
+
if (options.getSessionStorage(name)) continue;
|
|
108
239
|
}
|
|
109
|
-
options
|
|
110
|
-
} catch {}
|
|
111
|
-
}
|
|
112
|
-
if (options?.setHeader) for (let i = 0; i < storageAttributes.headers.length; i++) {
|
|
113
|
-
const { name } = storageAttributes.headers[i];
|
|
114
|
-
try {
|
|
115
|
-
options?.setHeader?.(name, locale);
|
|
240
|
+
options.setSessionStorage(name, locale);
|
|
116
241
|
} catch {}
|
|
117
242
|
}
|
|
243
|
+
if (options?.setHeader) for (let i = 0; i < storageAttributes.headers.length; i++) try {
|
|
244
|
+
options.setHeader(storageAttributes.headers[i].name, locale);
|
|
245
|
+
} catch {}
|
|
118
246
|
};
|
|
119
247
|
/**
|
|
120
|
-
* Utility object to get and set the locale in
|
|
121
|
-
*
|
|
122
|
-
* @property getLocale - Retrieves the locale from various storage mechanisms (cookies, localStorage, sessionStorage, headers).
|
|
123
|
-
* Retrieves the locale from various storage mechanisms (cookies, localStorage, sessionStorage, headers).
|
|
124
|
-
* The function checks storage locations in order of priority as defined in the configuration.
|
|
125
|
-
*
|
|
126
|
-
* @property setLocale - Stores the locale in various storage mechanisms (cookies, localStorage, sessionStorage, headers).
|
|
127
|
-
* The function writes to all configured storage locations according to their attributes.
|
|
128
|
-
* Respects overwrite flags for localStorage and sessionStorage.
|
|
248
|
+
* Utility object to get and set the locale in storage based on configuration.
|
|
129
249
|
*
|
|
130
|
-
* @
|
|
250
|
+
* @deprecated Use {@link LocaleStorageClient} (browser) or
|
|
251
|
+
* {@link LocaleStorageServer} (server) instead.
|
|
131
252
|
*/
|
|
132
253
|
const LocaleStorage = (options) => ({
|
|
133
254
|
getLocale: () => getLocaleFromStorage(options),
|
|
@@ -136,6 +257,12 @@ const LocaleStorage = (options) => ({
|
|
|
136
257
|
|
|
137
258
|
//#endregion
|
|
138
259
|
exports.LocaleStorage = LocaleStorage;
|
|
260
|
+
exports.LocaleStorageClient = LocaleStorageClient;
|
|
261
|
+
exports.LocaleStorageServer = LocaleStorageServer;
|
|
139
262
|
exports.getLocaleFromStorage = getLocaleFromStorage;
|
|
263
|
+
exports.getLocaleFromStorageClient = getLocaleFromStorageClient;
|
|
264
|
+
exports.getLocaleFromStorageServer = getLocaleFromStorageServer;
|
|
140
265
|
exports.setLocaleInStorage = setLocaleInStorage;
|
|
266
|
+
exports.setLocaleInStorageClient = setLocaleInStorageClient;
|
|
267
|
+
exports.setLocaleInStorageServer = setLocaleInStorageServer;
|
|
141
268
|
//# sourceMappingURL=localeStorage.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localeStorage.cjs","names":["configuration","getStorageAttributes","getCookie"],"sources":["../../../src/utils/localeStorage.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { CookiesAttributes } from '@intlayer/types/config';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport { getStorageAttributes } from '../getStorageAttributes';\nimport { getCookie } from './getCookie';\n\ntype CookieBuildAttributes = {\n /**\n * Cookie domain to store the locale information\n *\n * Default: undefined\n *\n * Define the domain where the cookie is available. Defaults to\n * the domain of the page where the cookie was created.\n */\n domain?: string;\n /**\n * Cookie path to store the locale information\n *\n * Default: undefined\n *\n * Define the path where the cookie is available. Defaults to '/'\n */\n path?: string;\n /**\n * Cookie secure to store the locale information\n *\n * Default: undefined\n *\n * A Boolean indicating if the cookie transmission requires a\n * secure protocol (https). Defaults to false.\n */\n secure?: boolean;\n /**\n * Cookie httpOnly to store the locale information\n *\n * Default: undefined\n *\n * The cookie httpOnly where the locale information is stored.\n */\n httpOnly?: boolean;\n /**\n * Cookie sameSite to store the locale information\n *\n * Default: undefined\n *\n * Asserts that a cookie must not be sent with cross-origin requests,\n * providing some protection against cross-site request forgery\n * attacks (CSRF)\n */\n sameSite?: 'strict' | 'lax' | 'none';\n\n /**\n * Cookie expires to store the locale information\n *\n * Default: undefined\n *\n * Define when the cookie will be removed. Value can be a Number\n * which will be interpreted as days from time of creation or a\n * Date instance. If omitted, the cookie becomes a session cookie.\n */\n expires?: number | undefined;\n};\n\nconst buildCookieString = (\n name: string,\n value: string,\n attributes: Omit<CookiesAttributes, 'name' | 'type'>\n): string => {\n const encodedValue = encodeURIComponent(value);\n const parts: string[] = [`${name}=${encodedValue}`];\n\n if (attributes.path) parts.push(`Path=${attributes.path}`);\n if (attributes.domain) parts.push(`Domain=${attributes.domain}`);\n if (attributes.expires instanceof Date)\n parts.push(`Expires=${attributes.expires.toUTCString()}`);\n\n if (attributes.secure) parts.push('Secure');\n if (attributes.sameSite) parts.push(`SameSite=${attributes.sameSite}`);\n return parts.join('; ');\n};\n\nexport type LocaleStorageOptions = {\n overwrite?: boolean;\n isCookieEnabled?: boolean;\n setCookieStore?: (\n name: string,\n value: string,\n cookie: CookieBuildAttributes\n ) => void;\n setCookieString?: (name: string, cookie: string) => void;\n getCookie?: (name: string) => string | undefined | null;\n setSessionStorage?: (name: string, value: string) => void;\n getSessionStorage?: (name: string) => string | undefined | null;\n setLocaleStorage?: (name: string, value: string) => void;\n getLocaleStorage?: (name: string) => string | undefined | null;\n getHeader?: (name: string) => string | undefined | null;\n setHeader?: (name: string, value: string) => void;\n};\n\n/**\n * Retrieves the locale from various storage mechanisms (cookies, localStorage, sessionStorage, headers).\n * The function checks storage locations in order of priority as defined in the configuration.\n *\n * @returns The locale if found in any storage, or undefined if not found\n */\nexport const getLocaleFromStorage = (\n options: Pick<\n LocaleStorageOptions,\n | 'getCookie'\n | 'getSessionStorage'\n | 'getLocaleStorage'\n | 'getHeader'\n | 'isCookieEnabled'\n >\n): Locale | undefined => {\n const { routing, internationalization } = configuration;\n const { locales } = internationalization;\n const { storage } = routing;\n\n // If storage is disabled, return undefined\n if (storage === false || options?.isCookieEnabled === false) return undefined;\n\n const storageAttributes = getStorageAttributes(storage);\n\n const isValidLocale = (value: string | null | undefined): value is Locale => {\n if (!value) return false;\n\n return locales.includes(value as Locale);\n };\n\n const readCookie = (name: string): string | undefined => {\n // Prefer provided getter (server or custom environment)\n try {\n const fromOption = options?.getCookie?.(name);\n\n if (fromOption !== null && fromOption !== undefined) return fromOption;\n } catch {}\n\n // Fallback to browser cookie parsing\n return getCookie(name);\n };\n\n // Check cookies first\n for (let i = 0; i < storageAttributes.cookies.length; i++) {\n const { name } = storageAttributes.cookies[i];\n\n const value = readCookie(name);\n\n if (isValidLocale(value)) return value;\n }\n\n // Then check localStorage candidates (browser only)\n for (let i = 0; i < storageAttributes.localStorage.length; i++) {\n const { name } = storageAttributes.localStorage[i];\n\n try {\n const value = options?.getLocaleStorage?.(name);\n\n if (isValidLocale(value)) return value;\n } catch {}\n }\n\n // Check sessionStorage candidates (browser only)\n for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {\n const { name } = storageAttributes.sessionStorage[i];\n\n try {\n const value = options?.getSessionStorage?.(name);\n\n if (isValidLocale(value)) return value;\n } catch {}\n }\n\n // Finally check header candidates (server only)\n for (let i = 0; i < storageAttributes.headers.length; i++) {\n const { name } = storageAttributes.headers[i];\n\n try {\n const value = options?.getHeader?.(name);\n\n if (isValidLocale(value)) return value;\n } catch {}\n }\n};\n\n/**\n * Stores the locale in various storage mechanisms (cookies, localStorage, sessionStorage, headers).\n * The function writes to all configured storage locations according to their attributes.\n * Respects overwrite flags for localStorage and sessionStorage.\n *\n * @param locale - The locale to store\n */\nexport const setLocaleInStorage = (\n locale: LocalesValues,\n options?: LocaleStorageOptions\n): void => {\n // If storage is disabled, do nothing\n if (\n configuration.routing.storage === false ||\n options?.isCookieEnabled === false\n )\n return;\n\n const storageAttributes = getStorageAttributes(configuration.routing.storage);\n\n // Write to cookies (server via setCookie, client via cookieStore/document)\n for (let i = 0; i < storageAttributes.cookies.length; i++) {\n const { name, attributes } = storageAttributes.cookies[i];\n\n try {\n if (options?.setCookieStore) {\n options?.setCookieStore?.(name, locale, {\n ...attributes,\n expires:\n attributes.expires instanceof Date\n ? attributes.expires.getTime()\n : attributes.expires,\n });\n }\n } catch {\n try {\n if (options?.setCookieString) {\n const cookieString = buildCookieString(name, locale, attributes);\n\n options?.setCookieString?.(name, cookieString);\n }\n } catch {}\n }\n }\n\n // Write to localStorage (browser only)\n if (options?.setLocaleStorage) {\n for (let i = 0; i < storageAttributes.localStorage.length; i++) {\n const { name } = storageAttributes.localStorage[i];\n\n try {\n const shouldOverwrite = options?.overwrite ?? true;\n\n if (!shouldOverwrite && options?.getLocaleStorage) {\n const existing = options?.getLocaleStorage?.(name);\n\n if (existing) continue;\n }\n options?.setLocaleStorage?.(name, locale);\n } catch {}\n }\n }\n\n // Write to sessionStorage (browser only)\n if (options?.setSessionStorage) {\n for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {\n const { name } = storageAttributes.sessionStorage[i];\n\n try {\n const shouldOverwrite = options?.overwrite ?? true;\n\n if (!shouldOverwrite && options?.getSessionStorage) {\n const existing = options?.getSessionStorage?.(name);\n if (existing) continue;\n }\n\n options?.setSessionStorage?.(name, locale);\n } catch {}\n }\n }\n\n // Write to headers (server only)\n if (options?.setHeader) {\n for (let i = 0; i < storageAttributes.headers.length; i++) {\n const { name } = storageAttributes.headers[i];\n\n try {\n options?.setHeader?.(name, locale);\n } catch {}\n }\n }\n};\n\n/**\n * Utility object to get and set the locale in the storage by considering the configuration\n *\n * @property getLocale - Retrieves the locale from various storage mechanisms (cookies, localStorage, sessionStorage, headers).\n * Retrieves the locale from various storage mechanisms (cookies, localStorage, sessionStorage, headers).\n * The function checks storage locations in order of priority as defined in the configuration.\n *\n * @property setLocale - Stores the locale in various storage mechanisms (cookies, localStorage, sessionStorage, headers).\n * The function writes to all configured storage locations according to their attributes.\n * Respects overwrite flags for localStorage and sessionStorage.\n *\n * @returns The locale if found in any storage, or undefined if not found\n */\nexport const LocaleStorage = (options: LocaleStorageOptions) => ({\n getLocale: () => getLocaleFromStorage(options),\n setLocale: (locale: LocalesValues) => setLocaleInStorage(locale, options),\n});\n"],"mappings":";;;;;;;;AAiE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AAI,SAAS,kBACX,MAAK,IAAI,IAAI,GAAG,IAAI,kBAAkB,eAAe,QAAQ,KAAK;EAChE,MAAM,EAAE,SAAS,kBAAkB,eAAe;AAElD,MAAI;AAGF,OAAI,EAFoB,SAAS,aAAa,SAEtB,SAAS,mBAE/B;QADiB,SAAS,oBAAoB,KAAK,CACrC;;AAGhB,YAAS,oBAAoB,MAAM,OAAO;UACpC;;AAKZ,KAAI,SAAS,UACX,MAAK,IAAI,IAAI,GAAG,IAAI,kBAAkB,QAAQ,QAAQ,KAAK;EACzD,MAAM,EAAE,SAAS,kBAAkB,QAAQ;AAE3C,MAAI;AACF,YAAS,YAAY,MAAM,OAAO;UAC5B;;;;;;;;;;;;;;;;AAkBd,MAAa,iBAAiB,aAAmC;CAC/D,iBAAiB,qBAAqB,QAAQ;CAC9C,YAAY,WAA0B,mBAAmB,QAAQ,QAAQ;CAC1E"}
|
|
1
|
+
{"version":3,"file":"localeStorage.cjs","names":["configuration","getCookie"],"sources":["../../../src/utils/localeStorage.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { CookiesAttributes } from '@intlayer/types/config';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport { getCookie } from './getCookie';\n\n// ============================================================================\n// Shared types\n// ============================================================================\n\nexport type CookieBuildAttributes = {\n domain?: string;\n path?: string;\n secure?: boolean;\n httpOnly?: boolean;\n sameSite?: 'strict' | 'lax' | 'none';\n /** Expiry as milliseconds since epoch (Date.getTime()) or number of days */\n expires?: number | undefined;\n};\n\n// ============================================================================\n// Shared helpers\n// ============================================================================\n\nconst buildCookieString = (\n name: string,\n value: string,\n attributes: Omit<CookiesAttributes, 'name' | 'type'>\n): string => {\n const encodedValue = encodeURIComponent(value);\n const parts: string[] = [`${name}=${encodedValue}`];\n\n if (attributes.path) parts.push(`Path=${attributes.path}`);\n if (attributes.domain) parts.push(`Domain=${attributes.domain}`);\n if (attributes.expires instanceof Date)\n parts.push(`Expires=${attributes.expires.toUTCString()}`);\n if (attributes.secure) parts.push('Secure');\n if (attributes.sameSite) parts.push(`SameSite=${attributes.sameSite}`);\n return parts.join('; ');\n};\n\n// ============================================================================\n// Client-specific types and functions\n// (cookies via browser APIs, localStorage, sessionStorage — no headers)\n// ============================================================================\n\nexport type LocaleStorageClientOptions = {\n overwrite?: boolean;\n isCookieEnabled?: boolean;\n setCookieStore?: (\n name: string,\n value: string,\n cookie: CookieBuildAttributes\n ) => void;\n setCookieString?: (name: string, cookie: string) => void;\n getCookie?: (name: string) => string | undefined | null;\n setSessionStorage?: (name: string, value: string) => void;\n getSessionStorage?: (name: string) => string | undefined | null;\n setLocaleStorage?: (name: string, value: string) => void;\n getLocaleStorage?: (name: string) => string | undefined | null;\n};\n\n/**\n * Retrieves the locale from browser storage mechanisms\n * (cookies, localStorage, sessionStorage).\n * Does not read from headers — use `getLocaleFromStorageServer` for that.\n */\nexport const getLocaleFromStorageClient = (\n options: LocaleStorageClientOptions\n): Locale | undefined => {\n const { routing, internationalization } = configuration;\n const { locales } = internationalization;\n const storageAttributes = routing.storage;\n\n if (options?.isCookieEnabled === false) return undefined;\n\n const isValidLocale = (value: string | null | undefined): value is Locale =>\n !!value && locales.includes(value as Locale);\n\n for (let i = 0; i < storageAttributes.cookies.length; i++) {\n try {\n const value = options?.getCookie?.(storageAttributes.cookies[i].name);\n if (isValidLocale(value)) return value;\n } catch {}\n }\n\n for (let i = 0; i < storageAttributes.localStorage.length; i++) {\n try {\n const value = options?.getLocaleStorage?.(\n storageAttributes.localStorage[i].name\n );\n if (isValidLocale(value)) return value;\n } catch {}\n }\n\n for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {\n try {\n const value = options?.getSessionStorage?.(\n storageAttributes.sessionStorage[i].name\n );\n if (isValidLocale(value)) return value;\n } catch {}\n }\n};\n\n/**\n * Stores the locale in browser storage mechanisms\n * (cookies, localStorage, sessionStorage).\n * Does not write to headers — use `setLocaleInStorageServer` for that.\n */\nexport const setLocaleInStorageClient = (\n locale: LocalesValues,\n options?: LocaleStorageClientOptions\n): void => {\n const { routing } = configuration;\n const storageAttributes = routing.storage;\n\n if (options?.isCookieEnabled === false) return;\n\n for (let i = 0; i < storageAttributes.cookies.length; i++) {\n const { name, attributes } = storageAttributes.cookies[i];\n try {\n if (options?.setCookieStore) {\n options.setCookieStore(name, locale, {\n ...attributes,\n expires:\n attributes.expires instanceof Date\n ? attributes.expires.getTime()\n : attributes.expires,\n });\n }\n } catch {\n try {\n if (options?.setCookieString) {\n options.setCookieString(\n name,\n buildCookieString(name, locale, attributes)\n );\n }\n } catch {}\n }\n }\n\n if (options?.setLocaleStorage) {\n for (let i = 0; i < storageAttributes.localStorage.length; i++) {\n const { name } = storageAttributes.localStorage[i];\n try {\n if (!(options?.overwrite ?? true) && options?.getLocaleStorage) {\n if (options.getLocaleStorage(name)) continue;\n }\n options.setLocaleStorage(name, locale);\n } catch {}\n }\n }\n\n if (options?.setSessionStorage) {\n for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {\n const { name } = storageAttributes.sessionStorage[i];\n try {\n if (!(options?.overwrite ?? true) && options?.getSessionStorage) {\n if (options.getSessionStorage(name)) continue;\n }\n options.setSessionStorage(name, locale);\n } catch {}\n }\n }\n};\n\n/**\n * Client-side locale storage utility.\n * Handles cookies (browser), localStorage and sessionStorage.\n * Does not access headers.\n *\n * @example\n * ```ts\n * const storage = LocaleStorageClient(localeStorageOptions);\n * const locale = storage.getLocale();\n * storage.setLocale('fr');\n * ```\n */\nexport const LocaleStorageClient = (options: LocaleStorageClientOptions) => ({\n getLocale: () => getLocaleFromStorageClient(options),\n setLocale: (locale: LocalesValues) =>\n setLocaleInStorageClient(locale, options),\n});\n\n// ============================================================================\n// Server-specific types and functions\n// (cookies via injected getter/setter, headers — no localStorage/sessionStorage)\n// ============================================================================\n\nexport type LocaleStorageServerOptions = {\n overwrite?: boolean;\n isCookieEnabled?: boolean;\n setCookieStore?: (\n name: string,\n value: string,\n cookie: CookieBuildAttributes\n ) => void;\n setCookieString?: (name: string, cookie: string) => void;\n getCookie?: (name: string) => string | undefined | null;\n getHeader?: (name: string) => string | undefined | null;\n setHeader?: (name: string, value: string) => void;\n};\n\n/**\n * Retrieves the locale from server-side storage mechanisms (cookies, headers).\n * Does not access localStorage or sessionStorage.\n * No browser cookie fallback — the caller must provide `getCookie`.\n */\nexport const getLocaleFromStorageServer = (\n options: LocaleStorageServerOptions\n): Locale | undefined => {\n const { routing, internationalization } = configuration;\n const { locales } = internationalization;\n const storageAttributes = routing.storage;\n\n if (options?.isCookieEnabled === false) return undefined;\n\n const isValidLocale = (value: string | null | undefined): value is Locale =>\n !!value && locales.includes(value as Locale);\n\n for (let i = 0; i < storageAttributes.cookies.length; i++) {\n try {\n const value = options?.getCookie?.(storageAttributes.cookies[i].name);\n if (isValidLocale(value)) return value;\n } catch {}\n }\n\n for (let i = 0; i < storageAttributes.headers.length; i++) {\n try {\n const value = options?.getHeader?.(storageAttributes.headers[i].name);\n if (isValidLocale(value)) return value;\n } catch {}\n }\n};\n\n/**\n * Stores the locale in server-side storage mechanisms (cookies, headers).\n * Does not write to localStorage or sessionStorage.\n */\nexport const setLocaleInStorageServer = (\n locale: LocalesValues,\n options?: LocaleStorageServerOptions\n): void => {\n const { routing } = configuration;\n const storageAttributes = routing.storage;\n\n if (options?.isCookieEnabled === false) return;\n\n for (let i = 0; i < storageAttributes.cookies.length; i++) {\n const { name, attributes } = storageAttributes.cookies[i];\n try {\n if (options?.setCookieStore) {\n options.setCookieStore(name, locale, {\n ...attributes,\n expires:\n attributes.expires instanceof Date\n ? attributes.expires.getTime()\n : attributes.expires,\n });\n }\n } catch {\n try {\n if (options?.setCookieString) {\n options.setCookieString(\n name,\n buildCookieString(name, locale, attributes)\n );\n }\n } catch {}\n }\n }\n\n if (options?.setHeader) {\n for (let i = 0; i < storageAttributes.headers.length; i++) {\n try {\n options.setHeader(storageAttributes.headers[i].name, locale);\n } catch {}\n }\n }\n};\n\n/**\n * Server-side locale storage utility.\n * Handles cookies (via injected getter/setter) and headers.\n * Does not access localStorage or sessionStorage.\n *\n * @example\n * ```ts\n * const storage = LocaleStorageServer({\n * getCookie: (name) => req.cookies[name],\n * setCookieStore: (name, value, attrs) => res.cookie(name, value, attrs),\n * getHeader: (name) => req.headers[name],\n * setHeader: (name, value) => res.setHeader(name, value),\n * });\n * const locale = storage.getLocale();\n * storage.setLocale('fr');\n * ```\n */\nexport const LocaleStorageServer = (options: LocaleStorageServerOptions) => ({\n getLocale: () => getLocaleFromStorageServer(options),\n setLocale: (locale: LocalesValues) =>\n setLocaleInStorageServer(locale, options),\n});\n\n// ============================================================================\n// Deprecated: combined LocaleStorage\n// Use LocaleStorageClient or LocaleStorageServer instead\n// ============================================================================\n\n/**\n * @deprecated Use {@link LocaleStorageClientOptions} or {@link LocaleStorageServerOptions} instead.\n */\nexport type LocaleStorageOptions = LocaleStorageClientOptions &\n LocaleStorageServerOptions;\n\n/**\n * Retrieves the locale from all storage mechanisms\n * (cookies, localStorage, sessionStorage, headers).\n *\n * @deprecated Use {@link getLocaleFromStorageClient} (browser) or\n * {@link getLocaleFromStorageServer} (server) instead.\n */\nexport const getLocaleFromStorage = (\n options: Pick<\n LocaleStorageOptions,\n | 'getCookie'\n | 'getSessionStorage'\n | 'getLocaleStorage'\n | 'getHeader'\n | 'isCookieEnabled'\n >\n): Locale | undefined => {\n const { routing, internationalization } = configuration;\n const { locales } = internationalization;\n const storageAttributes = routing.storage;\n\n if (options?.isCookieEnabled === false) return undefined;\n\n const isValidLocale = (value: string | null | undefined): value is Locale =>\n !!value && locales.includes(value as Locale);\n\n const readCookie = (name: string): string | undefined => {\n try {\n const fromOption = options?.getCookie?.(name);\n if (fromOption !== null && fromOption !== undefined) return fromOption;\n } catch {}\n // Browser fallback kept for backward compatibility\n return getCookie(name);\n };\n\n for (let i = 0; i < storageAttributes.cookies.length; i++) {\n const value = readCookie(storageAttributes.cookies[i].name);\n if (isValidLocale(value)) return value;\n }\n\n for (let i = 0; i < storageAttributes.localStorage.length; i++) {\n try {\n const value = options?.getLocaleStorage?.(\n storageAttributes.localStorage[i].name\n );\n if (isValidLocale(value)) return value;\n } catch {}\n }\n\n for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {\n try {\n const value = options?.getSessionStorage?.(\n storageAttributes.sessionStorage[i].name\n );\n if (isValidLocale(value)) return value;\n } catch {}\n }\n\n for (let i = 0; i < storageAttributes.headers.length; i++) {\n try {\n const value = options?.getHeader?.(storageAttributes.headers[i].name);\n if (isValidLocale(value)) return value;\n } catch {}\n }\n};\n\n/**\n * Stores the locale in all configured storage mechanisms\n * (cookies, localStorage, sessionStorage, headers).\n *\n * @deprecated Use {@link setLocaleInStorageClient} (browser) or\n * {@link setLocaleInStorageServer} (server) instead.\n */\nexport const setLocaleInStorage = (\n locale: LocalesValues,\n options?: LocaleStorageOptions\n): void => {\n const { routing } = configuration;\n const storageAttributes = routing.storage;\n\n if (options?.isCookieEnabled === false) return;\n\n for (let i = 0; i < storageAttributes.cookies.length; i++) {\n const { name, attributes } = storageAttributes.cookies[i];\n try {\n if (options?.setCookieStore) {\n options.setCookieStore(name, locale, {\n ...attributes,\n expires:\n attributes.expires instanceof Date\n ? attributes.expires.getTime()\n : attributes.expires,\n });\n }\n } catch {\n try {\n if (options?.setCookieString) {\n options.setCookieString(\n name,\n buildCookieString(name, locale, attributes)\n );\n }\n } catch {}\n }\n }\n\n if (options?.setLocaleStorage) {\n for (let i = 0; i < storageAttributes.localStorage.length; i++) {\n const { name } = storageAttributes.localStorage[i];\n try {\n if (!(options?.overwrite ?? true) && options?.getLocaleStorage) {\n if (options.getLocaleStorage(name)) continue;\n }\n options.setLocaleStorage(name, locale);\n } catch {}\n }\n }\n\n if (options?.setSessionStorage) {\n for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {\n const { name } = storageAttributes.sessionStorage[i];\n try {\n if (!(options?.overwrite ?? true) && options?.getSessionStorage) {\n if (options.getSessionStorage(name)) continue;\n }\n options.setSessionStorage(name, locale);\n } catch {}\n }\n }\n\n if (options?.setHeader) {\n for (let i = 0; i < storageAttributes.headers.length; i++) {\n try {\n options.setHeader(storageAttributes.headers[i].name, locale);\n } catch {}\n }\n }\n};\n\n/**\n * Utility object to get and set the locale in storage based on configuration.\n *\n * @deprecated Use {@link LocaleStorageClient} (browser) or\n * {@link LocaleStorageServer} (server) instead.\n */\nexport const LocaleStorage = (options: LocaleStorageOptions) => ({\n getLocale: () => getLocaleFromStorage(options),\n setLocale: (locale: LocalesValues) => setLocaleInStorage(locale, options),\n});\n"],"mappings":";;;;;;;AAwBA,MAAM,qBACJ,MACA,OACA,eACW;CAEX,MAAM,QAAkB,CAAC,GAAG,KAAK,GADZ,mBAAmB,MAAM,GACK;AAEnD,KAAI,WAAW,KAAM,OAAM,KAAK,QAAQ,WAAW,OAAO;AAC1D,KAAI,WAAW,OAAQ,OAAM,KAAK,UAAU,WAAW,SAAS;AAChE,KAAI,WAAW,mBAAmB,KAChC,OAAM,KAAK,WAAW,WAAW,QAAQ,aAAa,GAAG;AAC3D,KAAI,WAAW,OAAQ,OAAM,KAAK,SAAS;AAC3C,KAAI,WAAW,SAAU,OAAM,KAAK,YAAY,WAAW,WAAW;AACtE,QAAO,MAAM,KAAK,KAAK;;;;;;;AA6BzB,MAAa,8BACX,YACuB;CACvB,MAAM,EAAE,SAAS,yBAAyBA;CAC1C,MAAM,EAAE,YAAY;CACpB,MAAM,oBAAoB,QAAQ;AAElC,KAAI,SAAS,oBAAoB,MAAO,QAAO;CAE/C,MAAM,iBAAiB,UACrB,CAAC,CAAC,SAAS,QAAQ,SAAS,MAAgB;AAE9C,MAAK,IAAI,IAAI,GAAG,IAAI,kBAAkB,QAAQ,QAAQ,IACpD,KAAI;EACF,MAAM,QAAQ,SAAS,YAAY,kBAAkB,QAAQ,GAAG,KAAK;AACrE,MAAI,cAAc,MAAM,CAAE,QAAO;SAC3B;AAGV,MAAK,IAAI,IAAI,GAAG,IAAI,kBAAkB,aAAa,Q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tD;;;;;;;;AAWd,MAAa,iBAAiB,aAAmC;CAC/D,iBAAiB,qBAAqB,QAAQ;CAC9C,YAAY,WAA0B,mBAAmB,QAAQ,QAAQ;CAC1E"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getCachedIntl } from "../utils/intl.mjs";
|
|
2
2
|
import configuration from "@intlayer/config/built";
|
|
3
3
|
|
|
4
4
|
//#region src/formatters/compact.ts
|
|
@@ -13,7 +13,7 @@ import configuration from "@intlayer/config/built";
|
|
|
13
13
|
* compact("1000000", { locale: Locales.FRENCH, compactDisplay: "long" });
|
|
14
14
|
* // "1 million"
|
|
15
15
|
*/
|
|
16
|
-
const compact = (value, options) =>
|
|
16
|
+
const compact = (value, options) => getCachedIntl(Intl.NumberFormat, options?.locale ?? configuration?.internationalization?.defaultLocale, {
|
|
17
17
|
...options,
|
|
18
18
|
notation: "compact"
|
|
19
19
|
}).format(Number(value));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compact.mjs","names":[],"sources":["../../../src/formatters/compact.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport {
|
|
1
|
+
{"version":3,"file":"compact.mjs","names":[],"sources":["../../../src/formatters/compact.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport { getCachedIntl } from '../utils/intl';\n\n/**\n * Formats a numeric value using compact notation (e.g., 1K, 1M, 1B)\n * based on locale and formatting options.\n *\n * @example\n * compact(1200); // \"1.2K\"\n *\n * @example\n * compact(\"1000000\", { locale: Locales.FRENCH, compactDisplay: \"long\" });\n * // \"1 million\"\n */\nexport const compact = (\n value: string | number,\n options?: Intl.NumberFormatOptions & { locale?: LocalesValues }\n): string =>\n getCachedIntl(\n Intl.NumberFormat,\n options?.locale ?? configuration?.internationalization?.defaultLocale,\n {\n ...options,\n notation: 'compact',\n }\n ).format(Number(value));\n"],"mappings":";;;;;;;;;;;;;;;AAeA,MAAa,WACX,OACA,YAEA,cACE,KAAK,cACL,SAAS,UAAU,eAAe,sBAAsB,eACxD;CACE,GAAG;CACH,UAAU;CACX,CACF,CAAC,OAAO,OAAO,MAAM,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getCachedIntl } from "../utils/intl.mjs";
|
|
2
2
|
import configuration from "@intlayer/config/built";
|
|
3
3
|
|
|
4
4
|
//#region src/formatters/currency.ts
|
|
@@ -13,7 +13,7 @@ import configuration from "@intlayer/config/built";
|
|
|
13
13
|
* currency("5000", { locale: Locales.FRENCH, currency: "CAD", currencyDisplay: "code" });
|
|
14
14
|
* // "5 000,00 CAD"
|
|
15
15
|
*/
|
|
16
|
-
const currency = (value, options) =>
|
|
16
|
+
const currency = (value, options) => getCachedIntl(Intl.NumberFormat, options?.locale ?? configuration?.internationalization?.defaultLocale, {
|
|
17
17
|
style: "currency",
|
|
18
18
|
currency: options?.currency ?? "USD",
|
|
19
19
|
currencyDisplay: options?.currencyDisplay ?? "symbol",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"currency.mjs","names":[],"sources":["../../../src/formatters/currency.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport {
|
|
1
|
+
{"version":3,"file":"currency.mjs","names":[],"sources":["../../../src/formatters/currency.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport { getCachedIntl } from '../utils/intl';\n\n/**\n * Formats a numeric or string value into a localized currency string using the Intl API.\n *\n * @example\n * currency(1234.5, { currency: 'EUR' });\n * // \"€1,234.50\"\n *\n * @example\n * currency(\"5000\", { locale: Locales.FRENCH, currency: \"CAD\", currencyDisplay: \"code\" });\n * // \"5 000,00 CAD\"\n */\nexport const currency = (\n value: string | number,\n options?: Intl.NumberFormatOptions & { locale?: LocalesValues }\n): string =>\n getCachedIntl(\n Intl.NumberFormat,\n options?.locale ?? configuration?.internationalization?.defaultLocale,\n {\n style: 'currency',\n currency: options?.currency ?? 'USD',\n currencyDisplay: options?.currencyDisplay ?? 'symbol',\n minimumFractionDigits: options?.minimumFractionDigits ?? 2,\n maximumFractionDigits: options?.maximumFractionDigits ?? 2,\n ...options,\n }\n ).format(Number(value));\n"],"mappings":";;;;;;;;;;;;;;;AAeA,MAAa,YACX,OACA,YAEA,cACE,KAAK,cACL,SAAS,UAAU,eAAe,sBAAsB,eACxD;CACE,OAAO;CACP,UAAU,SAAS,YAAY;CAC/B,iBAAiB,SAAS,mBAAmB;CAC7C,uBAAuB,SAAS,yBAAyB;CACzD,uBAAuB,SAAS,yBAAyB;CACzD,GAAG;CACJ,CACF,CAAC,OAAO,OAAO,MAAM,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getCachedIntl } from "../utils/intl.mjs";
|
|
2
2
|
import configuration from "@intlayer/config/built";
|
|
3
3
|
|
|
4
4
|
//#region src/formatters/date.ts
|
|
@@ -51,7 +51,7 @@ const date = (date, options) => {
|
|
|
51
51
|
const dateTime = new Date(date);
|
|
52
52
|
const resolvedOptions = typeof options === "string" ? presets[options] ?? {} : options;
|
|
53
53
|
const locale = (typeof options === "object" ? options?.locale : void 0) ?? configuration?.internationalization?.defaultLocale;
|
|
54
|
-
return
|
|
54
|
+
return getCachedIntl(Intl.DateTimeFormat, locale, resolvedOptions).format(dateTime);
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"date.mjs","names":[],"sources":["../../../src/formatters/date.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport {
|
|
1
|
+
{"version":3,"file":"date.mjs","names":[],"sources":["../../../src/formatters/date.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport { getCachedIntl } from '../utils/intl';\n\nexport type DateTimePreset =\n | 'short'\n | 'long'\n | 'dateOnly'\n | 'timeOnly'\n | 'full';\n\nexport const presets: Record<DateTimePreset, Intl.DateTimeFormatOptions> = {\n short: {\n year: '2-digit',\n month: '2-digit',\n day: '2-digit',\n hour: '2-digit',\n minute: '2-digit',\n },\n long: {\n year: 'numeric',\n month: 'long',\n day: 'numeric',\n hour: 'numeric',\n minute: 'numeric',\n },\n full: {\n year: 'numeric',\n month: 'long',\n day: 'numeric',\n hour: 'numeric',\n minute: 'numeric',\n hour12: false,\n },\n dateOnly: {\n year: 'numeric',\n month: 'short',\n day: 'numeric',\n },\n timeOnly: {\n hour: 'numeric',\n minute: 'numeric',\n second: 'numeric',\n },\n};\n\n/**\n * Formats a date/time value into a localized string using Intl.DateTimeFormat.\n *\n * @example\n * date(new Date('2025-08-02T14:30:00Z'), { year: '2-digit', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' });\n * // \"08/02/25, 14:30\"\n *\n * @example\n * date(\"2025-08-02T14:30:00Z\", { locale: Locales.FRENCH, month: \"long\", day: \"numeric\" });\n * // \"2 août\"\n */\nexport const date = (\n date: Date | string | number,\n options?:\n | (Intl.DateTimeFormatOptions & { locale?: LocalesValues })\n | DateTimePreset\n): string => {\n const dateTime = new Date(date);\n\n const resolvedOptions =\n typeof options === 'string' ? (presets[options] ?? {}) : options;\n\n const locale =\n (typeof options === 'object' ? options?.locale : undefined) ??\n configuration?.internationalization?.defaultLocale;\n\n const formatter = getCachedIntl(Intl.DateTimeFormat, locale, resolvedOptions);\n\n return formatter.format(dateTime);\n};\n"],"mappings":";;;;AAWA,MAAa,UAA8D;CACzE,OAAO;EACL,MAAM;EACN,OAAO;EACP,KAAK;EACL,MAAM;EACN,QAAQ;EACT;CACD,MAAM;EACJ,MAAM;EACN,OAAO;EACP,KAAK;EACL,MAAM;EACN,QAAQ;EACT;CACD,MAAM;EACJ,MAAM;EACN,OAAO;EACP,KAAK;EACL,MAAM;EACN,QAAQ;EACR,QAAQ;EACT;CACD,UAAU;EACR,MAAM;EACN,OAAO;EACP,KAAK;EACN;CACD,UAAU;EACR,MAAM;EACN,QAAQ;EACR,QAAQ;EACT;CACF;;;;;;;;;;;;AAaD,MAAa,QACX,MACA,YAGW;CACX,MAAM,WAAW,IAAI,KAAK,KAAK;CAE/B,MAAM,kBACJ,OAAO,YAAY,WAAY,QAAQ,YAAY,EAAE,GAAI;CAE3D,MAAM,UACH,OAAO,YAAY,WAAW,SAAS,SAAS,WACjD,eAAe,sBAAsB;AAIvC,QAFkB,cAAc,KAAK,gBAAgB,QAAQ,gBAAgB,CAE5D,OAAO,SAAS"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CachedIntl, bindIntl,
|
|
1
|
+
import { CachedIntl, bindIntl, getCachedIntl } from "../utils/intl.mjs";
|
|
2
2
|
import { compact } from "./compact.mjs";
|
|
3
3
|
import { currency } from "./currency.mjs";
|
|
4
4
|
import { date, presets } from "./date.mjs";
|
|
@@ -8,4 +8,4 @@ import { percentage } from "./percentage.mjs";
|
|
|
8
8
|
import { relativeTime } from "./relativeTime.mjs";
|
|
9
9
|
import { units } from "./units.mjs";
|
|
10
10
|
|
|
11
|
-
export { CachedIntl, CachedIntl as Intl, bindIntl, compact,
|
|
11
|
+
export { CachedIntl, CachedIntl as Intl, bindIntl, compact, currency, date, getCachedIntl, list, number, percentage, presets, relativeTime, units };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getCachedIntl } from "../utils/intl.mjs";
|
|
2
2
|
import configuration from "@intlayer/config/built";
|
|
3
3
|
|
|
4
4
|
//#region src/formatters/list.ts
|
|
@@ -17,7 +17,7 @@ import configuration from "@intlayer/config/built";
|
|
|
17
17
|
* list([1, 2, 3], { type: 'unit' });
|
|
18
18
|
* // "1, 2, 3"
|
|
19
19
|
*/
|
|
20
|
-
const list = (values, options) =>
|
|
20
|
+
const list = (values, options) => getCachedIntl(Intl.ListFormat, options?.locale ?? configuration?.internationalization?.defaultLocale, {
|
|
21
21
|
type: options?.type ?? "conjunction",
|
|
22
22
|
style: options?.style ?? "long",
|
|
23
23
|
...options
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"list.mjs","names":[],"sources":["../../../src/formatters/list.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport {
|
|
1
|
+
{"version":3,"file":"list.mjs","names":[],"sources":["../../../src/formatters/list.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport { getCachedIntl } from '../utils/intl';\n\n/** Locally defined subset of Intl.ListFormatOptions so consumers don't need ES2021.Intl in their lib. */\ntype ListFormatOptions = {\n localeMatcher?: 'lookup' | 'best fit';\n type?: 'conjunction' | 'disjunction' | 'unit';\n style?: 'long' | 'short' | 'narrow';\n};\n\n/**\n * Formats an array of values into a localized list string using the Intl API.\n *\n * @example\n * list(['apple', 'banana', 'orange']);\n * // \"apple, banana, and orange\"\n *\n * @example\n * list(['red', 'green', 'blue'], { locale: Locales.FRENCH, type: 'disjunction' });\n * // \"rouge, vert ou bleu\"\n *\n * @example\n * list([1, 2, 3], { type: 'unit' });\n * // \"1, 2, 3\"\n */\nexport const list = (\n values: (string | number)[],\n options?: ListFormatOptions & { locale?: LocalesValues }\n): string =>\n getCachedIntl(\n (Intl as any).ListFormat,\n options?.locale ?? configuration?.internationalization?.defaultLocale,\n {\n type: options?.type ?? 'conjunction',\n style: options?.style ?? 'long',\n ...options,\n }\n ).format(values.map(String));\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA0BA,MAAa,QACX,QACA,YAEA,cACG,KAAa,YACd,SAAS,UAAU,eAAe,sBAAsB,eACxD;CACE,MAAM,SAAS,QAAQ;CACvB,OAAO,SAAS,SAAS;CACzB,GAAG;CACJ,CACF,CAAC,OAAO,OAAO,IAAI,OAAO,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getCachedIntl } from "../utils/intl.mjs";
|
|
2
2
|
import configuration from "@intlayer/config/built";
|
|
3
3
|
|
|
4
4
|
//#region src/formatters/number.ts
|
|
@@ -11,7 +11,7 @@ import configuration from "@intlayer/config/built";
|
|
|
11
11
|
* @example
|
|
12
12
|
* number("1000000", { locale: Locales.FRENCH }); // "1 000 000"
|
|
13
13
|
*/
|
|
14
|
-
const number = (value, options) =>
|
|
14
|
+
const number = (value, { locale, ...options } = {}) => getCachedIntl(Intl.NumberFormat, locale ?? configuration?.internationalization?.defaultLocale, options).format(Number(value));
|
|
15
15
|
|
|
16
16
|
//#endregion
|
|
17
17
|
export { number };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number.mjs","names":[],"sources":["../../../src/formatters/number.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport {
|
|
1
|
+
{"version":3,"file":"number.mjs","names":[],"sources":["../../../src/formatters/number.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport { getCachedIntl } from '../utils/intl';\n\n/**\n * Formats a numeric value using locale-aware formatting.\n *\n * @example\n * number(123456.789); // \"123,456.789\"\n *\n * @example\n * number(\"1000000\", { locale: Locales.FRENCH }); // \"1 000 000\"\n */\nexport const number = (\n value: string | number,\n {\n locale,\n ...options\n }: Intl.NumberFormatOptions & { locale?: LocalesValues } = {}\n): string =>\n getCachedIntl(\n Intl.NumberFormat,\n locale ?? configuration?.internationalization?.defaultLocale,\n options\n ).format(Number(value));\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAa,UACX,OACA,EACE,QACA,GAAG,YACsD,EAAE,KAE7D,cACE,KAAK,cACL,UAAU,eAAe,sBAAsB,eAC/C,QACD,CAAC,OAAO,OAAO,MAAM,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getCachedIntl } from "../utils/intl.mjs";
|
|
2
2
|
import configuration from "@intlayer/config/built";
|
|
3
3
|
|
|
4
4
|
//#region src/formatters/percentage.ts
|
|
@@ -11,10 +11,10 @@ import configuration from "@intlayer/config/built";
|
|
|
11
11
|
* @example
|
|
12
12
|
* percentage(0.25, { minimumFractionDigits: 2 }); // "25.00%"
|
|
13
13
|
*/
|
|
14
|
-
const percentage = (value, options) => {
|
|
14
|
+
const percentage = (value, { locale, ...options } = {}) => {
|
|
15
15
|
let numericValue = Number(value);
|
|
16
16
|
if (numericValue > 1) numericValue /= 100;
|
|
17
|
-
return
|
|
17
|
+
return getCachedIntl(Intl.NumberFormat, locale ?? configuration?.internationalization?.defaultLocale, {
|
|
18
18
|
style: "percent",
|
|
19
19
|
...options
|
|
20
20
|
}).format(Number(numericValue));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"percentage.mjs","names":[],"sources":["../../../src/formatters/percentage.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport {
|
|
1
|
+
{"version":3,"file":"percentage.mjs","names":[],"sources":["../../../src/formatters/percentage.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport { getCachedIntl } from '../utils/intl';\n\n/**\n * Formats a number as a percentage string (e.g., 0.25 → \"25%\").\n *\n * @example\n * percentage(0.25); // \"25%\"\n *\n * @example\n * percentage(0.25, { minimumFractionDigits: 2 }); // \"25.00%\"\n */\nexport const percentage = (\n value: string | number,\n {\n locale,\n ...options\n }: Intl.NumberFormatOptions & { locale?: LocalesValues } = {}\n): string => {\n let numericValue = Number(value);\n\n // Normalize: if user passes 10, treat it as 10% instead of 1000%\n if (numericValue > 1) {\n numericValue /= 100;\n }\n\n const formatter = getCachedIntl(\n Intl.NumberFormat,\n locale ?? configuration?.internationalization?.defaultLocale,\n {\n style: 'percent',\n ...options,\n }\n );\n\n return formatter.format(Number(numericValue));\n};\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAa,cACX,OACA,EACE,QACA,GAAG,YACsD,EAAE,KAClD;CACX,IAAI,eAAe,OAAO,MAAM;AAGhC,KAAI,eAAe,EACjB,iBAAgB;AAYlB,QATkB,cAChB,KAAK,cACL,UAAU,eAAe,sBAAsB,eAC/C;EACE,OAAO;EACP,GAAG;EACJ,CACF,CAEgB,OAAO,OAAO,aAAa,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getCachedIntl } from "../utils/intl.mjs";
|
|
2
2
|
import configuration from "@intlayer/config/built";
|
|
3
3
|
|
|
4
4
|
//#region src/formatters/relativeTime.ts
|
|
@@ -33,7 +33,7 @@ const relativeTime = (from, to = /* @__PURE__ */ new Date(), options) => {
|
|
|
33
33
|
const toDate = new Date(to);
|
|
34
34
|
const unit = options?.unit ?? "second";
|
|
35
35
|
const value = diffInUnit(fromDate, toDate, unit);
|
|
36
|
-
return
|
|
36
|
+
return getCachedIntl(Intl.RelativeTimeFormat, options?.locale ?? configuration?.internationalization?.defaultLocale, options).format(Math.round(value), unit);
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relativeTime.mjs","names":[],"sources":["../../../src/formatters/relativeTime.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport {
|
|
1
|
+
{"version":3,"file":"relativeTime.mjs","names":[],"sources":["../../../src/formatters/relativeTime.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport { getCachedIntl } from '../utils/intl';\n\ntype RelativeTimeUnit = Intl.RelativeTimeFormatUnit;\n\n/**\n * Calculate the difference between 2 dates in the given unit.\n */\nconst diffInUnit = (from: Date, to: Date, unit: RelativeTimeUnit): number => {\n const msDiff = to.getTime() - from.getTime();\n const sec = msDiff / 1000;\n\n switch (unit) {\n case 'second':\n return sec;\n case 'minute':\n return sec / 60;\n case 'hour':\n return sec / 3600;\n case 'day':\n return sec / 86400;\n case 'month':\n return sec / (30 * 86400); // approx\n case 'quarter':\n return sec / (3 * 30 * 86400); // 3 months approx\n case 'year':\n return sec / (365 * 86400); // approx\n default:\n return sec;\n }\n};\n\n/**\n * Formats the difference between two dates as a localized relative time string.\n *\n * @example\n * relativeTime(new Date(Date.now() - 30000)); // \"30 seconds ago\"\n *\n * @example\n * relativeTime(\"2025-01-01\", new Date(), { locale: Locales.FRENCH, unit: \"day\" });\n * // \"il y a 443 jours\"\n */\nexport const relativeTime = (\n from: Date | string | number,\n to: Date | string | number = new Date(),\n options?: Intl.RelativeTimeFormatOptions & {\n locale?: LocalesValues;\n unit?: RelativeTimeUnit;\n }\n): string => {\n const fromDate = new Date(from);\n const toDate = new Date(to);\n const unit = options?.unit ?? 'second';\n\n const value = diffInUnit(fromDate, toDate, unit);\n\n return getCachedIntl(\n Intl.RelativeTimeFormat,\n options?.locale ?? configuration?.internationalization?.defaultLocale,\n options\n ).format(Math.round(value), unit);\n};\n"],"mappings":";;;;;;;AASA,MAAM,cAAc,MAAY,IAAU,SAAmC;CAE3E,MAAM,OADS,GAAG,SAAS,GAAG,KAAK,SAAS,IACvB;AAErB,SAAQ,MAAR;EACE,KAAK,SACH,QAAO;EACT,KAAK,SACH,QAAO,MAAM;EACf,KAAK,OACH,QAAO,MAAM;EACf,KAAK,MACH,QAAO,MAAM;EACf,KAAK,QACH,QAAO,OAAO,KAAK;EACrB,KAAK,UACH,QAAO,OAAO,KAAS;EACzB,KAAK,OACH,QAAO,OAAO,MAAM;EACtB,QACE,QAAO;;;;;;;;;;;;;AAcb,MAAa,gBACX,MACA,qBAA6B,IAAI,MAAM,EACvC,YAIW;CACX,MAAM,WAAW,IAAI,KAAK,KAAK;CAC/B,MAAM,SAAS,IAAI,KAAK,GAAG;CAC3B,MAAM,OAAO,SAAS,QAAQ;CAE9B,MAAM,QAAQ,WAAW,UAAU,QAAQ,KAAK;AAEhD,QAAO,cACL,KAAK,oBACL,SAAS,UAAU,eAAe,sBAAsB,eACxD,QACD,CAAC,OAAO,KAAK,MAAM,MAAM,EAAE,KAAK"}
|