intor 2.2.11 → 2.2.13
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 +1 -1
- package/dist/config/index.d.cts +3 -3
- package/dist/config/index.d.ts +3 -3
- package/dist/config/index.js +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/next/index.cjs +121 -139
- package/dist/next/index.d.cts +8 -6
- package/dist/next/index.d.ts +8 -6
- package/dist/next/index.js +120 -138
- package/dist/next/middleware/index.cjs +1 -1
- package/dist/next/middleware/index.d.cts +3 -3
- package/dist/next/middleware/index.d.ts +3 -3
- package/dist/next/middleware/index.js +1 -1
- package/dist/next/server/index.cjs +1 -1
- package/dist/next/server/index.d.cts +3 -3
- package/dist/next/server/index.d.ts +3 -3
- package/dist/next/server/index.js +1 -1
- package/dist/react/index.cjs +120 -138
- package/dist/react/index.d.cts +8 -6
- package/dist/react/index.d.ts +8 -6
- package/dist/react/index.js +119 -137
- package/dist/server/index.cjs +3 -10
- package/dist/server/index.d.cts +3 -3
- package/dist/server/index.d.ts +3 -3
- package/dist/server/index.js +3 -10
- package/package.json +2 -2
package/dist/config/index.cjs
CHANGED
package/dist/config/index.d.cts
CHANGED
|
@@ -2,8 +2,8 @@ import { Level, NormalizerConfig, FormatterConfig, LoggerPreset } from 'logry/ed
|
|
|
2
2
|
import { Locale, LocaleMessages, FallbackLocalesMap } from 'intor-translator';
|
|
3
3
|
|
|
4
4
|
type CookieRawOptions = {
|
|
5
|
-
/**
|
|
6
|
-
|
|
5
|
+
/** Enable cookie usage (read/write) - default: true */
|
|
6
|
+
enabled?: boolean;
|
|
7
7
|
/** Allow the system to automatically set cookies - default: true */
|
|
8
8
|
autoSetCookie?: boolean;
|
|
9
9
|
/** default: "intor.i18n.locale" */
|
|
@@ -51,7 +51,6 @@ type BaseLoaderOptions = {
|
|
|
51
51
|
namespaces?: string[];
|
|
52
52
|
routeNamespaces?: RouteNamespaces;
|
|
53
53
|
concurrency?: number;
|
|
54
|
-
lazyLoad?: boolean;
|
|
55
54
|
};
|
|
56
55
|
type LocalLoader = BaseLoaderOptions & {
|
|
57
56
|
type: "local";
|
|
@@ -88,6 +87,7 @@ type RoutingRawOptions = {
|
|
|
88
87
|
type RoutingResolvedOptions = Required<RoutingRawOptions>;
|
|
89
88
|
|
|
90
89
|
type CacheRawOptions = {
|
|
90
|
+
/** default: process.env.NODE_ENV === "production" */
|
|
91
91
|
enabled?: boolean;
|
|
92
92
|
/** default: 60\*60\*1000 (1 hour) */
|
|
93
93
|
ttl?: number;
|
package/dist/config/index.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { Level, NormalizerConfig, FormatterConfig, LoggerPreset } from 'logry/ed
|
|
|
2
2
|
import { Locale, LocaleMessages, FallbackLocalesMap } from 'intor-translator';
|
|
3
3
|
|
|
4
4
|
type CookieRawOptions = {
|
|
5
|
-
/**
|
|
6
|
-
|
|
5
|
+
/** Enable cookie usage (read/write) - default: true */
|
|
6
|
+
enabled?: boolean;
|
|
7
7
|
/** Allow the system to automatically set cookies - default: true */
|
|
8
8
|
autoSetCookie?: boolean;
|
|
9
9
|
/** default: "intor.i18n.locale" */
|
|
@@ -51,7 +51,6 @@ type BaseLoaderOptions = {
|
|
|
51
51
|
namespaces?: string[];
|
|
52
52
|
routeNamespaces?: RouteNamespaces;
|
|
53
53
|
concurrency?: number;
|
|
54
|
-
lazyLoad?: boolean;
|
|
55
54
|
};
|
|
56
55
|
type LocalLoader = BaseLoaderOptions & {
|
|
57
56
|
type: "local";
|
|
@@ -88,6 +87,7 @@ type RoutingRawOptions = {
|
|
|
88
87
|
type RoutingResolvedOptions = Required<RoutingRawOptions>;
|
|
89
88
|
|
|
90
89
|
type CacheRawOptions = {
|
|
90
|
+
/** default: process.env.NODE_ENV === "production" */
|
|
91
91
|
enabled?: boolean;
|
|
92
92
|
/** default: 60\*60\*1000 (1 hour) */
|
|
93
93
|
ttl?: number;
|
package/dist/config/index.js
CHANGED
package/dist/index.d.cts
CHANGED
|
@@ -14,8 +14,8 @@ type RawCacheKey = string | boolean | Array<string | number | boolean | undefine
|
|
|
14
14
|
declare const normalizeCacheKey: (key?: RawCacheKey, delimiter?: string) => string | null;
|
|
15
15
|
|
|
16
16
|
type CookieRawOptions = {
|
|
17
|
-
/**
|
|
18
|
-
|
|
17
|
+
/** Enable cookie usage (read/write) - default: true */
|
|
18
|
+
enabled?: boolean;
|
|
19
19
|
/** Allow the system to automatically set cookies - default: true */
|
|
20
20
|
autoSetCookie?: boolean;
|
|
21
21
|
/** default: "intor.i18n.locale" */
|
|
@@ -63,7 +63,6 @@ type BaseLoaderOptions = {
|
|
|
63
63
|
namespaces?: string[];
|
|
64
64
|
routeNamespaces?: RouteNamespaces;
|
|
65
65
|
concurrency?: number;
|
|
66
|
-
lazyLoad?: boolean;
|
|
67
66
|
};
|
|
68
67
|
type LocalLoader = BaseLoaderOptions & {
|
|
69
68
|
type: "local";
|
|
@@ -100,6 +99,7 @@ type RoutingRawOptions = {
|
|
|
100
99
|
type RoutingResolvedOptions = Required<RoutingRawOptions>;
|
|
101
100
|
|
|
102
101
|
type CacheRawOptions = {
|
|
102
|
+
/** default: process.env.NODE_ENV === "production" */
|
|
103
103
|
enabled?: boolean;
|
|
104
104
|
/** default: 60\*60\*1000 (1 hour) */
|
|
105
105
|
ttl?: number;
|
package/dist/index.d.ts
CHANGED
|
@@ -14,8 +14,8 @@ type RawCacheKey = string | boolean | Array<string | number | boolean | undefine
|
|
|
14
14
|
declare const normalizeCacheKey: (key?: RawCacheKey, delimiter?: string) => string | null;
|
|
15
15
|
|
|
16
16
|
type CookieRawOptions = {
|
|
17
|
-
/**
|
|
18
|
-
|
|
17
|
+
/** Enable cookie usage (read/write) - default: true */
|
|
18
|
+
enabled?: boolean;
|
|
19
19
|
/** Allow the system to automatically set cookies - default: true */
|
|
20
20
|
autoSetCookie?: boolean;
|
|
21
21
|
/** default: "intor.i18n.locale" */
|
|
@@ -63,7 +63,6 @@ type BaseLoaderOptions = {
|
|
|
63
63
|
namespaces?: string[];
|
|
64
64
|
routeNamespaces?: RouteNamespaces;
|
|
65
65
|
concurrency?: number;
|
|
66
|
-
lazyLoad?: boolean;
|
|
67
66
|
};
|
|
68
67
|
type LocalLoader = BaseLoaderOptions & {
|
|
69
68
|
type: "local";
|
|
@@ -100,6 +99,7 @@ type RoutingRawOptions = {
|
|
|
100
99
|
type RoutingResolvedOptions = Required<RoutingRawOptions>;
|
|
101
100
|
|
|
102
101
|
type CacheRawOptions = {
|
|
102
|
+
/** default: process.env.NODE_ENV === "production" */
|
|
103
103
|
enabled?: boolean;
|
|
104
104
|
/** default: 60\*60\*1000 (1 hour) */
|
|
105
105
|
ttl?: number;
|
package/dist/next/index.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var formatUrl = require('next/dist/shared/lib/router/utils/format-url');
|
|
4
4
|
var NextLink = require('next/link');
|
|
5
|
-
var
|
|
5
|
+
var React7 = require('react');
|
|
6
6
|
var navigation = require('next/navigation');
|
|
7
7
|
var merge = require('lodash.merge');
|
|
8
8
|
var jsxRuntime = require('react/jsx-runtime');
|
|
@@ -32,7 +32,7 @@ function _interopNamespace(e) {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
var NextLink__default = /*#__PURE__*/_interopDefault(NextLink);
|
|
35
|
-
var
|
|
35
|
+
var React7__namespace = /*#__PURE__*/_interopNamespace(React7);
|
|
36
36
|
var merge__default = /*#__PURE__*/_interopDefault(merge);
|
|
37
37
|
var Keyv__default = /*#__PURE__*/_interopDefault(Keyv);
|
|
38
38
|
|
|
@@ -281,20 +281,71 @@ var localizePathname = ({
|
|
|
281
281
|
localePrefixedPathname
|
|
282
282
|
};
|
|
283
283
|
};
|
|
284
|
-
var ConfigContext =
|
|
284
|
+
var ConfigContext = React7__namespace.createContext(void 0);
|
|
285
285
|
function ConfigProvider({
|
|
286
286
|
value: { config, pathname },
|
|
287
287
|
children
|
|
288
288
|
}) {
|
|
289
|
-
const value =
|
|
289
|
+
const value = React7__namespace.useMemo(() => ({ config, pathname }), [config, pathname]);
|
|
290
290
|
return /* @__PURE__ */ jsxRuntime.jsx(ConfigContext.Provider, { value, children });
|
|
291
291
|
}
|
|
292
292
|
function useConfig() {
|
|
293
|
-
const context =
|
|
293
|
+
const context = React7__namespace.useContext(ConfigContext);
|
|
294
294
|
if (!context) throw new Error("useConfig must be used within ConfigProvider");
|
|
295
295
|
return context;
|
|
296
296
|
}
|
|
297
297
|
|
|
298
|
+
// src/shared/utils/client/build-cookie-string.ts
|
|
299
|
+
var buildCookieString = (cookie, locale) => {
|
|
300
|
+
const parts = [`${cookie.name}=${encodeURIComponent(locale)}`];
|
|
301
|
+
if (cookie.maxAge) {
|
|
302
|
+
const expires = new Date(Date.now() + cookie.maxAge * 1e3).toUTCString();
|
|
303
|
+
parts.push(`expires=${expires}`, `max-age=${cookie.maxAge}`);
|
|
304
|
+
}
|
|
305
|
+
parts.push(`path=${cookie.path ?? "/"}`);
|
|
306
|
+
if (cookie.domain) {
|
|
307
|
+
parts.push(`domain=${cookie.domain}`);
|
|
308
|
+
}
|
|
309
|
+
if (cookie.sameSite) {
|
|
310
|
+
parts.push(
|
|
311
|
+
`SameSite=${cookie.sameSite[0].toUpperCase()}${cookie.sameSite.slice(1).toLowerCase()}`
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
if (cookie.secure !== false) {
|
|
315
|
+
parts.push(`Secure`);
|
|
316
|
+
}
|
|
317
|
+
return parts.join("; ");
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
// src/shared/utils/client/set-locale-cookie-browser.ts
|
|
321
|
+
var setLocaleCookieBrowser = ({
|
|
322
|
+
cookie,
|
|
323
|
+
locale
|
|
324
|
+
}) => {
|
|
325
|
+
if (typeof document === "undefined") return;
|
|
326
|
+
if (!cookie.enabled || !cookie.autoSetCookie) return;
|
|
327
|
+
const cookieString = buildCookieString(cookie, locale);
|
|
328
|
+
document.cookie = cookieString;
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
// src/client/react/contexts/locale/utils/use-init-locale-cookie.ts
|
|
332
|
+
var useInitLocaleCookie = ({
|
|
333
|
+
config,
|
|
334
|
+
locale
|
|
335
|
+
}) => {
|
|
336
|
+
React7__namespace.useEffect(() => {
|
|
337
|
+
if (typeof document === "undefined") return;
|
|
338
|
+
const { cookie, routing } = config;
|
|
339
|
+
const { firstVisit } = routing;
|
|
340
|
+
const cookies2 = document.cookie.split(";").map((c) => c.trim());
|
|
341
|
+
const isCookieExists = cookies2.some((c) => c.startsWith(`${cookie.name}=`));
|
|
342
|
+
if (isCookieExists) return;
|
|
343
|
+
if (!firstVisit.redirect) return;
|
|
344
|
+
if (!cookie.enabled || !cookie.autoSetCookie) return;
|
|
345
|
+
setLocaleCookieBrowser({ cookie, locale });
|
|
346
|
+
}, []);
|
|
347
|
+
};
|
|
348
|
+
|
|
298
349
|
// src/config/constants/cache.constants.ts
|
|
299
350
|
var DEFAULT_CACHE_OPTIONS = {
|
|
300
351
|
enabled: process.env.NODE_ENV === "production",
|
|
@@ -502,19 +553,19 @@ var useRefetchMessages = ({
|
|
|
502
553
|
setLoadedMessages,
|
|
503
554
|
setIsLoadingMessages
|
|
504
555
|
}) => {
|
|
505
|
-
const { messages: staticMessages } = config;
|
|
506
|
-
const namespaces =
|
|
507
|
-
if (!
|
|
556
|
+
const { messages: staticMessages, loader } = config;
|
|
557
|
+
const namespaces = React7__namespace.useMemo(() => {
|
|
558
|
+
if (!loader) return [];
|
|
508
559
|
return resolveNamespaces({ config, pathname });
|
|
509
560
|
}, [config, pathname]);
|
|
510
|
-
const refetchMessages =
|
|
561
|
+
const refetchMessages = React7__namespace.useCallback(
|
|
511
562
|
async (newLocale) => {
|
|
512
|
-
if (
|
|
563
|
+
if (loader?.type === "remote") {
|
|
513
564
|
setIsLoadingMessages(true);
|
|
514
565
|
const loadedMessages = await loadRemoteMessages({
|
|
515
|
-
rootDir:
|
|
516
|
-
remoteUrl:
|
|
517
|
-
remoteHeaders:
|
|
566
|
+
rootDir: loader.rootDir,
|
|
567
|
+
remoteUrl: loader.remoteUrl,
|
|
568
|
+
remoteHeaders: loader.remoteHeaders,
|
|
518
569
|
locale: newLocale,
|
|
519
570
|
fallbackLocales: config.fallbackLocales[newLocale] || [],
|
|
520
571
|
namespaces,
|
|
@@ -529,8 +580,9 @@ var useRefetchMessages = ({
|
|
|
529
580
|
}
|
|
530
581
|
},
|
|
531
582
|
[
|
|
532
|
-
|
|
583
|
+
loader,
|
|
533
584
|
config.fallbackLocales,
|
|
585
|
+
config.cache,
|
|
534
586
|
config.id,
|
|
535
587
|
setIsLoadingMessages,
|
|
536
588
|
namespaces,
|
|
@@ -540,21 +592,21 @@ var useRefetchMessages = ({
|
|
|
540
592
|
);
|
|
541
593
|
return { refetchMessages };
|
|
542
594
|
};
|
|
543
|
-
var MessagesContext =
|
|
595
|
+
var MessagesContext = React7__namespace.createContext(void 0);
|
|
544
596
|
function MessagesProvider({
|
|
545
597
|
value: { messages = {} },
|
|
546
598
|
children
|
|
547
599
|
}) {
|
|
548
600
|
const { config, pathname } = useConfig();
|
|
549
|
-
const [loadedMessages, setLoadedMessages] =
|
|
550
|
-
const [isLoadingMessages, setIsLoadingMessages] =
|
|
601
|
+
const [loadedMessages, setLoadedMessages] = React7__namespace.useState(null);
|
|
602
|
+
const [isLoadingMessages, setIsLoadingMessages] = React7__namespace.useState(false);
|
|
551
603
|
const { refetchMessages } = useRefetchMessages({
|
|
552
604
|
config,
|
|
553
605
|
pathname,
|
|
554
606
|
setLoadedMessages,
|
|
555
607
|
setIsLoadingMessages
|
|
556
608
|
});
|
|
557
|
-
const value =
|
|
609
|
+
const value = React7__namespace.useMemo(
|
|
558
610
|
() => ({
|
|
559
611
|
messages: loadedMessages || messages,
|
|
560
612
|
isLoading: isLoadingMessages,
|
|
@@ -567,128 +619,61 @@ function MessagesProvider({
|
|
|
567
619
|
return /* @__PURE__ */ jsxRuntime.jsx(MessagesContext.Provider, { value, children });
|
|
568
620
|
}
|
|
569
621
|
function useMessages() {
|
|
570
|
-
const context =
|
|
622
|
+
const context = React7__namespace.useContext(MessagesContext);
|
|
571
623
|
if (!context)
|
|
572
624
|
throw new Error("useMessages must be used within a MessagesProvider");
|
|
573
625
|
return context;
|
|
574
626
|
}
|
|
575
|
-
|
|
576
|
-
// src/client/react/contexts/locale/utils/use-init-lazy-load.ts
|
|
577
|
-
var useInitLazyLoad = ({
|
|
578
|
-
loaderOptions,
|
|
579
|
-
currentLocale
|
|
580
|
-
}) => {
|
|
581
|
-
const { refetchMessages } = useMessages();
|
|
582
|
-
const lazyLoad = !!loaderOptions?.lazyLoad;
|
|
583
|
-
const isFirstLoadedRef = React6__namespace.useRef(false);
|
|
584
|
-
React6__namespace.useEffect(() => {
|
|
585
|
-
if (lazyLoad && !isFirstLoadedRef.current) {
|
|
586
|
-
void refetchMessages(currentLocale);
|
|
587
|
-
isFirstLoadedRef.current = true;
|
|
588
|
-
}
|
|
589
|
-
}, [lazyLoad, currentLocale, refetchMessages, isFirstLoadedRef]);
|
|
590
|
-
};
|
|
591
|
-
|
|
592
|
-
// src/shared/utils/client/build-cookie-string.ts
|
|
593
|
-
var buildCookieString = (cookie, locale) => {
|
|
594
|
-
const parts = [`${cookie.name}=${encodeURIComponent(locale)}`];
|
|
595
|
-
if (cookie.maxAge) {
|
|
596
|
-
const expires = new Date(Date.now() + cookie.maxAge * 1e3).toUTCString();
|
|
597
|
-
parts.push(`expires=${expires}`, `max-age=${cookie.maxAge}`);
|
|
598
|
-
}
|
|
599
|
-
parts.push(`path=${cookie.path ?? "/"}`);
|
|
600
|
-
if (cookie.domain) {
|
|
601
|
-
parts.push(`domain=${cookie.domain}`);
|
|
602
|
-
}
|
|
603
|
-
if (cookie.sameSite) {
|
|
604
|
-
parts.push(
|
|
605
|
-
`SameSite=${cookie.sameSite[0].toUpperCase()}${cookie.sameSite.slice(1).toLowerCase()}`
|
|
606
|
-
);
|
|
607
|
-
}
|
|
608
|
-
if (cookie.secure !== false) {
|
|
609
|
-
parts.push(`Secure`);
|
|
610
|
-
}
|
|
611
|
-
return parts.join("; ");
|
|
612
|
-
};
|
|
613
|
-
|
|
614
|
-
// src/shared/utils/client/set-locale-cookie-browser.ts
|
|
615
|
-
var setLocaleCookieBrowser = ({
|
|
616
|
-
cookie,
|
|
617
|
-
locale
|
|
618
|
-
}) => {
|
|
619
|
-
if (globalThis.window === void 0) return;
|
|
620
|
-
if (cookie.disabled || !cookie.autoSetCookie) return;
|
|
621
|
-
const cookieString = buildCookieString(cookie, locale);
|
|
622
|
-
document.cookie = cookieString;
|
|
623
|
-
};
|
|
624
|
-
|
|
625
|
-
// src/client/react/contexts/locale/utils/use-init-locale-cookie.ts
|
|
626
|
-
var useInitLocaleCookie = ({
|
|
627
|
-
config,
|
|
628
|
-
locale
|
|
629
|
-
}) => {
|
|
630
|
-
React6__namespace.useEffect(() => {
|
|
631
|
-
if (typeof document === "undefined") return;
|
|
632
|
-
const { cookie, routing } = config;
|
|
633
|
-
const { firstVisit } = routing;
|
|
634
|
-
const cookies2 = document.cookie.split(";").map((c) => c.trim());
|
|
635
|
-
const isCookieExists = cookies2.some((c) => c.startsWith(`${cookie.name}=`));
|
|
636
|
-
if (isCookieExists) return;
|
|
637
|
-
if (!firstVisit.redirect) return;
|
|
638
|
-
if (cookie.disabled || !cookie.autoSetCookie) return;
|
|
639
|
-
setLocaleCookieBrowser({ cookie, locale });
|
|
640
|
-
}, []);
|
|
641
|
-
};
|
|
642
|
-
var LocaleContext = React6__namespace.createContext(void 0);
|
|
627
|
+
var LocaleContext = React7__namespace.createContext(void 0);
|
|
643
628
|
|
|
644
629
|
// src/client/react/contexts/locale/utils/change-locale.ts
|
|
645
630
|
var changeLocale = ({
|
|
646
631
|
currentLocale,
|
|
647
632
|
newLocale,
|
|
648
|
-
|
|
633
|
+
loader,
|
|
649
634
|
cookie,
|
|
650
635
|
setLocale,
|
|
651
636
|
refetchMessages
|
|
652
637
|
}) => {
|
|
653
638
|
if (typeof document === "undefined") return;
|
|
654
|
-
const
|
|
639
|
+
const { type } = loader || {};
|
|
655
640
|
if (newLocale === currentLocale) return;
|
|
656
|
-
if (
|
|
641
|
+
if (type === "local") {
|
|
657
642
|
console.warn(
|
|
658
|
-
`[Intor] You are using
|
|
643
|
+
`[Intor] You are using "loader type: local" to switch languages. Please make sure to use the wrapped <Link> component to trigger a page reload, ensuring that the translation data is dynamically updated.`
|
|
659
644
|
);
|
|
660
645
|
}
|
|
661
646
|
setLocale(newLocale);
|
|
662
647
|
setLocaleCookieBrowser({ cookie, locale: newLocale });
|
|
663
648
|
document.documentElement.lang = newLocale;
|
|
664
|
-
if (
|
|
649
|
+
if (type === "remote" && refetchMessages) {
|
|
665
650
|
void refetchMessages(newLocale);
|
|
666
651
|
}
|
|
667
652
|
};
|
|
668
653
|
function LocaleProvider({
|
|
669
|
-
value: { initialLocale },
|
|
654
|
+
value: { initialLocale, onLocaleChange },
|
|
670
655
|
children
|
|
671
656
|
}) {
|
|
672
657
|
const { config } = useConfig();
|
|
673
658
|
const { refetchMessages } = useMessages();
|
|
674
|
-
const { loader
|
|
675
|
-
const [currentLocale, setCurrentLocale] =
|
|
676
|
-
useInitLazyLoad({ loaderOptions, currentLocale });
|
|
659
|
+
const { loader, cookie } = config;
|
|
660
|
+
const [currentLocale, setCurrentLocale] = React7__namespace.useState(initialLocale);
|
|
677
661
|
useInitLocaleCookie({ config, locale: initialLocale });
|
|
678
|
-
const setLocale =
|
|
679
|
-
(newLocale) => {
|
|
662
|
+
const setLocale = React7__namespace.useCallback(
|
|
663
|
+
async (newLocale) => {
|
|
680
664
|
changeLocale({
|
|
681
665
|
currentLocale,
|
|
682
666
|
newLocale,
|
|
683
|
-
|
|
667
|
+
loader,
|
|
684
668
|
cookie,
|
|
685
669
|
setLocale: setCurrentLocale,
|
|
686
670
|
refetchMessages
|
|
687
671
|
});
|
|
672
|
+
onLocaleChange?.(newLocale);
|
|
688
673
|
},
|
|
689
|
-
[currentLocale,
|
|
674
|
+
[currentLocale, loader, cookie, refetchMessages, onLocaleChange]
|
|
690
675
|
);
|
|
691
|
-
const value =
|
|
676
|
+
const value = React7__namespace.useMemo(
|
|
692
677
|
() => ({
|
|
693
678
|
locale: currentLocale,
|
|
694
679
|
setLocale
|
|
@@ -698,7 +683,7 @@ function LocaleProvider({
|
|
|
698
683
|
return /* @__PURE__ */ jsxRuntime.jsx(LocaleContext.Provider, { value, children });
|
|
699
684
|
}
|
|
700
685
|
function useLocale() {
|
|
701
|
-
const context =
|
|
686
|
+
const context = React7__namespace.useContext(LocaleContext);
|
|
702
687
|
if (!context)
|
|
703
688
|
throw new Error("useLocale must be used within a LocaleProvider");
|
|
704
689
|
return context;
|
|
@@ -821,7 +806,7 @@ var getI18nContext = async (config) => {
|
|
|
821
806
|
const headersStore = await headers.headers();
|
|
822
807
|
const { defaultLocale, supportedLocales = [], cookie, routing } = config;
|
|
823
808
|
let locale;
|
|
824
|
-
if (
|
|
809
|
+
if (cookie.enabled) {
|
|
825
810
|
const localeFromCookie = cookiesStore.get(cookie.name)?.value;
|
|
826
811
|
locale = normalizeLocale(localeFromCookie, supportedLocales);
|
|
827
812
|
if (locale) {
|
|
@@ -863,61 +848,51 @@ var redirect = async ({
|
|
|
863
848
|
});
|
|
864
849
|
navigation.redirect(localePrefixedPathname, type);
|
|
865
850
|
};
|
|
866
|
-
var TranslateHandlersContext =
|
|
851
|
+
var TranslateHandlersContext = React7__namespace.createContext(void 0);
|
|
867
852
|
var TranslateHandlersProvider = ({
|
|
868
|
-
|
|
869
|
-
|
|
853
|
+
handlers,
|
|
854
|
+
children
|
|
870
855
|
}) => {
|
|
871
|
-
|
|
872
|
-
return /* @__PURE__ */ jsxRuntime.jsx(TranslateHandlersContext.Provider, { value, children });
|
|
856
|
+
return /* @__PURE__ */ jsxRuntime.jsx(TranslateHandlersContext.Provider, { value: { handlers }, children });
|
|
873
857
|
};
|
|
874
858
|
function useTranslateHandlers() {
|
|
875
|
-
const context =
|
|
859
|
+
const context = React7__namespace.useContext(TranslateHandlersContext);
|
|
876
860
|
return context;
|
|
877
861
|
}
|
|
878
|
-
var
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
}, []);
|
|
884
|
-
const isBeforeCSRLoading = lazyLoad && !isCsr;
|
|
885
|
-
return isBeforeCSRLoading;
|
|
886
|
-
};
|
|
887
|
-
var TranslatorContext = React6__namespace.createContext(void 0);
|
|
888
|
-
var EMPTY_OBJECT = Object.freeze({});
|
|
889
|
-
function TranslatorProvider({ children }) {
|
|
862
|
+
var TranslatorContext = React7__namespace.createContext(void 0);
|
|
863
|
+
function TranslatorProvider({
|
|
864
|
+
value: { isLoading: externalIsLoading },
|
|
865
|
+
children
|
|
866
|
+
}) {
|
|
890
867
|
const { config } = useConfig();
|
|
891
|
-
const { messages, isLoading } = useMessages();
|
|
868
|
+
const { messages, isLoading: internalIsLoading } = useMessages();
|
|
892
869
|
const { locale } = useLocale();
|
|
893
|
-
const
|
|
870
|
+
const { handlers } = useTranslateHandlers();
|
|
894
871
|
const { fallbackLocales, translator: translatorOptions } = config;
|
|
895
|
-
const
|
|
896
|
-
const
|
|
897
|
-
|
|
898
|
-
messages
|
|
872
|
+
const isLoading = Boolean(externalIsLoading ?? internalIsLoading);
|
|
873
|
+
const translator = React7__namespace.useMemo(() => {
|
|
874
|
+
return new intorTranslator.Translator({
|
|
875
|
+
messages,
|
|
899
876
|
locale,
|
|
877
|
+
isLoading,
|
|
900
878
|
fallbackLocales,
|
|
901
879
|
loadingMessage: translatorOptions?.loadingMessage,
|
|
902
880
|
placeholder: translatorOptions?.placeholder,
|
|
903
|
-
handlers
|
|
881
|
+
handlers
|
|
904
882
|
});
|
|
905
|
-
translator.setLoading(isBeforeCSRLoading || isLoading);
|
|
906
|
-
return { translator };
|
|
907
883
|
}, [
|
|
908
|
-
fallbackLocales,
|
|
909
|
-
isBeforeCSRLoading,
|
|
910
|
-
isLoading,
|
|
911
|
-
locale,
|
|
912
884
|
messages,
|
|
913
|
-
|
|
885
|
+
locale,
|
|
886
|
+
isLoading,
|
|
887
|
+
fallbackLocales,
|
|
888
|
+
handlers,
|
|
914
889
|
translatorOptions?.loadingMessage,
|
|
915
890
|
translatorOptions?.placeholder
|
|
916
891
|
]);
|
|
917
|
-
return /* @__PURE__ */ jsxRuntime.jsx(TranslatorContext.Provider, { value, children });
|
|
892
|
+
return /* @__PURE__ */ jsxRuntime.jsx(TranslatorContext.Provider, { value: { translator }, children });
|
|
918
893
|
}
|
|
919
894
|
function useTranslator() {
|
|
920
|
-
const context =
|
|
895
|
+
const context = React7__namespace.useContext(TranslatorContext);
|
|
921
896
|
if (!context)
|
|
922
897
|
throw new Error(
|
|
923
898
|
"useTranslator must be used within IntorTranslatorProvider"
|
|
@@ -925,10 +900,17 @@ function useTranslator() {
|
|
|
925
900
|
return context;
|
|
926
901
|
}
|
|
927
902
|
var IntorProvider = ({
|
|
928
|
-
value: {
|
|
903
|
+
value: {
|
|
904
|
+
config,
|
|
905
|
+
pathname = "",
|
|
906
|
+
initialLocale,
|
|
907
|
+
messages = config.messages,
|
|
908
|
+
onLocaleChange,
|
|
909
|
+
isLoading
|
|
910
|
+
},
|
|
929
911
|
children
|
|
930
912
|
}) => {
|
|
931
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ConfigProvider, { value: { config, pathname }, children: /* @__PURE__ */ jsxRuntime.jsx(MessagesProvider, { value: { messages }, children: /* @__PURE__ */ jsxRuntime.jsx(LocaleProvider, { value: { initialLocale }, children: /* @__PURE__ */ jsxRuntime.jsx(TranslatorProvider, { children }) }) }) });
|
|
913
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ConfigProvider, { value: { config, pathname }, children: /* @__PURE__ */ jsxRuntime.jsx(MessagesProvider, { value: { messages }, children: /* @__PURE__ */ jsxRuntime.jsx(LocaleProvider, { value: { initialLocale, onLocaleChange }, children: /* @__PURE__ */ jsxRuntime.jsx(TranslatorProvider, { value: { isLoading }, children }) }) }) });
|
|
932
914
|
};
|
|
933
915
|
|
|
934
916
|
// src/client/react/hooks/use-translator.ts
|
package/dist/next/index.d.cts
CHANGED
|
@@ -88,8 +88,8 @@ declare const useRouter: () => {
|
|
|
88
88
|
};
|
|
89
89
|
|
|
90
90
|
type CookieRawOptions = {
|
|
91
|
-
/**
|
|
92
|
-
|
|
91
|
+
/** Enable cookie usage (read/write) - default: true */
|
|
92
|
+
enabled?: boolean;
|
|
93
93
|
/** Allow the system to automatically set cookies - default: true */
|
|
94
94
|
autoSetCookie?: boolean;
|
|
95
95
|
/** default: "intor.i18n.locale" */
|
|
@@ -137,7 +137,6 @@ type BaseLoaderOptions = {
|
|
|
137
137
|
namespaces?: string[];
|
|
138
138
|
routeNamespaces?: RouteNamespaces;
|
|
139
139
|
concurrency?: number;
|
|
140
|
-
lazyLoad?: boolean;
|
|
141
140
|
};
|
|
142
141
|
type LocalLoader = BaseLoaderOptions & {
|
|
143
142
|
type: "local";
|
|
@@ -174,6 +173,7 @@ type RoutingRawOptions = {
|
|
|
174
173
|
type RoutingResolvedOptions = Required<RoutingRawOptions>;
|
|
175
174
|
|
|
176
175
|
type CacheRawOptions = {
|
|
176
|
+
/** default: process.env.NODE_ENV === "production" */
|
|
177
177
|
enabled?: boolean;
|
|
178
178
|
/** default: 60\*60\*1000 (1 hour) */
|
|
179
179
|
ttl?: number;
|
|
@@ -223,18 +223,20 @@ interface IntorProviderProps {
|
|
|
223
223
|
initialLocale: Locale;
|
|
224
224
|
pathname?: string;
|
|
225
225
|
messages?: Readonly<LocaleMessages>;
|
|
226
|
+
onLocaleChange?: (newLocale: string) => Promise<void> | void;
|
|
227
|
+
isLoading?: boolean;
|
|
226
228
|
};
|
|
227
229
|
children: React.ReactNode;
|
|
228
230
|
}
|
|
229
231
|
|
|
230
|
-
declare const IntorProvider: ({ value: { config, pathname, initialLocale, messages }, children, }: IntorProviderProps) => react_jsx_runtime.JSX.Element;
|
|
232
|
+
declare const IntorProvider: ({ value: { config, pathname, initialLocale, messages, onLocaleChange, isLoading, }, children, }: IntorProviderProps) => react_jsx_runtime.JSX.Element;
|
|
231
233
|
|
|
232
234
|
type TranslateHandlersProviderProps = {
|
|
233
|
-
children: React.ReactNode;
|
|
234
235
|
handlers: TranslateHandlers;
|
|
236
|
+
children: React.ReactNode;
|
|
235
237
|
};
|
|
236
238
|
|
|
237
|
-
declare const TranslateHandlersProvider: ({
|
|
239
|
+
declare const TranslateHandlersProvider: ({ handlers, children, }: TranslateHandlersProviderProps) => react_jsx_runtime.JSX.Element;
|
|
238
240
|
|
|
239
241
|
/** Base properties shared by all translator instances. */
|
|
240
242
|
interface TranslatorBaseProps<M = unknown> {
|
package/dist/next/index.d.ts
CHANGED
|
@@ -88,8 +88,8 @@ declare const useRouter: () => {
|
|
|
88
88
|
};
|
|
89
89
|
|
|
90
90
|
type CookieRawOptions = {
|
|
91
|
-
/**
|
|
92
|
-
|
|
91
|
+
/** Enable cookie usage (read/write) - default: true */
|
|
92
|
+
enabled?: boolean;
|
|
93
93
|
/** Allow the system to automatically set cookies - default: true */
|
|
94
94
|
autoSetCookie?: boolean;
|
|
95
95
|
/** default: "intor.i18n.locale" */
|
|
@@ -137,7 +137,6 @@ type BaseLoaderOptions = {
|
|
|
137
137
|
namespaces?: string[];
|
|
138
138
|
routeNamespaces?: RouteNamespaces;
|
|
139
139
|
concurrency?: number;
|
|
140
|
-
lazyLoad?: boolean;
|
|
141
140
|
};
|
|
142
141
|
type LocalLoader = BaseLoaderOptions & {
|
|
143
142
|
type: "local";
|
|
@@ -174,6 +173,7 @@ type RoutingRawOptions = {
|
|
|
174
173
|
type RoutingResolvedOptions = Required<RoutingRawOptions>;
|
|
175
174
|
|
|
176
175
|
type CacheRawOptions = {
|
|
176
|
+
/** default: process.env.NODE_ENV === "production" */
|
|
177
177
|
enabled?: boolean;
|
|
178
178
|
/** default: 60\*60\*1000 (1 hour) */
|
|
179
179
|
ttl?: number;
|
|
@@ -223,18 +223,20 @@ interface IntorProviderProps {
|
|
|
223
223
|
initialLocale: Locale;
|
|
224
224
|
pathname?: string;
|
|
225
225
|
messages?: Readonly<LocaleMessages>;
|
|
226
|
+
onLocaleChange?: (newLocale: string) => Promise<void> | void;
|
|
227
|
+
isLoading?: boolean;
|
|
226
228
|
};
|
|
227
229
|
children: React.ReactNode;
|
|
228
230
|
}
|
|
229
231
|
|
|
230
|
-
declare const IntorProvider: ({ value: { config, pathname, initialLocale, messages }, children, }: IntorProviderProps) => react_jsx_runtime.JSX.Element;
|
|
232
|
+
declare const IntorProvider: ({ value: { config, pathname, initialLocale, messages, onLocaleChange, isLoading, }, children, }: IntorProviderProps) => react_jsx_runtime.JSX.Element;
|
|
231
233
|
|
|
232
234
|
type TranslateHandlersProviderProps = {
|
|
233
|
-
children: React.ReactNode;
|
|
234
235
|
handlers: TranslateHandlers;
|
|
236
|
+
children: React.ReactNode;
|
|
235
237
|
};
|
|
236
238
|
|
|
237
|
-
declare const TranslateHandlersProvider: ({
|
|
239
|
+
declare const TranslateHandlersProvider: ({ handlers, children, }: TranslateHandlersProviderProps) => react_jsx_runtime.JSX.Element;
|
|
238
240
|
|
|
239
241
|
/** Base properties shared by all translator instances. */
|
|
240
242
|
interface TranslatorBaseProps<M = unknown> {
|