intor 2.2.1 → 2.2.3
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.d.cts +9 -9
- package/dist/config/index.d.ts +9 -9
- package/dist/index.cjs +307 -453
- package/dist/index.d.cts +109 -51
- package/dist/index.d.ts +109 -51
- package/dist/index.js +309 -456
- package/dist/next/index.cjs +141 -148
- package/dist/next/index.d.cts +9 -9
- package/dist/next/index.d.ts +9 -9
- package/dist/next/index.js +139 -147
- package/dist/next/middleware/index.d.cts +9 -9
- package/dist/next/middleware/index.d.ts +9 -9
- package/dist/next/server/index.cjs +287 -430
- package/dist/next/server/index.d.cts +9 -9
- package/dist/next/server/index.d.ts +9 -9
- package/dist/next/server/index.js +289 -433
- package/package.json +6 -12
package/dist/next/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as React6 from 'react';
|
|
2
2
|
import { logry } from 'logry';
|
|
3
3
|
import Keyv from 'keyv';
|
|
4
|
+
import merge from 'lodash.merge';
|
|
4
5
|
import { Translator } from 'intor-translator';
|
|
5
6
|
import { formatUrl } from 'next/dist/shared/lib/router/utils/format-url';
|
|
6
7
|
import NextLink from 'next/link';
|
|
@@ -8,21 +9,21 @@ import { usePathname as usePathname$1, useRouter as useRouter$1, redirect as red
|
|
|
8
9
|
import { cookies, headers } from 'next/headers';
|
|
9
10
|
|
|
10
11
|
// src/adapters/next/contexts/intor-provider/intor-provider.tsx
|
|
11
|
-
var ConfigContext =
|
|
12
|
+
var ConfigContext = React6.createContext(void 0);
|
|
13
|
+
|
|
14
|
+
// src/adapters/next/contexts/config/provider.tsx
|
|
12
15
|
function ConfigProvider({
|
|
13
16
|
value: { config, pathname },
|
|
14
17
|
children
|
|
15
18
|
}) {
|
|
16
|
-
const value =
|
|
17
|
-
return /* @__PURE__ */
|
|
19
|
+
const value = React6.useMemo(() => ({ config, pathname }), [config, pathname]);
|
|
20
|
+
return /* @__PURE__ */ React6.createElement(ConfigContext.Provider, { value }, children);
|
|
18
21
|
}
|
|
19
22
|
function useConfig() {
|
|
20
|
-
const context =
|
|
23
|
+
const context = React6.useContext(ConfigContext);
|
|
21
24
|
if (!context) throw new Error("useConfig must be used within ConfigProvider");
|
|
22
25
|
return context;
|
|
23
26
|
}
|
|
24
|
-
var LocaleContext = React7.createContext(void 0);
|
|
25
|
-
var MessagesContext = React7.createContext(void 0);
|
|
26
27
|
|
|
27
28
|
// src/modules/config/constants/cache.constants.ts
|
|
28
29
|
var DEFAULT_CACHE_OPTIONS = {
|
|
@@ -44,17 +45,18 @@ var DEFAULT_FORMATTER_CONFIG = {
|
|
|
44
45
|
node: { meta: { compact: true }, lineBreaksAfter: 1 }
|
|
45
46
|
};
|
|
46
47
|
function getLogger({
|
|
47
|
-
id,
|
|
48
|
+
id = "default",
|
|
48
49
|
formatterConfig,
|
|
49
50
|
preset,
|
|
50
51
|
...options
|
|
51
52
|
}) {
|
|
52
53
|
const pool = getGlobalLoggerPool();
|
|
53
54
|
let logger = pool.get(id);
|
|
55
|
+
const useDefault = !formatterConfig && !preset;
|
|
54
56
|
if (!logger) {
|
|
55
57
|
logger = logry({
|
|
56
58
|
id,
|
|
57
|
-
formatterConfig:
|
|
59
|
+
formatterConfig: useDefault ? DEFAULT_FORMATTER_CONFIG : formatterConfig,
|
|
58
60
|
preset,
|
|
59
61
|
...options
|
|
60
62
|
});
|
|
@@ -66,28 +68,36 @@ function getLogger({
|
|
|
66
68
|
}
|
|
67
69
|
return logger;
|
|
68
70
|
}
|
|
71
|
+
|
|
72
|
+
// src/modules/messages/shared/utils/is-valid-messages.ts
|
|
73
|
+
function isPlainObject(value) {
|
|
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
|
+
}
|
|
69
92
|
function getGlobalMessagesPool() {
|
|
70
93
|
if (!globalThis.__INTOR_MESSAGES_POOL__) {
|
|
71
94
|
globalThis.__INTOR_MESSAGES_POOL__ = new Keyv();
|
|
72
95
|
}
|
|
73
96
|
return globalThis.__INTOR_MESSAGES_POOL__;
|
|
74
97
|
}
|
|
75
|
-
|
|
76
|
-
// src/shared/utils/merge-messages.ts
|
|
77
98
|
var mergeMessages = (staticMessages = {}, loadedMessages = {}) => {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const loaded = loadedMessages[locale];
|
|
81
|
-
if (!result[locale]) {
|
|
82
|
-
result[locale] = loaded;
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
result[locale] = {
|
|
86
|
-
...result[locale],
|
|
87
|
-
...loaded
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
return result;
|
|
99
|
+
if (!loadedMessages) return { ...staticMessages };
|
|
100
|
+
return merge({}, staticMessages, loadedMessages);
|
|
91
101
|
};
|
|
92
102
|
|
|
93
103
|
// src/shared/utils/normalize-cache-key.ts
|
|
@@ -119,6 +129,8 @@ var resolveNamespaces = ({
|
|
|
119
129
|
}) => {
|
|
120
130
|
const { loader } = config;
|
|
121
131
|
const { routeNamespaces = {}, namespaces } = loader || {};
|
|
132
|
+
if (Object.keys(routeNamespaces).length === 0 && !namespaces)
|
|
133
|
+
return void 0;
|
|
122
134
|
const standardizedPathname = standardizePathname({ config, pathname });
|
|
123
135
|
const placeholderRemovedPathname = standardizedPathname.replace(
|
|
124
136
|
`/${PREFIX_PLACEHOLDER}`,
|
|
@@ -274,23 +286,23 @@ var standardizePathname = ({
|
|
|
274
286
|
return normalizePathname(standardizedPathname);
|
|
275
287
|
};
|
|
276
288
|
|
|
277
|
-
// src/modules/messages/load-
|
|
278
|
-
var
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
locale,
|
|
289
|
+
// src/modules/messages/load-remote-messages/fetch-locale-messages/fetch-locale-messages.ts
|
|
290
|
+
var fetchLocaleMessages = async ({
|
|
291
|
+
remoteUrl,
|
|
292
|
+
remoteHeaders,
|
|
282
293
|
searchParams,
|
|
283
|
-
|
|
294
|
+
locale,
|
|
295
|
+
extraOptions: { loggerOptions } = {}
|
|
284
296
|
}) => {
|
|
285
297
|
const baseLogger = getLogger({ ...loggerOptions });
|
|
286
|
-
const logger = baseLogger.child({ scope: "fetch-messages" });
|
|
298
|
+
const logger = baseLogger.child({ scope: "fetch-locale-messages" });
|
|
287
299
|
try {
|
|
288
300
|
const params = new URLSearchParams(searchParams);
|
|
289
301
|
params.append("locale", locale);
|
|
290
|
-
const url = `${
|
|
302
|
+
const url = `${remoteUrl}?${params.toString()}`;
|
|
291
303
|
const headers2 = {
|
|
292
304
|
"Content-Type": "application/json",
|
|
293
|
-
...
|
|
305
|
+
...remoteHeaders
|
|
294
306
|
};
|
|
295
307
|
const response = await fetch(url, {
|
|
296
308
|
method: "GET",
|
|
@@ -298,17 +310,17 @@ var fetchMessages = async ({
|
|
|
298
310
|
cache: "no-store"
|
|
299
311
|
});
|
|
300
312
|
if (!response.ok) {
|
|
301
|
-
throw new Error(`
|
|
313
|
+
throw new Error(`HTTP error ${response.status} ${response.statusText}`);
|
|
302
314
|
}
|
|
303
315
|
const data = await response.json();
|
|
304
|
-
if (
|
|
305
|
-
throw new Error(
|
|
316
|
+
if (!isValidMessages(data[locale])) {
|
|
317
|
+
throw new Error("JSON file does not match NamespaceMessages structure");
|
|
306
318
|
}
|
|
307
319
|
return data;
|
|
308
320
|
} catch (error) {
|
|
309
|
-
logger.warn(
|
|
321
|
+
logger.warn("Fetching locale messages failed.", {
|
|
310
322
|
locale,
|
|
311
|
-
|
|
323
|
+
remoteUrl,
|
|
312
324
|
searchParams: decodeURIComponent(searchParams.toString()),
|
|
313
325
|
error
|
|
314
326
|
});
|
|
@@ -316,30 +328,7 @@ var fetchMessages = async ({
|
|
|
316
328
|
}
|
|
317
329
|
};
|
|
318
330
|
|
|
319
|
-
// src/modules/messages/load-
|
|
320
|
-
var fetchFallbackMessages = async ({
|
|
321
|
-
apiUrl,
|
|
322
|
-
apiHeaders,
|
|
323
|
-
searchParams,
|
|
324
|
-
fallbackLocales,
|
|
325
|
-
logger
|
|
326
|
-
}) => {
|
|
327
|
-
for (const fallbackLocale of fallbackLocales) {
|
|
328
|
-
const result = await fetchMessages({
|
|
329
|
-
apiUrl,
|
|
330
|
-
searchParams,
|
|
331
|
-
locale: fallbackLocale,
|
|
332
|
-
apiHeaders,
|
|
333
|
-
logger
|
|
334
|
-
});
|
|
335
|
-
if (result) {
|
|
336
|
-
return { locale: fallbackLocale, messages: result };
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
return;
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
// src/modules/messages/load-api-messages/utils/build-search-params.ts
|
|
331
|
+
// src/modules/messages/load-remote-messages/fetch-locale-messages/utils/build-search-params.ts
|
|
343
332
|
var buildSearchParams = (params) => {
|
|
344
333
|
const searchParams = new URLSearchParams();
|
|
345
334
|
const appendParam = (key, value) => {
|
|
@@ -357,78 +346,72 @@ var buildSearchParams = (params) => {
|
|
|
357
346
|
return searchParams;
|
|
358
347
|
};
|
|
359
348
|
|
|
360
|
-
// src/modules/messages/load-
|
|
361
|
-
var
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
349
|
+
// src/modules/messages/load-remote-messages/load-remote-messages.ts
|
|
350
|
+
var loadRemoteMessages = async ({
|
|
351
|
+
pool = getGlobalMessagesPool(),
|
|
352
|
+
rootDir,
|
|
353
|
+
remoteUrl,
|
|
354
|
+
remoteHeaders,
|
|
365
355
|
locale,
|
|
366
356
|
fallbackLocales = [],
|
|
367
357
|
namespaces = [],
|
|
368
|
-
|
|
369
|
-
|
|
358
|
+
extraOptions: {
|
|
359
|
+
cacheOptions = DEFAULT_CACHE_OPTIONS,
|
|
360
|
+
loggerOptions = { id: "default" }
|
|
361
|
+
} = {}
|
|
370
362
|
}) => {
|
|
371
363
|
const baseLogger = getLogger({ ...loggerOptions });
|
|
372
|
-
const logger = baseLogger.child({ scope: "load-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
return;
|
|
376
|
-
}
|
|
377
|
-
let pool;
|
|
378
|
-
if (cache.enabled) {
|
|
379
|
-
pool = getGlobalMessagesPool();
|
|
380
|
-
}
|
|
364
|
+
const logger = baseLogger.child({ scope: "load-remote-messages" });
|
|
365
|
+
const start = performance.now();
|
|
366
|
+
logger.debug("Loading remote messages from api.", { remoteUrl });
|
|
381
367
|
const key = normalizeCacheKey([
|
|
382
368
|
loggerOptions.id,
|
|
383
|
-
|
|
369
|
+
"loaderType:remote",
|
|
370
|
+
rootDir,
|
|
384
371
|
locale,
|
|
385
372
|
(fallbackLocales ?? []).toSorted().join(","),
|
|
386
373
|
(namespaces ?? []).toSorted().join(",")
|
|
387
374
|
]);
|
|
388
|
-
if (
|
|
375
|
+
if (cacheOptions.enabled && key) {
|
|
389
376
|
const cached = await pool?.get(key);
|
|
390
377
|
if (cached) {
|
|
391
378
|
logger.debug("Messages cache hit.", { key });
|
|
392
379
|
return cached;
|
|
393
380
|
}
|
|
394
381
|
}
|
|
395
|
-
const searchParams = buildSearchParams({
|
|
396
|
-
const
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
382
|
+
const searchParams = buildSearchParams({ rootDir, namespaces });
|
|
383
|
+
const candidateLocales = [locale, ...fallbackLocales || []];
|
|
384
|
+
let messages;
|
|
385
|
+
for (const candidateLocale of candidateLocales) {
|
|
386
|
+
try {
|
|
387
|
+
const result = await fetchLocaleMessages({
|
|
388
|
+
remoteUrl,
|
|
389
|
+
remoteHeaders,
|
|
390
|
+
searchParams,
|
|
391
|
+
locale: candidateLocale,
|
|
392
|
+
extraOptions: { loggerOptions }
|
|
393
|
+
});
|
|
394
|
+
if (result && Object.values(result[candidateLocale] || {}).length > 0) {
|
|
395
|
+
messages = result;
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
398
|
+
} catch (error) {
|
|
399
|
+
logger.error("Failed to fetch locale messages.", {
|
|
400
|
+
locale: candidateLocale,
|
|
401
|
+
error
|
|
402
|
+
});
|
|
406
403
|
}
|
|
407
|
-
return messages;
|
|
408
404
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
apiHeaders,
|
|
412
|
-
searchParams,
|
|
413
|
-
fallbackLocales,
|
|
414
|
-
logger: loggerOptions
|
|
415
|
-
});
|
|
416
|
-
if (fallbackResult) {
|
|
417
|
-
logger.info("Fallback locale succeeded.", {
|
|
418
|
-
usedLocale: fallbackResult.locale,
|
|
419
|
-
apiUrl,
|
|
420
|
-
searchParams: decodeURIComponent(searchParams.toString())
|
|
421
|
-
});
|
|
422
|
-
if (cache.enabled && key) {
|
|
423
|
-
await pool?.set(key, fallbackResult.messages, cache.ttl);
|
|
424
|
-
}
|
|
425
|
-
return fallbackResult.messages;
|
|
405
|
+
if (cacheOptions.enabled && key && messages) {
|
|
406
|
+
await pool?.set(key, messages, cacheOptions.ttl);
|
|
426
407
|
}
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
408
|
+
const end = performance.now();
|
|
409
|
+
const duration = Math.round(end - start);
|
|
410
|
+
logger.trace("Finished loading remote messages.", {
|
|
411
|
+
loadedLocale: messages ? Object.keys(messages)[0] : void 0,
|
|
412
|
+
duration: `${duration} ms`
|
|
430
413
|
});
|
|
431
|
-
return;
|
|
414
|
+
return messages;
|
|
432
415
|
};
|
|
433
416
|
|
|
434
417
|
// src/adapters/next/contexts/messages/utils/use-refetch-messages.ts
|
|
@@ -439,20 +422,25 @@ var useRefetchMessages = ({
|
|
|
439
422
|
setIsLoadingMessages
|
|
440
423
|
}) => {
|
|
441
424
|
const { messages: staticMessages } = config;
|
|
442
|
-
const namespaces =
|
|
425
|
+
const namespaces = React6.useMemo(() => {
|
|
443
426
|
if (!config.loader) return [];
|
|
444
427
|
return resolveNamespaces({ config, pathname });
|
|
445
428
|
}, [config, pathname]);
|
|
446
|
-
const refetchMessages =
|
|
429
|
+
const refetchMessages = React6.useCallback(
|
|
447
430
|
async (newLocale) => {
|
|
448
|
-
if (config.loader?.type === "
|
|
431
|
+
if (config.loader?.type === "remote") {
|
|
449
432
|
setIsLoadingMessages(true);
|
|
450
|
-
const loadedMessages = await
|
|
451
|
-
|
|
433
|
+
const loadedMessages = await loadRemoteMessages({
|
|
434
|
+
rootDir: config.loader.rootDir,
|
|
435
|
+
remoteUrl: config.loader.remoteUrl,
|
|
436
|
+
remoteHeaders: config.loader.remoteHeaders,
|
|
452
437
|
locale: newLocale,
|
|
453
438
|
fallbackLocales: config.fallbackLocales[newLocale] || [],
|
|
454
439
|
namespaces,
|
|
455
|
-
|
|
440
|
+
extraOptions: {
|
|
441
|
+
cacheOptions: config.cache,
|
|
442
|
+
loggerOptions: { id: config.id }
|
|
443
|
+
}
|
|
456
444
|
});
|
|
457
445
|
const messages = mergeMessages(staticMessages, loadedMessages);
|
|
458
446
|
setLoadedMessages(messages);
|
|
@@ -471,6 +459,7 @@ var useRefetchMessages = ({
|
|
|
471
459
|
);
|
|
472
460
|
return { refetchMessages };
|
|
473
461
|
};
|
|
462
|
+
var MessagesContext = React6.createContext(void 0);
|
|
474
463
|
|
|
475
464
|
// src/adapters/next/contexts/messages/provider.tsx
|
|
476
465
|
function MessagesProvider({
|
|
@@ -478,15 +467,15 @@ function MessagesProvider({
|
|
|
478
467
|
children
|
|
479
468
|
}) {
|
|
480
469
|
const { config, pathname } = useConfig();
|
|
481
|
-
const [loadedMessages, setLoadedMessages] =
|
|
482
|
-
const [isLoadingMessages, setIsLoadingMessages] =
|
|
470
|
+
const [loadedMessages, setLoadedMessages] = React6.useState(null);
|
|
471
|
+
const [isLoadingMessages, setIsLoadingMessages] = React6.useState(false);
|
|
483
472
|
const { refetchMessages } = useRefetchMessages({
|
|
484
473
|
config,
|
|
485
474
|
pathname,
|
|
486
475
|
setLoadedMessages,
|
|
487
476
|
setIsLoadingMessages
|
|
488
477
|
});
|
|
489
|
-
const value =
|
|
478
|
+
const value = React6.useMemo(
|
|
490
479
|
() => ({
|
|
491
480
|
messages: loadedMessages || messages,
|
|
492
481
|
isLoading: isLoadingMessages,
|
|
@@ -496,10 +485,10 @@ function MessagesProvider({
|
|
|
496
485
|
}),
|
|
497
486
|
[loadedMessages, messages, isLoadingMessages, refetchMessages]
|
|
498
487
|
);
|
|
499
|
-
return /* @__PURE__ */
|
|
488
|
+
return /* @__PURE__ */ React6.createElement(MessagesContext.Provider, { value }, children);
|
|
500
489
|
}
|
|
501
490
|
function useMessages() {
|
|
502
|
-
const context =
|
|
491
|
+
const context = React6.useContext(MessagesContext);
|
|
503
492
|
if (!context)
|
|
504
493
|
throw new Error("useMessages must be used within a MessagesProvider");
|
|
505
494
|
return context;
|
|
@@ -512,8 +501,8 @@ var useInitLazyLoad = ({
|
|
|
512
501
|
}) => {
|
|
513
502
|
const { refetchMessages } = useMessages();
|
|
514
503
|
const lazyLoad = !!loaderOptions?.lazyLoad;
|
|
515
|
-
const isFirstLoadedRef =
|
|
516
|
-
|
|
504
|
+
const isFirstLoadedRef = React6.useRef(false);
|
|
505
|
+
React6.useEffect(() => {
|
|
517
506
|
if (lazyLoad && !isFirstLoadedRef.current) {
|
|
518
507
|
void refetchMessages(currentLocale);
|
|
519
508
|
isFirstLoadedRef.current = true;
|
|
@@ -559,7 +548,7 @@ var useInitLocaleCookie = ({
|
|
|
559
548
|
config,
|
|
560
549
|
locale
|
|
561
550
|
}) => {
|
|
562
|
-
|
|
551
|
+
React6.useEffect(() => {
|
|
563
552
|
if (typeof document === "undefined") return;
|
|
564
553
|
const { cookie, routing } = config;
|
|
565
554
|
const { firstVisit } = routing;
|
|
@@ -571,6 +560,7 @@ var useInitLocaleCookie = ({
|
|
|
571
560
|
setLocaleCookieBrowser({ cookie, locale });
|
|
572
561
|
}, []);
|
|
573
562
|
};
|
|
563
|
+
var LocaleContext = React6.createContext(void 0);
|
|
574
564
|
|
|
575
565
|
// src/adapters/next/contexts/locale/utils/change-locale.ts
|
|
576
566
|
var changeLocale = ({
|
|
@@ -584,15 +574,15 @@ var changeLocale = ({
|
|
|
584
574
|
if (typeof document === "undefined") return;
|
|
585
575
|
const loaderType = loaderOptions?.type;
|
|
586
576
|
if (newLocale === currentLocale) return;
|
|
587
|
-
if (loaderType === "
|
|
577
|
+
if (loaderType === "local") {
|
|
588
578
|
console.warn(
|
|
589
|
-
`[Intor] You are using dynamic
|
|
579
|
+
`[Intor] You are using dynamic 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.`
|
|
590
580
|
);
|
|
591
581
|
}
|
|
592
582
|
setLocale(newLocale);
|
|
593
583
|
setLocaleCookieBrowser({ cookie, locale: newLocale });
|
|
594
584
|
document.documentElement.lang = newLocale;
|
|
595
|
-
if (loaderType === "
|
|
585
|
+
if (loaderType === "remote" && refetchMessages) {
|
|
596
586
|
void refetchMessages(newLocale);
|
|
597
587
|
}
|
|
598
588
|
};
|
|
@@ -605,10 +595,10 @@ function LocaleProvider({
|
|
|
605
595
|
const { config } = useConfig();
|
|
606
596
|
const { refetchMessages } = useMessages();
|
|
607
597
|
const { loader: loaderOptions, cookie } = config;
|
|
608
|
-
const [currentLocale, setCurrentLocale] =
|
|
598
|
+
const [currentLocale, setCurrentLocale] = React6.useState(initialLocale);
|
|
609
599
|
useInitLazyLoad({ loaderOptions, currentLocale });
|
|
610
600
|
useInitLocaleCookie({ config, locale: initialLocale });
|
|
611
|
-
const setLocale =
|
|
601
|
+
const setLocale = React6.useCallback(
|
|
612
602
|
(newLocale) => {
|
|
613
603
|
changeLocale({
|
|
614
604
|
currentLocale,
|
|
@@ -621,43 +611,45 @@ function LocaleProvider({
|
|
|
621
611
|
},
|
|
622
612
|
[currentLocale, loaderOptions, cookie, refetchMessages]
|
|
623
613
|
);
|
|
624
|
-
const value =
|
|
614
|
+
const value = React6.useMemo(
|
|
625
615
|
() => ({
|
|
626
616
|
locale: currentLocale,
|
|
627
617
|
setLocale
|
|
628
618
|
}),
|
|
629
619
|
[currentLocale, setLocale]
|
|
630
620
|
);
|
|
631
|
-
return /* @__PURE__ */
|
|
621
|
+
return /* @__PURE__ */ React6.createElement(LocaleContext.Provider, { value }, children);
|
|
632
622
|
}
|
|
633
623
|
function useLocale() {
|
|
634
|
-
const context =
|
|
624
|
+
const context = React6.useContext(LocaleContext);
|
|
635
625
|
if (!context)
|
|
636
626
|
throw new Error("useLocale must be used within a LocaleProvider");
|
|
637
627
|
return context;
|
|
638
628
|
}
|
|
639
|
-
var
|
|
640
|
-
|
|
629
|
+
var TranslateHandlersContext = React6.createContext(void 0);
|
|
630
|
+
|
|
631
|
+
// src/adapters/next/contexts/translate-handlers/provider.tsx
|
|
641
632
|
var TranslateHandlersProvider = ({
|
|
642
633
|
children,
|
|
643
634
|
handlers
|
|
644
635
|
}) => {
|
|
645
636
|
const value = handlers;
|
|
646
|
-
return /* @__PURE__ */
|
|
637
|
+
return /* @__PURE__ */ React6.createElement(TranslateHandlersContext.Provider, { value }, children);
|
|
647
638
|
};
|
|
648
639
|
function useTranslateHandlers() {
|
|
649
|
-
const context =
|
|
640
|
+
const context = React6.useContext(TranslateHandlersContext);
|
|
650
641
|
return context;
|
|
651
642
|
}
|
|
652
643
|
var useInitLoadingState = (config) => {
|
|
653
644
|
const lazyLoad = !!config.loader?.lazyLoad;
|
|
654
|
-
const [isCsr, setIsCsr] =
|
|
655
|
-
|
|
645
|
+
const [isCsr, setIsCsr] = React6.useState(false);
|
|
646
|
+
React6.useEffect(() => {
|
|
656
647
|
setIsCsr(true);
|
|
657
648
|
}, []);
|
|
658
649
|
const isBeforeCSRLoading = lazyLoad && !isCsr;
|
|
659
650
|
return isBeforeCSRLoading;
|
|
660
651
|
};
|
|
652
|
+
var TranslatorContext = React6.createContext(void 0);
|
|
661
653
|
|
|
662
654
|
// src/adapters/next/contexts/translator/provider.tsx
|
|
663
655
|
var EMPTY_OBJECT = Object.freeze({});
|
|
@@ -668,7 +660,7 @@ function TranslatorProvider({ children }) {
|
|
|
668
660
|
const translatorHandlers = useTranslateHandlers();
|
|
669
661
|
const { fallbackLocales, translator: translatorOptions } = config;
|
|
670
662
|
const isBeforeCSRLoading = useInitLoadingState(config);
|
|
671
|
-
const value =
|
|
663
|
+
const value = React6.useMemo(() => {
|
|
672
664
|
const translator = new Translator({
|
|
673
665
|
messages: messages || EMPTY_OBJECT,
|
|
674
666
|
locale,
|
|
@@ -689,10 +681,10 @@ function TranslatorProvider({ children }) {
|
|
|
689
681
|
translatorOptions?.loadingMessage,
|
|
690
682
|
translatorOptions?.placeholder
|
|
691
683
|
]);
|
|
692
|
-
return /* @__PURE__ */
|
|
684
|
+
return /* @__PURE__ */ React6.createElement(TranslatorContext.Provider, { value }, children);
|
|
693
685
|
}
|
|
694
686
|
function useTranslator() {
|
|
695
|
-
const context =
|
|
687
|
+
const context = React6.useContext(TranslatorContext);
|
|
696
688
|
if (!context)
|
|
697
689
|
throw new Error(
|
|
698
690
|
"useTranslator must be used within IntorTranslatorProvider"
|
|
@@ -705,7 +697,7 @@ var IntorProvider = ({
|
|
|
705
697
|
value: { config, pathname, initialLocale, messages },
|
|
706
698
|
children
|
|
707
699
|
}) => {
|
|
708
|
-
return /* @__PURE__ */
|
|
700
|
+
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))));
|
|
709
701
|
};
|
|
710
702
|
|
|
711
703
|
// src/adapters/next/hooks/use-translator/use-translator.ts
|
|
@@ -802,7 +794,7 @@ var shouldFullReload = ({
|
|
|
802
794
|
}) => {
|
|
803
795
|
const loader = config.loader;
|
|
804
796
|
if (!loader || !loader.type) return false;
|
|
805
|
-
if (loader.type === "
|
|
797
|
+
if (loader.type === "remote" && !loader.fullReload) return false;
|
|
806
798
|
const { maybeLocale, isLocalePrefixed } = extractPathname({
|
|
807
799
|
config,
|
|
808
800
|
pathname: targetPathname
|
|
@@ -864,7 +856,7 @@ var Link = ({
|
|
|
864
856
|
onClick?.(e);
|
|
865
857
|
switchLocale({ href: formatted, locale });
|
|
866
858
|
};
|
|
867
|
-
return /* @__PURE__ */
|
|
859
|
+
return /* @__PURE__ */ React6.createElement(NextLink, { href: resolvedHref, onClick: handleClick, ...props }, children);
|
|
868
860
|
};
|
|
869
861
|
var useRouter = () => {
|
|
870
862
|
const { push, replace, ...rest } = useRouter$1();
|
|
@@ -42,28 +42,28 @@ type RouteNamespaces = {
|
|
|
42
42
|
[key: string]: string[];
|
|
43
43
|
default: string[];
|
|
44
44
|
};
|
|
45
|
-
interface
|
|
45
|
+
interface RemoteHeaders {
|
|
46
46
|
authorization?: string;
|
|
47
47
|
"x-api-key"?: string;
|
|
48
48
|
[key: string]: string | undefined;
|
|
49
49
|
}
|
|
50
50
|
type BaseLoaderOptions = {
|
|
51
|
-
|
|
51
|
+
rootDir?: string;
|
|
52
52
|
namespaces?: string[];
|
|
53
53
|
routeNamespaces?: RouteNamespaces;
|
|
54
54
|
concurrency?: number;
|
|
55
55
|
lazyLoad?: boolean;
|
|
56
56
|
};
|
|
57
|
-
type
|
|
58
|
-
type: "
|
|
57
|
+
type LocalLoader = BaseLoaderOptions & {
|
|
58
|
+
type: "local";
|
|
59
59
|
};
|
|
60
|
-
type
|
|
61
|
-
type: "
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
type RemoteLoader = BaseLoaderOptions & {
|
|
61
|
+
type: "remote";
|
|
62
|
+
remoteUrl: string;
|
|
63
|
+
remoteHeaders?: RemoteHeaders;
|
|
64
64
|
fullReload?: boolean;
|
|
65
65
|
};
|
|
66
|
-
type LoaderOptions =
|
|
66
|
+
type LoaderOptions = LocalLoader | RemoteLoader;
|
|
67
67
|
|
|
68
68
|
type LoggerOptions = {
|
|
69
69
|
level?: Level;
|
|
@@ -42,28 +42,28 @@ type RouteNamespaces = {
|
|
|
42
42
|
[key: string]: string[];
|
|
43
43
|
default: string[];
|
|
44
44
|
};
|
|
45
|
-
interface
|
|
45
|
+
interface RemoteHeaders {
|
|
46
46
|
authorization?: string;
|
|
47
47
|
"x-api-key"?: string;
|
|
48
48
|
[key: string]: string | undefined;
|
|
49
49
|
}
|
|
50
50
|
type BaseLoaderOptions = {
|
|
51
|
-
|
|
51
|
+
rootDir?: string;
|
|
52
52
|
namespaces?: string[];
|
|
53
53
|
routeNamespaces?: RouteNamespaces;
|
|
54
54
|
concurrency?: number;
|
|
55
55
|
lazyLoad?: boolean;
|
|
56
56
|
};
|
|
57
|
-
type
|
|
58
|
-
type: "
|
|
57
|
+
type LocalLoader = BaseLoaderOptions & {
|
|
58
|
+
type: "local";
|
|
59
59
|
};
|
|
60
|
-
type
|
|
61
|
-
type: "
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
type RemoteLoader = BaseLoaderOptions & {
|
|
61
|
+
type: "remote";
|
|
62
|
+
remoteUrl: string;
|
|
63
|
+
remoteHeaders?: RemoteHeaders;
|
|
64
64
|
fullReload?: boolean;
|
|
65
65
|
};
|
|
66
|
-
type LoaderOptions =
|
|
66
|
+
type LoaderOptions = LocalLoader | RemoteLoader;
|
|
67
67
|
|
|
68
68
|
type LoggerOptions = {
|
|
69
69
|
level?: Level;
|