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.
@@ -1,14 +1,15 @@
1
1
  'use strict';
2
2
 
3
- var React6 = require('react');
4
- var logry = require('logry');
5
- var Keyv = require('keyv');
6
- var merge = require('lodash.merge');
7
- var intorTranslator = require('intor-translator');
8
3
  var formatUrl = require('next/dist/shared/lib/router/utils/format-url');
9
4
  var NextLink = require('next/link');
5
+ var React6 = require('react');
10
6
  var navigation = require('next/navigation');
7
+ var merge = require('lodash.merge');
8
+ var jsxRuntime = require('react/jsx-runtime');
9
+ var logry = require('logry');
10
+ var Keyv = require('keyv');
11
11
  var headers = require('next/headers');
12
+ var intorTranslator = require('intor-translator');
12
13
 
13
14
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
14
15
 
@@ -30,98 +31,15 @@ function _interopNamespace(e) {
30
31
  return Object.freeze(n);
31
32
  }
32
33
 
34
+ var NextLink__default = /*#__PURE__*/_interopDefault(NextLink);
33
35
  var React6__namespace = /*#__PURE__*/_interopNamespace(React6);
34
- var Keyv__default = /*#__PURE__*/_interopDefault(Keyv);
35
36
  var merge__default = /*#__PURE__*/_interopDefault(merge);
36
- var NextLink__default = /*#__PURE__*/_interopDefault(NextLink);
37
-
38
- // src/adapters/next/contexts/intor-provider/intor-provider.tsx
39
- var ConfigContext = React6__namespace.createContext(void 0);
40
-
41
- // src/adapters/next/contexts/config/provider.tsx
42
- function ConfigProvider({
43
- value: { config, pathname },
44
- children
45
- }) {
46
- const value = React6__namespace.useMemo(() => ({ config, pathname }), [config, pathname]);
47
- return /* @__PURE__ */ React6__namespace.createElement(ConfigContext.Provider, { value }, children);
48
- }
49
- function useConfig() {
50
- const context = React6__namespace.useContext(ConfigContext);
51
- if (!context) throw new Error("useConfig must be used within ConfigProvider");
52
- return context;
53
- }
54
-
55
- // src/modules/config/constants/cache.constants.ts
56
- var DEFAULT_CACHE_OPTIONS = {
57
- enabled: process.env.NODE_ENV === "production",
58
- ttl: 60 * 60 * 1e3
59
- // 1 hour
60
- };
61
-
62
- // src/shared/logger/global-logger-pool.ts
63
- function getGlobalLoggerPool() {
64
- if (!globalThis.__INTOR_LOGGER_POOL__) {
65
- globalThis.__INTOR_LOGGER_POOL__ = /* @__PURE__ */ new Map();
66
- }
67
- return globalThis.__INTOR_LOGGER_POOL__;
68
- }
37
+ var Keyv__default = /*#__PURE__*/_interopDefault(Keyv);
69
38
 
70
- // src/shared/logger/get-logger.ts
71
- var DEFAULT_FORMATTER_CONFIG = {
72
- node: { meta: { compact: true }, lineBreaksAfter: 1 }
73
- };
74
- function getLogger({
75
- id = "default",
76
- formatterConfig,
77
- preset,
78
- ...options
79
- }) {
80
- const pool = getGlobalLoggerPool();
81
- let logger = pool.get(id);
82
- const useDefault = !formatterConfig && !preset;
83
- if (!logger) {
84
- logger = logry.logry({
85
- id,
86
- formatterConfig: useDefault ? DEFAULT_FORMATTER_CONFIG : formatterConfig,
87
- preset,
88
- ...options
89
- });
90
- pool.set(id, logger);
91
- if (pool.size > 1e3) {
92
- const keys = [...pool.keys()];
93
- for (const key of keys.slice(0, 200)) pool.delete(key);
94
- }
95
- }
96
- return logger;
97
- }
39
+ // src/adapters/next/navigation/link.tsx
98
40
 
99
- // src/modules/messages/shared/utils/is-valid-messages.ts
100
- function isPlainObject(value) {
101
- return typeof value === "object" && value !== null && !Array.isArray(value);
102
- }
103
- function isValidMessages(value) {
104
- if (!isPlainObject(value)) return false;
105
- const stack = [value];
106
- while (stack.length > 0) {
107
- const current = stack.pop();
108
- for (const v of Object.values(current)) {
109
- if (typeof v === "string") continue;
110
- if (isPlainObject(v)) {
111
- stack.push(v);
112
- } else {
113
- return false;
114
- }
115
- }
116
- }
117
- return true;
118
- }
119
- function getGlobalMessagesPool() {
120
- if (!globalThis.__INTOR_MESSAGES_POOL__) {
121
- globalThis.__INTOR_MESSAGES_POOL__ = new Keyv__default.default();
122
- }
123
- return globalThis.__INTOR_MESSAGES_POOL__;
124
- }
41
+ // src/shared/constants/prefix-placeholder.ts
42
+ var PREFIX_PLACEHOLDER = "{locale}";
125
43
  var mergeMessages = (staticMessages = {}, loadedMessages = {}) => {
126
44
  if (!loadedMessages) return { ...staticMessages };
127
45
  return merge__default.default({}, staticMessages, loadedMessages);
@@ -146,9 +64,6 @@ var normalizeCacheKey = (key, delimiter = CACHE_KEY_DELIMITER) => {
146
64
  return String(key);
147
65
  };
148
66
 
149
- // src/shared/constants/prefix-placeholder.ts
150
- var PREFIX_PLACEHOLDER = "{locale}";
151
-
152
67
  // src/shared/utils/resolve-namespaces.ts
153
68
  var resolveNamespaces = ({
154
69
  config,
@@ -313,7 +228,145 @@ var standardizePathname = ({
313
228
  return normalizePathname(standardizedPathname);
314
229
  };
315
230
 
316
- // src/modules/messages/load-remote-messages/fetch-locale-messages/fetch-locale-messages.ts
231
+ // src/adapters/next/shared/utils/locale-prefix-pathname.ts
232
+ var localePrefixPathname = ({
233
+ config,
234
+ pathname: standardizedPathname,
235
+ locale
236
+ }) => {
237
+ const { routing } = config;
238
+ const { prefix } = routing;
239
+ if (prefix !== "none" && !locale) {
240
+ throw new Error('No locale when using prefix "all", "except-default"');
241
+ }
242
+ if (prefix === "all") {
243
+ return normalizePathname(
244
+ standardizedPathname.replaceAll(PREFIX_PLACEHOLDER, locale)
245
+ );
246
+ }
247
+ if (prefix === "except-default") {
248
+ return locale === config.defaultLocale ? normalizePathname(
249
+ standardizedPathname.replaceAll(`/${PREFIX_PLACEHOLDER}`, "")
250
+ ) : normalizePathname(
251
+ standardizedPathname.replaceAll(PREFIX_PLACEHOLDER, locale)
252
+ );
253
+ }
254
+ return normalizePathname(
255
+ standardizedPathname.replaceAll(`/${PREFIX_PLACEHOLDER}`, "")
256
+ );
257
+ };
258
+
259
+ // src/adapters/next/shared/utils/localize-pathname.ts
260
+ var localizePathname = ({
261
+ config,
262
+ pathname: rawPathname,
263
+ locale
264
+ }) => {
265
+ const { unprefixedPathname } = extractPathname({
266
+ config,
267
+ pathname: rawPathname
268
+ });
269
+ const standardizedPathname = standardizePathname({
270
+ config,
271
+ pathname: unprefixedPathname
272
+ });
273
+ const localePrefixedPathname = localePrefixPathname({
274
+ config,
275
+ pathname: standardizedPathname,
276
+ locale
277
+ });
278
+ return {
279
+ unprefixedPathname,
280
+ standardizedPathname,
281
+ localePrefixedPathname
282
+ };
283
+ };
284
+ var ConfigContext = React6__namespace.createContext(void 0);
285
+ function ConfigProvider({
286
+ value: { config, pathname },
287
+ children
288
+ }) {
289
+ const value = React6__namespace.useMemo(() => ({ config, pathname }), [config, pathname]);
290
+ return /* @__PURE__ */ jsxRuntime.jsx(ConfigContext.Provider, { value, children });
291
+ }
292
+ function useConfig() {
293
+ const context = React6__namespace.useContext(ConfigContext);
294
+ if (!context) throw new Error("useConfig must be used within ConfigProvider");
295
+ return context;
296
+ }
297
+
298
+ // src/config/constants/cache.constants.ts
299
+ var DEFAULT_CACHE_OPTIONS = {
300
+ enabled: process.env.NODE_ENV === "production",
301
+ ttl: 60 * 60 * 1e3
302
+ // 1 hour
303
+ };
304
+
305
+ // src/server/shared/logger/global-logger-pool.ts
306
+ function getGlobalLoggerPool() {
307
+ if (!globalThis.__INTOR_LOGGER_POOL__) {
308
+ globalThis.__INTOR_LOGGER_POOL__ = /* @__PURE__ */ new Map();
309
+ }
310
+ return globalThis.__INTOR_LOGGER_POOL__;
311
+ }
312
+
313
+ // src/server/shared/logger/get-logger.ts
314
+ var DEFAULT_FORMATTER_CONFIG = {
315
+ node: { meta: { compact: true }, lineBreaksAfter: 1 }
316
+ };
317
+ function getLogger({
318
+ id = "default",
319
+ formatterConfig,
320
+ preset,
321
+ ...options
322
+ }) {
323
+ const pool = getGlobalLoggerPool();
324
+ let logger = pool.get(id);
325
+ const useDefault = !formatterConfig && !preset;
326
+ if (!logger) {
327
+ logger = logry.logry({
328
+ id,
329
+ formatterConfig: useDefault ? DEFAULT_FORMATTER_CONFIG : formatterConfig,
330
+ preset,
331
+ ...options
332
+ });
333
+ pool.set(id, logger);
334
+ if (pool.size > 1e3) {
335
+ const keys = [...pool.keys()];
336
+ for (const key of keys.slice(0, 200)) pool.delete(key);
337
+ }
338
+ }
339
+ return logger;
340
+ }
341
+
342
+ // src/server/messages/shared/utils/is-valid-messages.ts
343
+ function isPlainObject(value) {
344
+ return typeof value === "object" && value !== null && !Array.isArray(value);
345
+ }
346
+ function isValidMessages(value) {
347
+ if (!isPlainObject(value)) return false;
348
+ const stack = [value];
349
+ while (stack.length > 0) {
350
+ const current = stack.pop();
351
+ for (const v of Object.values(current)) {
352
+ if (typeof v === "string") continue;
353
+ if (isPlainObject(v)) {
354
+ stack.push(v);
355
+ } else {
356
+ return false;
357
+ }
358
+ }
359
+ }
360
+ return true;
361
+ }
362
+ function getGlobalMessagesPool() {
363
+ if (!globalThis.__INTOR_MESSAGES_POOL__) {
364
+ globalThis.__INTOR_MESSAGES_POOL__ = new Keyv__default.default();
365
+ }
366
+ return globalThis.__INTOR_MESSAGES_POOL__;
367
+ }
368
+
369
+ // src/server/messages/load-remote-messages/fetch-locale-messages/fetch-locale-messages.ts
317
370
  var fetchLocaleMessages = async ({
318
371
  remoteUrl,
319
372
  remoteHeaders,
@@ -355,7 +408,7 @@ var fetchLocaleMessages = async ({
355
408
  }
356
409
  };
357
410
 
358
- // src/modules/messages/load-remote-messages/fetch-locale-messages/utils/build-search-params.ts
411
+ // src/server/messages/load-remote-messages/fetch-locale-messages/utils/build-search-params.ts
359
412
  var buildSearchParams = (params) => {
360
413
  const searchParams = new URLSearchParams();
361
414
  const appendParam = (key, value) => {
@@ -373,7 +426,7 @@ var buildSearchParams = (params) => {
373
426
  return searchParams;
374
427
  };
375
428
 
376
- // src/modules/messages/load-remote-messages/load-remote-messages.ts
429
+ // src/server/messages/load-remote-messages/load-remote-messages.ts
377
430
  var loadRemoteMessages = async ({
378
431
  pool = getGlobalMessagesPool(),
379
432
  rootDir,
@@ -442,7 +495,7 @@ var loadRemoteMessages = async ({
442
495
  return messages;
443
496
  };
444
497
 
445
- // src/adapters/next/contexts/messages/utils/use-refetch-messages.ts
498
+ // src/client/react/contexts/messages/utils/use-refetch-messages.ts
446
499
  var useRefetchMessages = ({
447
500
  config,
448
501
  pathname,
@@ -488,10 +541,8 @@ var useRefetchMessages = ({
488
541
  return { refetchMessages };
489
542
  };
490
543
  var MessagesContext = React6__namespace.createContext(void 0);
491
-
492
- // src/adapters/next/contexts/messages/provider.tsx
493
544
  function MessagesProvider({
494
- value: { messages },
545
+ value: { messages = {} },
495
546
  children
496
547
  }) {
497
548
  const { config, pathname } = useConfig();
@@ -513,7 +564,7 @@ function MessagesProvider({
513
564
  }),
514
565
  [loadedMessages, messages, isLoadingMessages, refetchMessages]
515
566
  );
516
- return /* @__PURE__ */ React6__namespace.createElement(MessagesContext.Provider, { value }, children);
567
+ return /* @__PURE__ */ jsxRuntime.jsx(MessagesContext.Provider, { value, children });
517
568
  }
518
569
  function useMessages() {
519
570
  const context = React6__namespace.useContext(MessagesContext);
@@ -522,7 +573,7 @@ function useMessages() {
522
573
  return context;
523
574
  }
524
575
 
525
- // src/adapters/next/contexts/locale/utils/use-init-lazy-load.ts
576
+ // src/client/react/contexts/locale/utils/use-init-lazy-load.ts
526
577
  var useInitLazyLoad = ({
527
578
  loaderOptions,
528
579
  currentLocale
@@ -538,7 +589,7 @@ var useInitLazyLoad = ({
538
589
  }, [lazyLoad, currentLocale, refetchMessages, isFirstLoadedRef]);
539
590
  };
540
591
 
541
- // src/adapters/next/shared/utils/build-cookie-string.ts
592
+ // src/shared/utils/client/build-cookie-string.ts
542
593
  var buildCookieString = (cookie, locale) => {
543
594
  const parts = [`${cookie.name}=${encodeURIComponent(locale)}`];
544
595
  if (cookie.maxAge) {
@@ -560,7 +611,7 @@ var buildCookieString = (cookie, locale) => {
560
611
  return parts.join("; ");
561
612
  };
562
613
 
563
- // src/adapters/next/shared/utils/set-locale-cookie-browser.ts
614
+ // src/shared/utils/client/set-locale-cookie-browser.ts
564
615
  var setLocaleCookieBrowser = ({
565
616
  cookie,
566
617
  locale
@@ -571,7 +622,7 @@ var setLocaleCookieBrowser = ({
571
622
  document.cookie = cookieString;
572
623
  };
573
624
 
574
- // src/adapters/next/contexts/locale/utils/use-init-locale-cookie.ts
625
+ // src/client/react/contexts/locale/utils/use-init-locale-cookie.ts
575
626
  var useInitLocaleCookie = ({
576
627
  config,
577
628
  locale
@@ -590,7 +641,7 @@ var useInitLocaleCookie = ({
590
641
  };
591
642
  var LocaleContext = React6__namespace.createContext(void 0);
592
643
 
593
- // src/adapters/next/contexts/locale/utils/change-locale.ts
644
+ // src/client/react/contexts/locale/utils/change-locale.ts
594
645
  var changeLocale = ({
595
646
  currentLocale,
596
647
  newLocale,
@@ -614,8 +665,6 @@ var changeLocale = ({
614
665
  void refetchMessages(newLocale);
615
666
  }
616
667
  };
617
-
618
- // src/adapters/next/contexts/locale/provider.tsx
619
668
  function LocaleProvider({
620
669
  value: { initialLocale },
621
670
  children
@@ -646,7 +695,7 @@ function LocaleProvider({
646
695
  }),
647
696
  [currentLocale, setLocale]
648
697
  );
649
- return /* @__PURE__ */ React6__namespace.createElement(LocaleContext.Provider, { value }, children);
698
+ return /* @__PURE__ */ jsxRuntime.jsx(LocaleContext.Provider, { value, children });
650
699
  }
651
700
  function useLocale() {
652
701
  const context = React6__namespace.useContext(LocaleContext);
@@ -654,163 +703,22 @@ function useLocale() {
654
703
  throw new Error("useLocale must be used within a LocaleProvider");
655
704
  return context;
656
705
  }
657
- var TranslateHandlersContext = React6__namespace.createContext(void 0);
658
-
659
- // src/adapters/next/contexts/translate-handlers/provider.tsx
660
- var TranslateHandlersProvider = ({
661
- children,
662
- handlers
663
- }) => {
664
- const value = handlers;
665
- return /* @__PURE__ */ React6__namespace.createElement(TranslateHandlersContext.Provider, { value }, children);
666
- };
667
- function useTranslateHandlers() {
668
- const context = React6__namespace.useContext(TranslateHandlersContext);
669
- return context;
670
- }
671
- var useInitLoadingState = (config) => {
672
- const lazyLoad = !!config.loader?.lazyLoad;
673
- const [isCsr, setIsCsr] = React6__namespace.useState(false);
674
- React6__namespace.useEffect(() => {
675
- setIsCsr(true);
676
- }, []);
677
- const isBeforeCSRLoading = lazyLoad && !isCsr;
678
- return isBeforeCSRLoading;
679
- };
680
- var TranslatorContext = React6__namespace.createContext(void 0);
681
-
682
- // src/adapters/next/contexts/translator/provider.tsx
683
- var EMPTY_OBJECT = Object.freeze({});
684
- function TranslatorProvider({ children }) {
685
- const { config } = useConfig();
686
- const { messages, isLoading } = useMessages();
687
- const { locale } = useLocale();
688
- const translatorHandlers = useTranslateHandlers();
689
- const { fallbackLocales, translator: translatorOptions } = config;
690
- const isBeforeCSRLoading = useInitLoadingState(config);
691
- const value = React6__namespace.useMemo(() => {
692
- const translator = new intorTranslator.Translator({
693
- messages: messages || EMPTY_OBJECT,
694
- locale,
695
- fallbackLocales,
696
- loadingMessage: translatorOptions?.loadingMessage,
697
- placeholder: translatorOptions?.placeholder,
698
- handlers: translatorHandlers
699
- });
700
- translator.setLoading(isBeforeCSRLoading || isLoading);
701
- return { translator };
702
- }, [
703
- fallbackLocales,
704
- isBeforeCSRLoading,
705
- isLoading,
706
- locale,
707
- messages,
708
- translatorHandlers,
709
- translatorOptions?.loadingMessage,
710
- translatorOptions?.placeholder
711
- ]);
712
- return /* @__PURE__ */ React6__namespace.createElement(TranslatorContext.Provider, { value }, children);
713
- }
714
- function useTranslator() {
715
- const context = React6__namespace.useContext(TranslatorContext);
716
- if (!context)
717
- throw new Error(
718
- "useTranslator must be used within IntorTranslatorProvider"
719
- );
720
- return context;
721
- }
722
-
723
- // src/adapters/next/contexts/intor-provider/intor-provider.tsx
724
- var IntorProvider = ({
725
- value: { config, pathname, initialLocale, messages },
726
- children
727
- }) => {
728
- return /* @__PURE__ */ React6__namespace.createElement(ConfigProvider, { value: { config, pathname } }, /* @__PURE__ */ React6__namespace.createElement(MessagesProvider, { value: { messages } }, /* @__PURE__ */ React6__namespace.createElement(LocaleProvider, { value: { initialLocale } }, /* @__PURE__ */ React6__namespace.createElement(TranslatorProvider, null, children))));
729
- };
730
-
731
- // src/adapters/next/hooks/use-translator/use-translator.ts
732
- function useTranslator2(preKey) {
733
- const { translator } = useTranslator();
734
- const { setLocale } = useLocale();
735
- const props = {
736
- messages: translator.messages,
737
- locale: translator.locale,
738
- isLoading: translator.isLoading,
739
- setLocale
740
- };
741
- const scoped = translator.scoped(preKey);
742
- return {
743
- ...props,
744
- hasKey: preKey ? scoped.hasKey : translator.hasKey,
745
- t: preKey ? scoped.t : translator.t
746
- };
747
- }
748
-
749
- // src/adapters/next/shared/utils/locale-prefix-pathname.ts
750
- var localePrefixPathname = ({
751
- config,
752
- pathname: standardizedPathname,
753
- locale
754
- }) => {
755
- const { routing } = config;
756
- const { prefix } = routing;
757
- if (prefix !== "none" && !locale) {
758
- throw new Error('No locale when using prefix "all", "except-default"');
759
- }
760
- if (prefix === "all") {
761
- return normalizePathname(
762
- standardizedPathname.replaceAll(PREFIX_PLACEHOLDER, locale)
763
- );
764
- }
765
- if (prefix === "except-default") {
766
- return locale === config.defaultLocale ? normalizePathname(
767
- standardizedPathname.replaceAll(`/${PREFIX_PLACEHOLDER}`, "")
768
- ) : normalizePathname(
769
- standardizedPathname.replaceAll(PREFIX_PLACEHOLDER, locale)
770
- );
771
- }
772
- return normalizePathname(
773
- standardizedPathname.replaceAll(`/${PREFIX_PLACEHOLDER}`, "")
774
- );
775
- };
776
-
777
- // src/adapters/next/shared/utils/localize-pathname.ts
778
- var localizePathname = ({
779
- config,
780
- pathname: rawPathname,
781
- locale
782
- }) => {
783
- const { unprefixedPathname } = extractPathname({
784
- config,
785
- pathname: rawPathname
786
- });
787
- const standardizedPathname = standardizePathname({
788
- config,
789
- pathname: unprefixedPathname
790
- });
791
- const localePrefixedPathname = localePrefixPathname({
792
- config,
793
- pathname: standardizedPathname,
794
- locale
795
- });
796
- return {
797
- unprefixedPathname,
798
- standardizedPathname,
799
- localePrefixedPathname
800
- };
801
- };
802
706
 
803
707
  // src/adapters/next/navigation/use-pathname.ts
804
708
  var usePathname = () => {
805
709
  const { config } = useConfig();
806
710
  const { locale } = useLocale();
807
711
  const rawPathname = navigation.usePathname();
808
- const { localePrefixedPathname } = localizePathname({
712
+ const { localePrefixedPathname, standardizedPathname, unprefixedPathname } = localizePathname({
809
713
  config,
810
714
  pathname: rawPathname,
811
715
  locale
812
716
  });
813
- return localePrefixedPathname;
717
+ return {
718
+ localizedPathname: localePrefixedPathname,
719
+ standardizedPathname,
720
+ unprefixedPathname
721
+ };
814
722
  };
815
723
 
816
724
  // src/adapters/next/navigation/utils/should-full-reload.ts
@@ -835,14 +743,14 @@ var shouldFullReload = ({
835
743
  var useLocaleSwitch = () => {
836
744
  const { config } = useConfig();
837
745
  const { locale: currentLocale, setLocale } = useLocale();
838
- const pathname = usePathname();
746
+ const { localizedPathname } = usePathname();
839
747
  const resolveHref = ({
840
748
  href,
841
749
  locale
842
750
  }) => {
843
751
  const isLocaleValid = locale && config.supportedLocales?.includes(locale);
844
752
  const targetLocale = isLocaleValid ? locale : currentLocale;
845
- const targetPathname = href ?? pathname;
753
+ const targetPathname = href ?? localizedPathname;
846
754
  const isExternal = targetPathname.startsWith("http");
847
755
  const resolvedHref = !isExternal ? localizePathname({
848
756
  config,
@@ -868,8 +776,6 @@ var useLocaleSwitch = () => {
868
776
  };
869
777
  return { resolveHref, switchLocale };
870
778
  };
871
-
872
- // src/adapters/next/navigation/link.tsx
873
779
  var Link = ({
874
780
  href,
875
781
  locale,
@@ -884,7 +790,7 @@ var Link = ({
884
790
  onClick?.(e);
885
791
  switchLocale({ href: formatted, locale });
886
792
  };
887
- return /* @__PURE__ */ React6__namespace.createElement(NextLink__default.default, { href: resolvedHref, onClick: handleClick, ...props }, children);
793
+ return /* @__PURE__ */ jsxRuntime.jsx(NextLink__default.default, { href: resolvedHref, onClick: handleClick, ...props, children });
888
794
  };
889
795
  var useRouter = () => {
890
796
  const { push, replace, ...rest } = navigation.useRouter();
@@ -957,10 +863,94 @@ var redirect = async ({
957
863
  });
958
864
  navigation.redirect(localePrefixedPathname, type);
959
865
  };
866
+ var TranslateHandlersContext = React6__namespace.createContext(void 0);
867
+ var TranslateHandlersProvider = ({
868
+ children,
869
+ handlers
870
+ }) => {
871
+ const value = handlers;
872
+ return /* @__PURE__ */ jsxRuntime.jsx(TranslateHandlersContext.Provider, { value, children });
873
+ };
874
+ function useTranslateHandlers() {
875
+ const context = React6__namespace.useContext(TranslateHandlersContext);
876
+ return context;
877
+ }
878
+ var useInitLoadingState = (config) => {
879
+ const lazyLoad = !!config.loader?.lazyLoad;
880
+ const [isCsr, setIsCsr] = React6__namespace.useState(false);
881
+ React6__namespace.useEffect(() => {
882
+ setIsCsr(true);
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 }) {
890
+ const { config } = useConfig();
891
+ const { messages, isLoading } = useMessages();
892
+ const { locale } = useLocale();
893
+ const translatorHandlers = useTranslateHandlers();
894
+ const { fallbackLocales, translator: translatorOptions } = config;
895
+ const isBeforeCSRLoading = useInitLoadingState(config);
896
+ const value = React6__namespace.useMemo(() => {
897
+ const translator = new intorTranslator.Translator({
898
+ messages: messages || EMPTY_OBJECT,
899
+ locale,
900
+ fallbackLocales,
901
+ loadingMessage: translatorOptions?.loadingMessage,
902
+ placeholder: translatorOptions?.placeholder,
903
+ handlers: translatorHandlers
904
+ });
905
+ translator.setLoading(isBeforeCSRLoading || isLoading);
906
+ return { translator };
907
+ }, [
908
+ fallbackLocales,
909
+ isBeforeCSRLoading,
910
+ isLoading,
911
+ locale,
912
+ messages,
913
+ translatorHandlers,
914
+ translatorOptions?.loadingMessage,
915
+ translatorOptions?.placeholder
916
+ ]);
917
+ return /* @__PURE__ */ jsxRuntime.jsx(TranslatorContext.Provider, { value, children });
918
+ }
919
+ function useTranslator() {
920
+ const context = React6__namespace.useContext(TranslatorContext);
921
+ if (!context)
922
+ throw new Error(
923
+ "useTranslator must be used within IntorTranslatorProvider"
924
+ );
925
+ return context;
926
+ }
927
+ var IntorProvider = ({
928
+ value: { config, pathname = "", initialLocale, messages = config.messages },
929
+ children
930
+ }) => {
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 }) }) }) });
932
+ };
933
+
934
+ // src/client/react/hooks/use-translator.ts
935
+ function useTranslator2(preKey) {
936
+ const { translator } = useTranslator();
937
+ const { setLocale } = useLocale();
938
+ const props = {
939
+ messages: translator.messages,
940
+ locale: translator.locale,
941
+ isLoading: translator.isLoading,
942
+ setLocale
943
+ };
944
+ const scoped = translator.scoped(preKey);
945
+ return {
946
+ ...props,
947
+ hasKey: preKey ? scoped.hasKey : translator.hasKey,
948
+ t: preKey ? scoped.t : translator.t
949
+ };
950
+ }
960
951
 
961
952
  exports.IntorProvider = IntorProvider;
962
953
  exports.Link = Link;
963
- exports.PATHNAME_HEADER_NAME = PATHNAME_HEADER_NAME;
964
954
  exports.TranslateHandlersProvider = TranslateHandlersProvider;
965
955
  exports.redirect = redirect;
966
956
  exports.usePathname = usePathname;