intor 2.2.4 → 2.2.6
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/config/index.cjs +12 -10
- package/dist/config/index.js +12 -10
- package/dist/index.cjs +21 -583
- package/dist/index.d.cts +28 -260
- package/dist/index.d.ts +28 -260
- package/dist/index.js +20 -574
- package/dist/next/index.cjs +255 -265
- package/dist/next/index.d.cts +102 -92
- package/dist/next/index.d.ts +102 -92
- package/dist/next/index.js +254 -263
- package/dist/next/server/index.cjs +13 -13
- package/dist/next/server/index.js +13 -13
- package/dist/react/index.cjs +650 -0
- package/dist/react/index.d.cts +213 -0
- package/dist/react/index.d.ts +213 -0
- package/dist/react/index.js +622 -0
- package/dist/server/index.cjs +698 -0
- package/dist/server/index.d.cts +372 -0
- package/dist/server/index.d.ts +372 -0
- package/dist/server/index.js +682 -0
- package/package.json +21 -11
package/dist/next/index.js
CHANGED
|
@@ -1,100 +1,18 @@
|
|
|
1
|
-
import * as React6 from 'react';
|
|
2
|
-
import { logry } from 'logry';
|
|
3
|
-
import Keyv from 'keyv';
|
|
4
|
-
import merge from 'lodash.merge';
|
|
5
|
-
import { Translator } from 'intor-translator';
|
|
6
1
|
import { formatUrl } from 'next/dist/shared/lib/router/utils/format-url';
|
|
7
2
|
import NextLink from 'next/link';
|
|
3
|
+
import * as React6 from 'react';
|
|
8
4
|
import { usePathname as usePathname$1, useRouter as useRouter$1, redirect as redirect$1 } from 'next/navigation';
|
|
5
|
+
import merge from 'lodash.merge';
|
|
6
|
+
import { jsx } from 'react/jsx-runtime';
|
|
7
|
+
import { logry } from 'logry';
|
|
8
|
+
import Keyv from 'keyv';
|
|
9
9
|
import { cookies, headers } from 'next/headers';
|
|
10
|
+
import { Translator } from 'intor-translator';
|
|
10
11
|
|
|
11
|
-
// src/adapters/next/
|
|
12
|
-
var ConfigContext = React6.createContext(void 0);
|
|
13
|
-
|
|
14
|
-
// src/adapters/next/contexts/config/provider.tsx
|
|
15
|
-
function ConfigProvider({
|
|
16
|
-
value: { config, pathname },
|
|
17
|
-
children
|
|
18
|
-
}) {
|
|
19
|
-
const value = React6.useMemo(() => ({ config, pathname }), [config, pathname]);
|
|
20
|
-
return /* @__PURE__ */ React6.createElement(ConfigContext.Provider, { value }, children);
|
|
21
|
-
}
|
|
22
|
-
function useConfig() {
|
|
23
|
-
const context = React6.useContext(ConfigContext);
|
|
24
|
-
if (!context) throw new Error("useConfig must be used within ConfigProvider");
|
|
25
|
-
return context;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// src/modules/config/constants/cache.constants.ts
|
|
29
|
-
var DEFAULT_CACHE_OPTIONS = {
|
|
30
|
-
enabled: process.env.NODE_ENV === "production",
|
|
31
|
-
ttl: 60 * 60 * 1e3
|
|
32
|
-
// 1 hour
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
// src/shared/logger/global-logger-pool.ts
|
|
36
|
-
function getGlobalLoggerPool() {
|
|
37
|
-
if (!globalThis.__INTOR_LOGGER_POOL__) {
|
|
38
|
-
globalThis.__INTOR_LOGGER_POOL__ = /* @__PURE__ */ new Map();
|
|
39
|
-
}
|
|
40
|
-
return globalThis.__INTOR_LOGGER_POOL__;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// src/shared/logger/get-logger.ts
|
|
44
|
-
var DEFAULT_FORMATTER_CONFIG = {
|
|
45
|
-
node: { meta: { compact: true }, lineBreaksAfter: 1 }
|
|
46
|
-
};
|
|
47
|
-
function getLogger({
|
|
48
|
-
id = "default",
|
|
49
|
-
formatterConfig,
|
|
50
|
-
preset,
|
|
51
|
-
...options
|
|
52
|
-
}) {
|
|
53
|
-
const pool = getGlobalLoggerPool();
|
|
54
|
-
let logger = pool.get(id);
|
|
55
|
-
const useDefault = !formatterConfig && !preset;
|
|
56
|
-
if (!logger) {
|
|
57
|
-
logger = logry({
|
|
58
|
-
id,
|
|
59
|
-
formatterConfig: useDefault ? DEFAULT_FORMATTER_CONFIG : formatterConfig,
|
|
60
|
-
preset,
|
|
61
|
-
...options
|
|
62
|
-
});
|
|
63
|
-
pool.set(id, logger);
|
|
64
|
-
if (pool.size > 1e3) {
|
|
65
|
-
const keys = [...pool.keys()];
|
|
66
|
-
for (const key of keys.slice(0, 200)) pool.delete(key);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return logger;
|
|
70
|
-
}
|
|
12
|
+
// src/adapters/next/navigation/link.tsx
|
|
71
13
|
|
|
72
|
-
// src/
|
|
73
|
-
|
|
74
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
75
|
-
}
|
|
76
|
-
function isValidMessages(value) {
|
|
77
|
-
if (!isPlainObject(value)) return false;
|
|
78
|
-
const stack = [value];
|
|
79
|
-
while (stack.length > 0) {
|
|
80
|
-
const current = stack.pop();
|
|
81
|
-
for (const v of Object.values(current)) {
|
|
82
|
-
if (typeof v === "string") continue;
|
|
83
|
-
if (isPlainObject(v)) {
|
|
84
|
-
stack.push(v);
|
|
85
|
-
} else {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return true;
|
|
91
|
-
}
|
|
92
|
-
function getGlobalMessagesPool() {
|
|
93
|
-
if (!globalThis.__INTOR_MESSAGES_POOL__) {
|
|
94
|
-
globalThis.__INTOR_MESSAGES_POOL__ = new Keyv();
|
|
95
|
-
}
|
|
96
|
-
return globalThis.__INTOR_MESSAGES_POOL__;
|
|
97
|
-
}
|
|
14
|
+
// src/shared/constants/prefix-placeholder.ts
|
|
15
|
+
var PREFIX_PLACEHOLDER = "{locale}";
|
|
98
16
|
var mergeMessages = (staticMessages = {}, loadedMessages = {}) => {
|
|
99
17
|
if (!loadedMessages) return { ...staticMessages };
|
|
100
18
|
return merge({}, staticMessages, loadedMessages);
|
|
@@ -119,9 +37,6 @@ var normalizeCacheKey = (key, delimiter = CACHE_KEY_DELIMITER) => {
|
|
|
119
37
|
return String(key);
|
|
120
38
|
};
|
|
121
39
|
|
|
122
|
-
// src/shared/constants/prefix-placeholder.ts
|
|
123
|
-
var PREFIX_PLACEHOLDER = "{locale}";
|
|
124
|
-
|
|
125
40
|
// src/shared/utils/resolve-namespaces.ts
|
|
126
41
|
var resolveNamespaces = ({
|
|
127
42
|
config,
|
|
@@ -286,7 +201,145 @@ var standardizePathname = ({
|
|
|
286
201
|
return normalizePathname(standardizedPathname);
|
|
287
202
|
};
|
|
288
203
|
|
|
289
|
-
// src/
|
|
204
|
+
// src/adapters/next/shared/utils/locale-prefix-pathname.ts
|
|
205
|
+
var localePrefixPathname = ({
|
|
206
|
+
config,
|
|
207
|
+
pathname: standardizedPathname,
|
|
208
|
+
locale
|
|
209
|
+
}) => {
|
|
210
|
+
const { routing } = config;
|
|
211
|
+
const { prefix } = routing;
|
|
212
|
+
if (prefix !== "none" && !locale) {
|
|
213
|
+
throw new Error('No locale when using prefix "all", "except-default"');
|
|
214
|
+
}
|
|
215
|
+
if (prefix === "all") {
|
|
216
|
+
return normalizePathname(
|
|
217
|
+
standardizedPathname.replaceAll(PREFIX_PLACEHOLDER, locale)
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
if (prefix === "except-default") {
|
|
221
|
+
return locale === config.defaultLocale ? normalizePathname(
|
|
222
|
+
standardizedPathname.replaceAll(`/${PREFIX_PLACEHOLDER}`, "")
|
|
223
|
+
) : normalizePathname(
|
|
224
|
+
standardizedPathname.replaceAll(PREFIX_PLACEHOLDER, locale)
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
return normalizePathname(
|
|
228
|
+
standardizedPathname.replaceAll(`/${PREFIX_PLACEHOLDER}`, "")
|
|
229
|
+
);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// src/adapters/next/shared/utils/localize-pathname.ts
|
|
233
|
+
var localizePathname = ({
|
|
234
|
+
config,
|
|
235
|
+
pathname: rawPathname,
|
|
236
|
+
locale
|
|
237
|
+
}) => {
|
|
238
|
+
const { unprefixedPathname } = extractPathname({
|
|
239
|
+
config,
|
|
240
|
+
pathname: rawPathname
|
|
241
|
+
});
|
|
242
|
+
const standardizedPathname = standardizePathname({
|
|
243
|
+
config,
|
|
244
|
+
pathname: unprefixedPathname
|
|
245
|
+
});
|
|
246
|
+
const localePrefixedPathname = localePrefixPathname({
|
|
247
|
+
config,
|
|
248
|
+
pathname: standardizedPathname,
|
|
249
|
+
locale
|
|
250
|
+
});
|
|
251
|
+
return {
|
|
252
|
+
unprefixedPathname,
|
|
253
|
+
standardizedPathname,
|
|
254
|
+
localePrefixedPathname
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
var ConfigContext = React6.createContext(void 0);
|
|
258
|
+
function ConfigProvider({
|
|
259
|
+
value: { config, pathname },
|
|
260
|
+
children
|
|
261
|
+
}) {
|
|
262
|
+
const value = React6.useMemo(() => ({ config, pathname }), [config, pathname]);
|
|
263
|
+
return /* @__PURE__ */ jsx(ConfigContext.Provider, { value, children });
|
|
264
|
+
}
|
|
265
|
+
function useConfig() {
|
|
266
|
+
const context = React6.useContext(ConfigContext);
|
|
267
|
+
if (!context) throw new Error("useConfig must be used within ConfigProvider");
|
|
268
|
+
return context;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// src/config/constants/cache.constants.ts
|
|
272
|
+
var DEFAULT_CACHE_OPTIONS = {
|
|
273
|
+
enabled: process.env.NODE_ENV === "production",
|
|
274
|
+
ttl: 60 * 60 * 1e3
|
|
275
|
+
// 1 hour
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
// src/server/shared/logger/global-logger-pool.ts
|
|
279
|
+
function getGlobalLoggerPool() {
|
|
280
|
+
if (!globalThis.__INTOR_LOGGER_POOL__) {
|
|
281
|
+
globalThis.__INTOR_LOGGER_POOL__ = /* @__PURE__ */ new Map();
|
|
282
|
+
}
|
|
283
|
+
return globalThis.__INTOR_LOGGER_POOL__;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// src/server/shared/logger/get-logger.ts
|
|
287
|
+
var DEFAULT_FORMATTER_CONFIG = {
|
|
288
|
+
node: { meta: { compact: true }, lineBreaksAfter: 1 }
|
|
289
|
+
};
|
|
290
|
+
function getLogger({
|
|
291
|
+
id = "default",
|
|
292
|
+
formatterConfig,
|
|
293
|
+
preset,
|
|
294
|
+
...options
|
|
295
|
+
}) {
|
|
296
|
+
const pool = getGlobalLoggerPool();
|
|
297
|
+
let logger = pool.get(id);
|
|
298
|
+
const useDefault = !formatterConfig && !preset;
|
|
299
|
+
if (!logger) {
|
|
300
|
+
logger = logry({
|
|
301
|
+
id,
|
|
302
|
+
formatterConfig: useDefault ? DEFAULT_FORMATTER_CONFIG : formatterConfig,
|
|
303
|
+
preset,
|
|
304
|
+
...options
|
|
305
|
+
});
|
|
306
|
+
pool.set(id, logger);
|
|
307
|
+
if (pool.size > 1e3) {
|
|
308
|
+
const keys = [...pool.keys()];
|
|
309
|
+
for (const key of keys.slice(0, 200)) pool.delete(key);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return logger;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// src/server/messages/shared/utils/is-valid-messages.ts
|
|
316
|
+
function isPlainObject(value) {
|
|
317
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
318
|
+
}
|
|
319
|
+
function isValidMessages(value) {
|
|
320
|
+
if (!isPlainObject(value)) return false;
|
|
321
|
+
const stack = [value];
|
|
322
|
+
while (stack.length > 0) {
|
|
323
|
+
const current = stack.pop();
|
|
324
|
+
for (const v of Object.values(current)) {
|
|
325
|
+
if (typeof v === "string") continue;
|
|
326
|
+
if (isPlainObject(v)) {
|
|
327
|
+
stack.push(v);
|
|
328
|
+
} else {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
function getGlobalMessagesPool() {
|
|
336
|
+
if (!globalThis.__INTOR_MESSAGES_POOL__) {
|
|
337
|
+
globalThis.__INTOR_MESSAGES_POOL__ = new Keyv();
|
|
338
|
+
}
|
|
339
|
+
return globalThis.__INTOR_MESSAGES_POOL__;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// src/server/messages/load-remote-messages/fetch-locale-messages/fetch-locale-messages.ts
|
|
290
343
|
var fetchLocaleMessages = async ({
|
|
291
344
|
remoteUrl,
|
|
292
345
|
remoteHeaders,
|
|
@@ -328,7 +381,7 @@ var fetchLocaleMessages = async ({
|
|
|
328
381
|
}
|
|
329
382
|
};
|
|
330
383
|
|
|
331
|
-
// src/
|
|
384
|
+
// src/server/messages/load-remote-messages/fetch-locale-messages/utils/build-search-params.ts
|
|
332
385
|
var buildSearchParams = (params) => {
|
|
333
386
|
const searchParams = new URLSearchParams();
|
|
334
387
|
const appendParam = (key, value) => {
|
|
@@ -346,7 +399,7 @@ var buildSearchParams = (params) => {
|
|
|
346
399
|
return searchParams;
|
|
347
400
|
};
|
|
348
401
|
|
|
349
|
-
// src/
|
|
402
|
+
// src/server/messages/load-remote-messages/load-remote-messages.ts
|
|
350
403
|
var loadRemoteMessages = async ({
|
|
351
404
|
pool = getGlobalMessagesPool(),
|
|
352
405
|
rootDir,
|
|
@@ -415,7 +468,7 @@ var loadRemoteMessages = async ({
|
|
|
415
468
|
return messages;
|
|
416
469
|
};
|
|
417
470
|
|
|
418
|
-
// src/
|
|
471
|
+
// src/client/react/contexts/messages/utils/use-refetch-messages.ts
|
|
419
472
|
var useRefetchMessages = ({
|
|
420
473
|
config,
|
|
421
474
|
pathname,
|
|
@@ -461,10 +514,8 @@ var useRefetchMessages = ({
|
|
|
461
514
|
return { refetchMessages };
|
|
462
515
|
};
|
|
463
516
|
var MessagesContext = React6.createContext(void 0);
|
|
464
|
-
|
|
465
|
-
// src/adapters/next/contexts/messages/provider.tsx
|
|
466
517
|
function MessagesProvider({
|
|
467
|
-
value: { messages },
|
|
518
|
+
value: { messages = {} },
|
|
468
519
|
children
|
|
469
520
|
}) {
|
|
470
521
|
const { config, pathname } = useConfig();
|
|
@@ -486,7 +537,7 @@ function MessagesProvider({
|
|
|
486
537
|
}),
|
|
487
538
|
[loadedMessages, messages, isLoadingMessages, refetchMessages]
|
|
488
539
|
);
|
|
489
|
-
return /* @__PURE__ */
|
|
540
|
+
return /* @__PURE__ */ jsx(MessagesContext.Provider, { value, children });
|
|
490
541
|
}
|
|
491
542
|
function useMessages() {
|
|
492
543
|
const context = React6.useContext(MessagesContext);
|
|
@@ -495,7 +546,7 @@ function useMessages() {
|
|
|
495
546
|
return context;
|
|
496
547
|
}
|
|
497
548
|
|
|
498
|
-
// src/
|
|
549
|
+
// src/client/react/contexts/locale/utils/use-init-lazy-load.ts
|
|
499
550
|
var useInitLazyLoad = ({
|
|
500
551
|
loaderOptions,
|
|
501
552
|
currentLocale
|
|
@@ -511,7 +562,7 @@ var useInitLazyLoad = ({
|
|
|
511
562
|
}, [lazyLoad, currentLocale, refetchMessages, isFirstLoadedRef]);
|
|
512
563
|
};
|
|
513
564
|
|
|
514
|
-
// src/
|
|
565
|
+
// src/shared/utils/client/build-cookie-string.ts
|
|
515
566
|
var buildCookieString = (cookie, locale) => {
|
|
516
567
|
const parts = [`${cookie.name}=${encodeURIComponent(locale)}`];
|
|
517
568
|
if (cookie.maxAge) {
|
|
@@ -533,7 +584,7 @@ var buildCookieString = (cookie, locale) => {
|
|
|
533
584
|
return parts.join("; ");
|
|
534
585
|
};
|
|
535
586
|
|
|
536
|
-
// src/
|
|
587
|
+
// src/shared/utils/client/set-locale-cookie-browser.ts
|
|
537
588
|
var setLocaleCookieBrowser = ({
|
|
538
589
|
cookie,
|
|
539
590
|
locale
|
|
@@ -544,7 +595,7 @@ var setLocaleCookieBrowser = ({
|
|
|
544
595
|
document.cookie = cookieString;
|
|
545
596
|
};
|
|
546
597
|
|
|
547
|
-
// src/
|
|
598
|
+
// src/client/react/contexts/locale/utils/use-init-locale-cookie.ts
|
|
548
599
|
var useInitLocaleCookie = ({
|
|
549
600
|
config,
|
|
550
601
|
locale
|
|
@@ -563,7 +614,7 @@ var useInitLocaleCookie = ({
|
|
|
563
614
|
};
|
|
564
615
|
var LocaleContext = React6.createContext(void 0);
|
|
565
616
|
|
|
566
|
-
// src/
|
|
617
|
+
// src/client/react/contexts/locale/utils/change-locale.ts
|
|
567
618
|
var changeLocale = ({
|
|
568
619
|
currentLocale,
|
|
569
620
|
newLocale,
|
|
@@ -587,8 +638,6 @@ var changeLocale = ({
|
|
|
587
638
|
void refetchMessages(newLocale);
|
|
588
639
|
}
|
|
589
640
|
};
|
|
590
|
-
|
|
591
|
-
// src/adapters/next/contexts/locale/provider.tsx
|
|
592
641
|
function LocaleProvider({
|
|
593
642
|
value: { initialLocale },
|
|
594
643
|
children
|
|
@@ -619,7 +668,7 @@ function LocaleProvider({
|
|
|
619
668
|
}),
|
|
620
669
|
[currentLocale, setLocale]
|
|
621
670
|
);
|
|
622
|
-
return /* @__PURE__ */
|
|
671
|
+
return /* @__PURE__ */ jsx(LocaleContext.Provider, { value, children });
|
|
623
672
|
}
|
|
624
673
|
function useLocale() {
|
|
625
674
|
const context = React6.useContext(LocaleContext);
|
|
@@ -627,163 +676,22 @@ function useLocale() {
|
|
|
627
676
|
throw new Error("useLocale must be used within a LocaleProvider");
|
|
628
677
|
return context;
|
|
629
678
|
}
|
|
630
|
-
var TranslateHandlersContext = React6.createContext(void 0);
|
|
631
|
-
|
|
632
|
-
// src/adapters/next/contexts/translate-handlers/provider.tsx
|
|
633
|
-
var TranslateHandlersProvider = ({
|
|
634
|
-
children,
|
|
635
|
-
handlers
|
|
636
|
-
}) => {
|
|
637
|
-
const value = handlers;
|
|
638
|
-
return /* @__PURE__ */ React6.createElement(TranslateHandlersContext.Provider, { value }, children);
|
|
639
|
-
};
|
|
640
|
-
function useTranslateHandlers() {
|
|
641
|
-
const context = React6.useContext(TranslateHandlersContext);
|
|
642
|
-
return context;
|
|
643
|
-
}
|
|
644
|
-
var useInitLoadingState = (config) => {
|
|
645
|
-
const lazyLoad = !!config.loader?.lazyLoad;
|
|
646
|
-
const [isCsr, setIsCsr] = React6.useState(false);
|
|
647
|
-
React6.useEffect(() => {
|
|
648
|
-
setIsCsr(true);
|
|
649
|
-
}, []);
|
|
650
|
-
const isBeforeCSRLoading = lazyLoad && !isCsr;
|
|
651
|
-
return isBeforeCSRLoading;
|
|
652
|
-
};
|
|
653
|
-
var TranslatorContext = React6.createContext(void 0);
|
|
654
|
-
|
|
655
|
-
// src/adapters/next/contexts/translator/provider.tsx
|
|
656
|
-
var EMPTY_OBJECT = Object.freeze({});
|
|
657
|
-
function TranslatorProvider({ children }) {
|
|
658
|
-
const { config } = useConfig();
|
|
659
|
-
const { messages, isLoading } = useMessages();
|
|
660
|
-
const { locale } = useLocale();
|
|
661
|
-
const translatorHandlers = useTranslateHandlers();
|
|
662
|
-
const { fallbackLocales, translator: translatorOptions } = config;
|
|
663
|
-
const isBeforeCSRLoading = useInitLoadingState(config);
|
|
664
|
-
const value = React6.useMemo(() => {
|
|
665
|
-
const translator = new Translator({
|
|
666
|
-
messages: messages || EMPTY_OBJECT,
|
|
667
|
-
locale,
|
|
668
|
-
fallbackLocales,
|
|
669
|
-
loadingMessage: translatorOptions?.loadingMessage,
|
|
670
|
-
placeholder: translatorOptions?.placeholder,
|
|
671
|
-
handlers: translatorHandlers
|
|
672
|
-
});
|
|
673
|
-
translator.setLoading(isBeforeCSRLoading || isLoading);
|
|
674
|
-
return { translator };
|
|
675
|
-
}, [
|
|
676
|
-
fallbackLocales,
|
|
677
|
-
isBeforeCSRLoading,
|
|
678
|
-
isLoading,
|
|
679
|
-
locale,
|
|
680
|
-
messages,
|
|
681
|
-
translatorHandlers,
|
|
682
|
-
translatorOptions?.loadingMessage,
|
|
683
|
-
translatorOptions?.placeholder
|
|
684
|
-
]);
|
|
685
|
-
return /* @__PURE__ */ React6.createElement(TranslatorContext.Provider, { value }, children);
|
|
686
|
-
}
|
|
687
|
-
function useTranslator() {
|
|
688
|
-
const context = React6.useContext(TranslatorContext);
|
|
689
|
-
if (!context)
|
|
690
|
-
throw new Error(
|
|
691
|
-
"useTranslator must be used within IntorTranslatorProvider"
|
|
692
|
-
);
|
|
693
|
-
return context;
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
// src/adapters/next/contexts/intor-provider/intor-provider.tsx
|
|
697
|
-
var IntorProvider = ({
|
|
698
|
-
value: { config, pathname, initialLocale, messages },
|
|
699
|
-
children
|
|
700
|
-
}) => {
|
|
701
|
-
return /* @__PURE__ */ React6.createElement(ConfigProvider, { value: { config, pathname } }, /* @__PURE__ */ React6.createElement(MessagesProvider, { value: { messages } }, /* @__PURE__ */ React6.createElement(LocaleProvider, { value: { initialLocale } }, /* @__PURE__ */ React6.createElement(TranslatorProvider, null, children))));
|
|
702
|
-
};
|
|
703
|
-
|
|
704
|
-
// src/adapters/next/hooks/use-translator/use-translator.ts
|
|
705
|
-
function useTranslator2(preKey) {
|
|
706
|
-
const { translator } = useTranslator();
|
|
707
|
-
const { setLocale } = useLocale();
|
|
708
|
-
const props = {
|
|
709
|
-
messages: translator.messages,
|
|
710
|
-
locale: translator.locale,
|
|
711
|
-
isLoading: translator.isLoading,
|
|
712
|
-
setLocale
|
|
713
|
-
};
|
|
714
|
-
const scoped = translator.scoped(preKey);
|
|
715
|
-
return {
|
|
716
|
-
...props,
|
|
717
|
-
hasKey: preKey ? scoped.hasKey : translator.hasKey,
|
|
718
|
-
t: preKey ? scoped.t : translator.t
|
|
719
|
-
};
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
// src/adapters/next/shared/utils/locale-prefix-pathname.ts
|
|
723
|
-
var localePrefixPathname = ({
|
|
724
|
-
config,
|
|
725
|
-
pathname: standardizedPathname,
|
|
726
|
-
locale
|
|
727
|
-
}) => {
|
|
728
|
-
const { routing } = config;
|
|
729
|
-
const { prefix } = routing;
|
|
730
|
-
if (prefix !== "none" && !locale) {
|
|
731
|
-
throw new Error('No locale when using prefix "all", "except-default"');
|
|
732
|
-
}
|
|
733
|
-
if (prefix === "all") {
|
|
734
|
-
return normalizePathname(
|
|
735
|
-
standardizedPathname.replaceAll(PREFIX_PLACEHOLDER, locale)
|
|
736
|
-
);
|
|
737
|
-
}
|
|
738
|
-
if (prefix === "except-default") {
|
|
739
|
-
return locale === config.defaultLocale ? normalizePathname(
|
|
740
|
-
standardizedPathname.replaceAll(`/${PREFIX_PLACEHOLDER}`, "")
|
|
741
|
-
) : normalizePathname(
|
|
742
|
-
standardizedPathname.replaceAll(PREFIX_PLACEHOLDER, locale)
|
|
743
|
-
);
|
|
744
|
-
}
|
|
745
|
-
return normalizePathname(
|
|
746
|
-
standardizedPathname.replaceAll(`/${PREFIX_PLACEHOLDER}`, "")
|
|
747
|
-
);
|
|
748
|
-
};
|
|
749
|
-
|
|
750
|
-
// src/adapters/next/shared/utils/localize-pathname.ts
|
|
751
|
-
var localizePathname = ({
|
|
752
|
-
config,
|
|
753
|
-
pathname: rawPathname,
|
|
754
|
-
locale
|
|
755
|
-
}) => {
|
|
756
|
-
const { unprefixedPathname } = extractPathname({
|
|
757
|
-
config,
|
|
758
|
-
pathname: rawPathname
|
|
759
|
-
});
|
|
760
|
-
const standardizedPathname = standardizePathname({
|
|
761
|
-
config,
|
|
762
|
-
pathname: unprefixedPathname
|
|
763
|
-
});
|
|
764
|
-
const localePrefixedPathname = localePrefixPathname({
|
|
765
|
-
config,
|
|
766
|
-
pathname: standardizedPathname,
|
|
767
|
-
locale
|
|
768
|
-
});
|
|
769
|
-
return {
|
|
770
|
-
unprefixedPathname,
|
|
771
|
-
standardizedPathname,
|
|
772
|
-
localePrefixedPathname
|
|
773
|
-
};
|
|
774
|
-
};
|
|
775
679
|
|
|
776
680
|
// src/adapters/next/navigation/use-pathname.ts
|
|
777
681
|
var usePathname = () => {
|
|
778
682
|
const { config } = useConfig();
|
|
779
683
|
const { locale } = useLocale();
|
|
780
684
|
const rawPathname = usePathname$1();
|
|
781
|
-
const { localePrefixedPathname } = localizePathname({
|
|
685
|
+
const { localePrefixedPathname, standardizedPathname, unprefixedPathname } = localizePathname({
|
|
782
686
|
config,
|
|
783
687
|
pathname: rawPathname,
|
|
784
688
|
locale
|
|
785
689
|
});
|
|
786
|
-
return
|
|
690
|
+
return {
|
|
691
|
+
localizedPathname: localePrefixedPathname,
|
|
692
|
+
standardizedPathname,
|
|
693
|
+
unprefixedPathname
|
|
694
|
+
};
|
|
787
695
|
};
|
|
788
696
|
|
|
789
697
|
// src/adapters/next/navigation/utils/should-full-reload.ts
|
|
@@ -808,14 +716,14 @@ var shouldFullReload = ({
|
|
|
808
716
|
var useLocaleSwitch = () => {
|
|
809
717
|
const { config } = useConfig();
|
|
810
718
|
const { locale: currentLocale, setLocale } = useLocale();
|
|
811
|
-
const
|
|
719
|
+
const { localizedPathname } = usePathname();
|
|
812
720
|
const resolveHref = ({
|
|
813
721
|
href,
|
|
814
722
|
locale
|
|
815
723
|
}) => {
|
|
816
724
|
const isLocaleValid = locale && config.supportedLocales?.includes(locale);
|
|
817
725
|
const targetLocale = isLocaleValid ? locale : currentLocale;
|
|
818
|
-
const targetPathname = href ??
|
|
726
|
+
const targetPathname = href ?? localizedPathname;
|
|
819
727
|
const isExternal = targetPathname.startsWith("http");
|
|
820
728
|
const resolvedHref = !isExternal ? localizePathname({
|
|
821
729
|
config,
|
|
@@ -841,8 +749,6 @@ var useLocaleSwitch = () => {
|
|
|
841
749
|
};
|
|
842
750
|
return { resolveHref, switchLocale };
|
|
843
751
|
};
|
|
844
|
-
|
|
845
|
-
// src/adapters/next/navigation/link.tsx
|
|
846
752
|
var Link = ({
|
|
847
753
|
href,
|
|
848
754
|
locale,
|
|
@@ -857,7 +763,7 @@ var Link = ({
|
|
|
857
763
|
onClick?.(e);
|
|
858
764
|
switchLocale({ href: formatted, locale });
|
|
859
765
|
};
|
|
860
|
-
return /* @__PURE__ */
|
|
766
|
+
return /* @__PURE__ */ jsx(NextLink, { href: resolvedHref, onClick: handleClick, ...props, children });
|
|
861
767
|
};
|
|
862
768
|
var useRouter = () => {
|
|
863
769
|
const { push, replace, ...rest } = useRouter$1();
|
|
@@ -930,5 +836,90 @@ var redirect = async ({
|
|
|
930
836
|
});
|
|
931
837
|
redirect$1(localePrefixedPathname, type);
|
|
932
838
|
};
|
|
839
|
+
var TranslateHandlersContext = React6.createContext(void 0);
|
|
840
|
+
var TranslateHandlersProvider = ({
|
|
841
|
+
children,
|
|
842
|
+
handlers
|
|
843
|
+
}) => {
|
|
844
|
+
const value = handlers;
|
|
845
|
+
return /* @__PURE__ */ jsx(TranslateHandlersContext.Provider, { value, children });
|
|
846
|
+
};
|
|
847
|
+
function useTranslateHandlers() {
|
|
848
|
+
const context = React6.useContext(TranslateHandlersContext);
|
|
849
|
+
return context;
|
|
850
|
+
}
|
|
851
|
+
var useInitLoadingState = (config) => {
|
|
852
|
+
const lazyLoad = !!config.loader?.lazyLoad;
|
|
853
|
+
const [isCsr, setIsCsr] = React6.useState(false);
|
|
854
|
+
React6.useEffect(() => {
|
|
855
|
+
setIsCsr(true);
|
|
856
|
+
}, []);
|
|
857
|
+
const isBeforeCSRLoading = lazyLoad && !isCsr;
|
|
858
|
+
return isBeforeCSRLoading;
|
|
859
|
+
};
|
|
860
|
+
var TranslatorContext = React6.createContext(void 0);
|
|
861
|
+
var EMPTY_OBJECT = Object.freeze({});
|
|
862
|
+
function TranslatorProvider({ children }) {
|
|
863
|
+
const { config } = useConfig();
|
|
864
|
+
const { messages, isLoading } = useMessages();
|
|
865
|
+
const { locale } = useLocale();
|
|
866
|
+
const translatorHandlers = useTranslateHandlers();
|
|
867
|
+
const { fallbackLocales, translator: translatorOptions } = config;
|
|
868
|
+
const isBeforeCSRLoading = useInitLoadingState(config);
|
|
869
|
+
const value = React6.useMemo(() => {
|
|
870
|
+
const translator = new Translator({
|
|
871
|
+
messages: messages || EMPTY_OBJECT,
|
|
872
|
+
locale,
|
|
873
|
+
fallbackLocales,
|
|
874
|
+
loadingMessage: translatorOptions?.loadingMessage,
|
|
875
|
+
placeholder: translatorOptions?.placeholder,
|
|
876
|
+
handlers: translatorHandlers
|
|
877
|
+
});
|
|
878
|
+
translator.setLoading(isBeforeCSRLoading || isLoading);
|
|
879
|
+
return { translator };
|
|
880
|
+
}, [
|
|
881
|
+
fallbackLocales,
|
|
882
|
+
isBeforeCSRLoading,
|
|
883
|
+
isLoading,
|
|
884
|
+
locale,
|
|
885
|
+
messages,
|
|
886
|
+
translatorHandlers,
|
|
887
|
+
translatorOptions?.loadingMessage,
|
|
888
|
+
translatorOptions?.placeholder
|
|
889
|
+
]);
|
|
890
|
+
return /* @__PURE__ */ jsx(TranslatorContext.Provider, { value, children });
|
|
891
|
+
}
|
|
892
|
+
function useTranslator() {
|
|
893
|
+
const context = React6.useContext(TranslatorContext);
|
|
894
|
+
if (!context)
|
|
895
|
+
throw new Error(
|
|
896
|
+
"useTranslator must be used within IntorTranslatorProvider"
|
|
897
|
+
);
|
|
898
|
+
return context;
|
|
899
|
+
}
|
|
900
|
+
var IntorProvider = ({
|
|
901
|
+
value: { config, pathname = "", initialLocale, messages = config.messages },
|
|
902
|
+
children
|
|
903
|
+
}) => {
|
|
904
|
+
return /* @__PURE__ */ jsx(ConfigProvider, { value: { config, pathname }, children: /* @__PURE__ */ jsx(MessagesProvider, { value: { messages }, children: /* @__PURE__ */ jsx(LocaleProvider, { value: { initialLocale }, children: /* @__PURE__ */ jsx(TranslatorProvider, { children }) }) }) });
|
|
905
|
+
};
|
|
906
|
+
|
|
907
|
+
// src/client/react/hooks/use-translator.ts
|
|
908
|
+
function useTranslator2(preKey) {
|
|
909
|
+
const { translator } = useTranslator();
|
|
910
|
+
const { setLocale } = useLocale();
|
|
911
|
+
const props = {
|
|
912
|
+
messages: translator.messages,
|
|
913
|
+
locale: translator.locale,
|
|
914
|
+
isLoading: translator.isLoading,
|
|
915
|
+
setLocale
|
|
916
|
+
};
|
|
917
|
+
const scoped = translator.scoped(preKey);
|
|
918
|
+
return {
|
|
919
|
+
...props,
|
|
920
|
+
hasKey: preKey ? scoped.hasKey : translator.hasKey,
|
|
921
|
+
t: preKey ? scoped.t : translator.t
|
|
922
|
+
};
|
|
923
|
+
}
|
|
933
924
|
|
|
934
|
-
export { IntorProvider, Link,
|
|
925
|
+
export { IntorProvider, Link, TranslateHandlersProvider, redirect, usePathname, useRouter, useTranslator2 as useTranslator };
|