intor 2.2.0 → 2.2.2
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 +306 -453
- package/dist/index.d.cts +76 -54
- package/dist/index.d.ts +76 -54
- package/dist/index.js +309 -457
- 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.cjs +7 -19
- package/dist/next/middleware/index.d.cts +10 -14
- package/dist/next/middleware/index.d.ts +10 -14
- package/dist/next/middleware/index.js +7 -19
- package/dist/next/server/index.cjs +286 -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 -434
- package/package.json +4 -12
package/dist/next/index.cjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var React6 = require('react');
|
|
4
4
|
var logry = require('logry');
|
|
5
5
|
var Keyv = require('keyv');
|
|
6
|
+
var merge = require('lodash.merge');
|
|
6
7
|
var intorTranslator = require('intor-translator');
|
|
7
8
|
var formatUrl = require('next/dist/shared/lib/router/utils/format-url');
|
|
8
9
|
var NextLink = require('next/link');
|
|
@@ -29,26 +30,27 @@ function _interopNamespace(e) {
|
|
|
29
30
|
return Object.freeze(n);
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
var
|
|
33
|
+
var React6__namespace = /*#__PURE__*/_interopNamespace(React6);
|
|
33
34
|
var Keyv__default = /*#__PURE__*/_interopDefault(Keyv);
|
|
35
|
+
var merge__default = /*#__PURE__*/_interopDefault(merge);
|
|
34
36
|
var NextLink__default = /*#__PURE__*/_interopDefault(NextLink);
|
|
35
37
|
|
|
36
38
|
// src/adapters/next/contexts/intor-provider/intor-provider.tsx
|
|
37
|
-
var ConfigContext =
|
|
39
|
+
var ConfigContext = React6__namespace.createContext(void 0);
|
|
40
|
+
|
|
41
|
+
// src/adapters/next/contexts/config/provider.tsx
|
|
38
42
|
function ConfigProvider({
|
|
39
43
|
value: { config, pathname },
|
|
40
44
|
children
|
|
41
45
|
}) {
|
|
42
|
-
const value =
|
|
43
|
-
return /* @__PURE__ */
|
|
46
|
+
const value = React6__namespace.useMemo(() => ({ config, pathname }), [config, pathname]);
|
|
47
|
+
return /* @__PURE__ */ React6__namespace.createElement(ConfigContext.Provider, { value }, children);
|
|
44
48
|
}
|
|
45
49
|
function useConfig() {
|
|
46
|
-
const context =
|
|
50
|
+
const context = React6__namespace.useContext(ConfigContext);
|
|
47
51
|
if (!context) throw new Error("useConfig must be used within ConfigProvider");
|
|
48
52
|
return context;
|
|
49
53
|
}
|
|
50
|
-
var LocaleContext = React7__namespace.createContext(void 0);
|
|
51
|
-
var MessagesContext = React7__namespace.createContext(void 0);
|
|
52
54
|
|
|
53
55
|
// src/modules/config/constants/cache.constants.ts
|
|
54
56
|
var DEFAULT_CACHE_OPTIONS = {
|
|
@@ -70,17 +72,18 @@ var DEFAULT_FORMATTER_CONFIG = {
|
|
|
70
72
|
node: { meta: { compact: true }, lineBreaksAfter: 1 }
|
|
71
73
|
};
|
|
72
74
|
function getLogger({
|
|
73
|
-
id,
|
|
75
|
+
id = "default",
|
|
74
76
|
formatterConfig,
|
|
75
77
|
preset,
|
|
76
78
|
...options
|
|
77
79
|
}) {
|
|
78
80
|
const pool = getGlobalLoggerPool();
|
|
79
81
|
let logger = pool.get(id);
|
|
82
|
+
const useDefault = !formatterConfig && !preset;
|
|
80
83
|
if (!logger) {
|
|
81
84
|
logger = logry.logry({
|
|
82
85
|
id,
|
|
83
|
-
formatterConfig:
|
|
86
|
+
formatterConfig: useDefault ? DEFAULT_FORMATTER_CONFIG : formatterConfig,
|
|
84
87
|
preset,
|
|
85
88
|
...options
|
|
86
89
|
});
|
|
@@ -92,28 +95,36 @@ function getLogger({
|
|
|
92
95
|
}
|
|
93
96
|
return logger;
|
|
94
97
|
}
|
|
98
|
+
|
|
99
|
+
// src/modules/messages/shared/utils/is-namespace-messages.ts
|
|
100
|
+
function isPlainObject(value) {
|
|
101
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
102
|
+
}
|
|
103
|
+
function isNamespaceMessages(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
|
+
}
|
|
95
119
|
function getGlobalMessagesPool() {
|
|
96
120
|
if (!globalThis.__INTOR_MESSAGES_POOL__) {
|
|
97
121
|
globalThis.__INTOR_MESSAGES_POOL__ = new Keyv__default.default();
|
|
98
122
|
}
|
|
99
123
|
return globalThis.__INTOR_MESSAGES_POOL__;
|
|
100
124
|
}
|
|
101
|
-
|
|
102
|
-
// src/shared/utils/merge-messages.ts
|
|
103
125
|
var mergeMessages = (staticMessages = {}, loadedMessages = {}) => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const loaded = loadedMessages[locale];
|
|
107
|
-
if (!result[locale]) {
|
|
108
|
-
result[locale] = loaded;
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
result[locale] = {
|
|
112
|
-
...result[locale],
|
|
113
|
-
...loaded
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
return result;
|
|
126
|
+
if (!loadedMessages) return { ...staticMessages };
|
|
127
|
+
return merge__default.default({}, staticMessages, loadedMessages);
|
|
117
128
|
};
|
|
118
129
|
|
|
119
130
|
// src/shared/utils/normalize-cache-key.ts
|
|
@@ -145,6 +156,8 @@ var resolveNamespaces = ({
|
|
|
145
156
|
}) => {
|
|
146
157
|
const { loader } = config;
|
|
147
158
|
const { routeNamespaces = {}, namespaces } = loader || {};
|
|
159
|
+
if (Object.keys(routeNamespaces).length === 0 && !namespaces)
|
|
160
|
+
return void 0;
|
|
148
161
|
const standardizedPathname = standardizePathname({ config, pathname });
|
|
149
162
|
const placeholderRemovedPathname = standardizedPathname.replace(
|
|
150
163
|
`/${PREFIX_PLACEHOLDER}`,
|
|
@@ -300,23 +313,23 @@ var standardizePathname = ({
|
|
|
300
313
|
return normalizePathname(standardizedPathname);
|
|
301
314
|
};
|
|
302
315
|
|
|
303
|
-
// src/modules/messages/load-
|
|
304
|
-
var
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
locale,
|
|
316
|
+
// src/modules/messages/load-remote-messages/fetch-locale-messages/fetch-locale-messages.ts
|
|
317
|
+
var fetchLocaleMessages = async ({
|
|
318
|
+
remoteUrl,
|
|
319
|
+
remoteHeaders,
|
|
308
320
|
searchParams,
|
|
309
|
-
|
|
321
|
+
locale,
|
|
322
|
+
extraOptions: { loggerOptions } = {}
|
|
310
323
|
}) => {
|
|
311
324
|
const baseLogger = getLogger({ ...loggerOptions });
|
|
312
|
-
const logger = baseLogger.child({ scope: "fetch-messages" });
|
|
325
|
+
const logger = baseLogger.child({ scope: "fetch-locale-messages" });
|
|
313
326
|
try {
|
|
314
327
|
const params = new URLSearchParams(searchParams);
|
|
315
328
|
params.append("locale", locale);
|
|
316
|
-
const url = `${
|
|
329
|
+
const url = `${remoteUrl}?${params.toString()}`;
|
|
317
330
|
const headers2 = {
|
|
318
331
|
"Content-Type": "application/json",
|
|
319
|
-
...
|
|
332
|
+
...remoteHeaders
|
|
320
333
|
};
|
|
321
334
|
const response = await fetch(url, {
|
|
322
335
|
method: "GET",
|
|
@@ -324,17 +337,17 @@ var fetchMessages = async ({
|
|
|
324
337
|
cache: "no-store"
|
|
325
338
|
});
|
|
326
339
|
if (!response.ok) {
|
|
327
|
-
throw new Error(`
|
|
340
|
+
throw new Error(`HTTP error ${response.status} ${response.statusText}`);
|
|
328
341
|
}
|
|
329
342
|
const data = await response.json();
|
|
330
|
-
if (
|
|
331
|
-
throw new Error(
|
|
343
|
+
if (!isNamespaceMessages(data[locale])) {
|
|
344
|
+
throw new Error("JSON file does not match NamespaceMessages structure");
|
|
332
345
|
}
|
|
333
346
|
return data;
|
|
334
347
|
} catch (error) {
|
|
335
|
-
logger.warn(
|
|
348
|
+
logger.warn("Fetching locale messages failed.", {
|
|
336
349
|
locale,
|
|
337
|
-
|
|
350
|
+
remoteUrl,
|
|
338
351
|
searchParams: decodeURIComponent(searchParams.toString()),
|
|
339
352
|
error
|
|
340
353
|
});
|
|
@@ -342,30 +355,7 @@ var fetchMessages = async ({
|
|
|
342
355
|
}
|
|
343
356
|
};
|
|
344
357
|
|
|
345
|
-
// src/modules/messages/load-
|
|
346
|
-
var fetchFallbackMessages = async ({
|
|
347
|
-
apiUrl,
|
|
348
|
-
apiHeaders,
|
|
349
|
-
searchParams,
|
|
350
|
-
fallbackLocales,
|
|
351
|
-
logger
|
|
352
|
-
}) => {
|
|
353
|
-
for (const fallbackLocale of fallbackLocales) {
|
|
354
|
-
const result = await fetchMessages({
|
|
355
|
-
apiUrl,
|
|
356
|
-
searchParams,
|
|
357
|
-
locale: fallbackLocale,
|
|
358
|
-
apiHeaders,
|
|
359
|
-
logger
|
|
360
|
-
});
|
|
361
|
-
if (result) {
|
|
362
|
-
return { locale: fallbackLocale, messages: result };
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
return;
|
|
366
|
-
};
|
|
367
|
-
|
|
368
|
-
// src/modules/messages/load-api-messages/utils/build-search-params.ts
|
|
358
|
+
// src/modules/messages/load-remote-messages/fetch-locale-messages/utils/build-search-params.ts
|
|
369
359
|
var buildSearchParams = (params) => {
|
|
370
360
|
const searchParams = new URLSearchParams();
|
|
371
361
|
const appendParam = (key, value) => {
|
|
@@ -383,78 +373,72 @@ var buildSearchParams = (params) => {
|
|
|
383
373
|
return searchParams;
|
|
384
374
|
};
|
|
385
375
|
|
|
386
|
-
// src/modules/messages/load-
|
|
387
|
-
var
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
376
|
+
// src/modules/messages/load-remote-messages/load-remote-messages.ts
|
|
377
|
+
var loadRemoteMessages = async ({
|
|
378
|
+
pool = getGlobalMessagesPool(),
|
|
379
|
+
rootDir,
|
|
380
|
+
remoteUrl,
|
|
381
|
+
remoteHeaders,
|
|
391
382
|
locale,
|
|
392
383
|
fallbackLocales = [],
|
|
393
384
|
namespaces = [],
|
|
394
|
-
|
|
395
|
-
|
|
385
|
+
extraOptions: {
|
|
386
|
+
cacheOptions = DEFAULT_CACHE_OPTIONS,
|
|
387
|
+
loggerOptions = { id: "default" }
|
|
388
|
+
} = {}
|
|
396
389
|
}) => {
|
|
397
390
|
const baseLogger = getLogger({ ...loggerOptions });
|
|
398
|
-
const logger = baseLogger.child({ scope: "load-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
return;
|
|
402
|
-
}
|
|
403
|
-
let pool;
|
|
404
|
-
if (cache.enabled) {
|
|
405
|
-
pool = getGlobalMessagesPool();
|
|
406
|
-
}
|
|
391
|
+
const logger = baseLogger.child({ scope: "load-remote-messages" });
|
|
392
|
+
const start = performance.now();
|
|
393
|
+
logger.debug("Loading remote messages from api.", { remoteUrl });
|
|
407
394
|
const key = normalizeCacheKey([
|
|
408
395
|
loggerOptions.id,
|
|
409
|
-
|
|
396
|
+
"loaderType:remote",
|
|
397
|
+
rootDir,
|
|
410
398
|
locale,
|
|
411
399
|
(fallbackLocales ?? []).toSorted().join(","),
|
|
412
400
|
(namespaces ?? []).toSorted().join(",")
|
|
413
401
|
]);
|
|
414
|
-
if (
|
|
402
|
+
if (cacheOptions.enabled && key) {
|
|
415
403
|
const cached = await pool?.get(key);
|
|
416
404
|
if (cached) {
|
|
417
405
|
logger.debug("Messages cache hit.", { key });
|
|
418
406
|
return cached;
|
|
419
407
|
}
|
|
420
408
|
}
|
|
421
|
-
const searchParams = buildSearchParams({
|
|
422
|
-
const
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
409
|
+
const searchParams = buildSearchParams({ rootDir, namespaces });
|
|
410
|
+
const candidateLocales = [locale, ...fallbackLocales || []];
|
|
411
|
+
let messages;
|
|
412
|
+
for (const candidateLocale of candidateLocales) {
|
|
413
|
+
try {
|
|
414
|
+
const result = await fetchLocaleMessages({
|
|
415
|
+
remoteUrl,
|
|
416
|
+
remoteHeaders,
|
|
417
|
+
searchParams,
|
|
418
|
+
locale: candidateLocale,
|
|
419
|
+
extraOptions: { loggerOptions }
|
|
420
|
+
});
|
|
421
|
+
if (result && Object.values(result[candidateLocale] || {}).length > 0) {
|
|
422
|
+
messages = result;
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
} catch (error) {
|
|
426
|
+
logger.error("Failed to fetch locale messages.", {
|
|
427
|
+
locale: candidateLocale,
|
|
428
|
+
error
|
|
429
|
+
});
|
|
432
430
|
}
|
|
433
|
-
return messages;
|
|
434
431
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
apiHeaders,
|
|
438
|
-
searchParams,
|
|
439
|
-
fallbackLocales,
|
|
440
|
-
logger: loggerOptions
|
|
441
|
-
});
|
|
442
|
-
if (fallbackResult) {
|
|
443
|
-
logger.info("Fallback locale succeeded.", {
|
|
444
|
-
usedLocale: fallbackResult.locale,
|
|
445
|
-
apiUrl,
|
|
446
|
-
searchParams: decodeURIComponent(searchParams.toString())
|
|
447
|
-
});
|
|
448
|
-
if (cache.enabled && key) {
|
|
449
|
-
await pool?.set(key, fallbackResult.messages, cache.ttl);
|
|
450
|
-
}
|
|
451
|
-
return fallbackResult.messages;
|
|
432
|
+
if (cacheOptions.enabled && key && messages) {
|
|
433
|
+
await pool?.set(key, messages, cacheOptions.ttl);
|
|
452
434
|
}
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
435
|
+
const end = performance.now();
|
|
436
|
+
const duration = Math.round(end - start);
|
|
437
|
+
logger.trace("Finished loading remote messages.", {
|
|
438
|
+
loadedLocale: messages ? Object.keys(messages)[0] : void 0,
|
|
439
|
+
duration: `${duration} ms`
|
|
456
440
|
});
|
|
457
|
-
return;
|
|
441
|
+
return messages;
|
|
458
442
|
};
|
|
459
443
|
|
|
460
444
|
// src/adapters/next/contexts/messages/utils/use-refetch-messages.ts
|
|
@@ -465,20 +449,25 @@ var useRefetchMessages = ({
|
|
|
465
449
|
setIsLoadingMessages
|
|
466
450
|
}) => {
|
|
467
451
|
const { messages: staticMessages } = config;
|
|
468
|
-
const namespaces =
|
|
452
|
+
const namespaces = React6__namespace.useMemo(() => {
|
|
469
453
|
if (!config.loader) return [];
|
|
470
454
|
return resolveNamespaces({ config, pathname });
|
|
471
455
|
}, [config, pathname]);
|
|
472
|
-
const refetchMessages =
|
|
456
|
+
const refetchMessages = React6__namespace.useCallback(
|
|
473
457
|
async (newLocale) => {
|
|
474
|
-
if (config.loader?.type === "
|
|
458
|
+
if (config.loader?.type === "remote") {
|
|
475
459
|
setIsLoadingMessages(true);
|
|
476
|
-
const loadedMessages = await
|
|
477
|
-
|
|
460
|
+
const loadedMessages = await loadRemoteMessages({
|
|
461
|
+
rootDir: config.loader.rootDir,
|
|
462
|
+
remoteUrl: config.loader.remoteUrl,
|
|
463
|
+
remoteHeaders: config.loader.remoteHeaders,
|
|
478
464
|
locale: newLocale,
|
|
479
465
|
fallbackLocales: config.fallbackLocales[newLocale] || [],
|
|
480
466
|
namespaces,
|
|
481
|
-
|
|
467
|
+
extraOptions: {
|
|
468
|
+
cacheOptions: config.cache,
|
|
469
|
+
loggerOptions: { id: config.id }
|
|
470
|
+
}
|
|
482
471
|
});
|
|
483
472
|
const messages = mergeMessages(staticMessages, loadedMessages);
|
|
484
473
|
setLoadedMessages(messages);
|
|
@@ -497,6 +486,7 @@ var useRefetchMessages = ({
|
|
|
497
486
|
);
|
|
498
487
|
return { refetchMessages };
|
|
499
488
|
};
|
|
489
|
+
var MessagesContext = React6__namespace.createContext(void 0);
|
|
500
490
|
|
|
501
491
|
// src/adapters/next/contexts/messages/provider.tsx
|
|
502
492
|
function MessagesProvider({
|
|
@@ -504,15 +494,15 @@ function MessagesProvider({
|
|
|
504
494
|
children
|
|
505
495
|
}) {
|
|
506
496
|
const { config, pathname } = useConfig();
|
|
507
|
-
const [loadedMessages, setLoadedMessages] =
|
|
508
|
-
const [isLoadingMessages, setIsLoadingMessages] =
|
|
497
|
+
const [loadedMessages, setLoadedMessages] = React6__namespace.useState(null);
|
|
498
|
+
const [isLoadingMessages, setIsLoadingMessages] = React6__namespace.useState(false);
|
|
509
499
|
const { refetchMessages } = useRefetchMessages({
|
|
510
500
|
config,
|
|
511
501
|
pathname,
|
|
512
502
|
setLoadedMessages,
|
|
513
503
|
setIsLoadingMessages
|
|
514
504
|
});
|
|
515
|
-
const value =
|
|
505
|
+
const value = React6__namespace.useMemo(
|
|
516
506
|
() => ({
|
|
517
507
|
messages: loadedMessages || messages,
|
|
518
508
|
isLoading: isLoadingMessages,
|
|
@@ -522,10 +512,10 @@ function MessagesProvider({
|
|
|
522
512
|
}),
|
|
523
513
|
[loadedMessages, messages, isLoadingMessages, refetchMessages]
|
|
524
514
|
);
|
|
525
|
-
return /* @__PURE__ */
|
|
515
|
+
return /* @__PURE__ */ React6__namespace.createElement(MessagesContext.Provider, { value }, children);
|
|
526
516
|
}
|
|
527
517
|
function useMessages() {
|
|
528
|
-
const context =
|
|
518
|
+
const context = React6__namespace.useContext(MessagesContext);
|
|
529
519
|
if (!context)
|
|
530
520
|
throw new Error("useMessages must be used within a MessagesProvider");
|
|
531
521
|
return context;
|
|
@@ -538,8 +528,8 @@ var useInitLazyLoad = ({
|
|
|
538
528
|
}) => {
|
|
539
529
|
const { refetchMessages } = useMessages();
|
|
540
530
|
const lazyLoad = !!loaderOptions?.lazyLoad;
|
|
541
|
-
const isFirstLoadedRef =
|
|
542
|
-
|
|
531
|
+
const isFirstLoadedRef = React6__namespace.useRef(false);
|
|
532
|
+
React6__namespace.useEffect(() => {
|
|
543
533
|
if (lazyLoad && !isFirstLoadedRef.current) {
|
|
544
534
|
void refetchMessages(currentLocale);
|
|
545
535
|
isFirstLoadedRef.current = true;
|
|
@@ -585,7 +575,7 @@ var useInitLocaleCookie = ({
|
|
|
585
575
|
config,
|
|
586
576
|
locale
|
|
587
577
|
}) => {
|
|
588
|
-
|
|
578
|
+
React6__namespace.useEffect(() => {
|
|
589
579
|
if (typeof document === "undefined") return;
|
|
590
580
|
const { cookie, routing } = config;
|
|
591
581
|
const { firstVisit } = routing;
|
|
@@ -597,6 +587,7 @@ var useInitLocaleCookie = ({
|
|
|
597
587
|
setLocaleCookieBrowser({ cookie, locale });
|
|
598
588
|
}, []);
|
|
599
589
|
};
|
|
590
|
+
var LocaleContext = React6__namespace.createContext(void 0);
|
|
600
591
|
|
|
601
592
|
// src/adapters/next/contexts/locale/utils/change-locale.ts
|
|
602
593
|
var changeLocale = ({
|
|
@@ -610,15 +601,15 @@ var changeLocale = ({
|
|
|
610
601
|
if (typeof document === "undefined") return;
|
|
611
602
|
const loaderType = loaderOptions?.type;
|
|
612
603
|
if (newLocale === currentLocale) return;
|
|
613
|
-
if (loaderType === "
|
|
604
|
+
if (loaderType === "local") {
|
|
614
605
|
console.warn(
|
|
615
|
-
`[Intor] You are using dynamic
|
|
606
|
+
`[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.`
|
|
616
607
|
);
|
|
617
608
|
}
|
|
618
609
|
setLocale(newLocale);
|
|
619
610
|
setLocaleCookieBrowser({ cookie, locale: newLocale });
|
|
620
611
|
document.documentElement.lang = newLocale;
|
|
621
|
-
if (loaderType === "
|
|
612
|
+
if (loaderType === "remote" && refetchMessages) {
|
|
622
613
|
void refetchMessages(newLocale);
|
|
623
614
|
}
|
|
624
615
|
};
|
|
@@ -631,10 +622,10 @@ function LocaleProvider({
|
|
|
631
622
|
const { config } = useConfig();
|
|
632
623
|
const { refetchMessages } = useMessages();
|
|
633
624
|
const { loader: loaderOptions, cookie } = config;
|
|
634
|
-
const [currentLocale, setCurrentLocale] =
|
|
625
|
+
const [currentLocale, setCurrentLocale] = React6__namespace.useState(initialLocale);
|
|
635
626
|
useInitLazyLoad({ loaderOptions, currentLocale });
|
|
636
627
|
useInitLocaleCookie({ config, locale: initialLocale });
|
|
637
|
-
const setLocale =
|
|
628
|
+
const setLocale = React6__namespace.useCallback(
|
|
638
629
|
(newLocale) => {
|
|
639
630
|
changeLocale({
|
|
640
631
|
currentLocale,
|
|
@@ -647,43 +638,45 @@ function LocaleProvider({
|
|
|
647
638
|
},
|
|
648
639
|
[currentLocale, loaderOptions, cookie, refetchMessages]
|
|
649
640
|
);
|
|
650
|
-
const value =
|
|
641
|
+
const value = React6__namespace.useMemo(
|
|
651
642
|
() => ({
|
|
652
643
|
locale: currentLocale,
|
|
653
644
|
setLocale
|
|
654
645
|
}),
|
|
655
646
|
[currentLocale, setLocale]
|
|
656
647
|
);
|
|
657
|
-
return /* @__PURE__ */
|
|
648
|
+
return /* @__PURE__ */ React6__namespace.createElement(LocaleContext.Provider, { value }, children);
|
|
658
649
|
}
|
|
659
650
|
function useLocale() {
|
|
660
|
-
const context =
|
|
651
|
+
const context = React6__namespace.useContext(LocaleContext);
|
|
661
652
|
if (!context)
|
|
662
653
|
throw new Error("useLocale must be used within a LocaleProvider");
|
|
663
654
|
return context;
|
|
664
655
|
}
|
|
665
|
-
var
|
|
666
|
-
|
|
656
|
+
var TranslateHandlersContext = React6__namespace.createContext(void 0);
|
|
657
|
+
|
|
658
|
+
// src/adapters/next/contexts/translate-handlers/provider.tsx
|
|
667
659
|
var TranslateHandlersProvider = ({
|
|
668
660
|
children,
|
|
669
661
|
handlers
|
|
670
662
|
}) => {
|
|
671
663
|
const value = handlers;
|
|
672
|
-
return /* @__PURE__ */
|
|
664
|
+
return /* @__PURE__ */ React6__namespace.createElement(TranslateHandlersContext.Provider, { value }, children);
|
|
673
665
|
};
|
|
674
666
|
function useTranslateHandlers() {
|
|
675
|
-
const context =
|
|
667
|
+
const context = React6__namespace.useContext(TranslateHandlersContext);
|
|
676
668
|
return context;
|
|
677
669
|
}
|
|
678
670
|
var useInitLoadingState = (config) => {
|
|
679
671
|
const lazyLoad = !!config.loader?.lazyLoad;
|
|
680
|
-
const [isCsr, setIsCsr] =
|
|
681
|
-
|
|
672
|
+
const [isCsr, setIsCsr] = React6__namespace.useState(false);
|
|
673
|
+
React6__namespace.useEffect(() => {
|
|
682
674
|
setIsCsr(true);
|
|
683
675
|
}, []);
|
|
684
676
|
const isBeforeCSRLoading = lazyLoad && !isCsr;
|
|
685
677
|
return isBeforeCSRLoading;
|
|
686
678
|
};
|
|
679
|
+
var TranslatorContext = React6__namespace.createContext(void 0);
|
|
687
680
|
|
|
688
681
|
// src/adapters/next/contexts/translator/provider.tsx
|
|
689
682
|
var EMPTY_OBJECT = Object.freeze({});
|
|
@@ -694,7 +687,7 @@ function TranslatorProvider({ children }) {
|
|
|
694
687
|
const translatorHandlers = useTranslateHandlers();
|
|
695
688
|
const { fallbackLocales, translator: translatorOptions } = config;
|
|
696
689
|
const isBeforeCSRLoading = useInitLoadingState(config);
|
|
697
|
-
const value =
|
|
690
|
+
const value = React6__namespace.useMemo(() => {
|
|
698
691
|
const translator = new intorTranslator.Translator({
|
|
699
692
|
messages: messages || EMPTY_OBJECT,
|
|
700
693
|
locale,
|
|
@@ -715,10 +708,10 @@ function TranslatorProvider({ children }) {
|
|
|
715
708
|
translatorOptions?.loadingMessage,
|
|
716
709
|
translatorOptions?.placeholder
|
|
717
710
|
]);
|
|
718
|
-
return /* @__PURE__ */
|
|
711
|
+
return /* @__PURE__ */ React6__namespace.createElement(TranslatorContext.Provider, { value }, children);
|
|
719
712
|
}
|
|
720
713
|
function useTranslator() {
|
|
721
|
-
const context =
|
|
714
|
+
const context = React6__namespace.useContext(TranslatorContext);
|
|
722
715
|
if (!context)
|
|
723
716
|
throw new Error(
|
|
724
717
|
"useTranslator must be used within IntorTranslatorProvider"
|
|
@@ -731,7 +724,7 @@ var IntorProvider = ({
|
|
|
731
724
|
value: { config, pathname, initialLocale, messages },
|
|
732
725
|
children
|
|
733
726
|
}) => {
|
|
734
|
-
return /* @__PURE__ */
|
|
727
|
+
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))));
|
|
735
728
|
};
|
|
736
729
|
|
|
737
730
|
// src/adapters/next/hooks/use-translator/use-translator.ts
|
|
@@ -828,7 +821,7 @@ var shouldFullReload = ({
|
|
|
828
821
|
}) => {
|
|
829
822
|
const loader = config.loader;
|
|
830
823
|
if (!loader || !loader.type) return false;
|
|
831
|
-
if (loader.type === "
|
|
824
|
+
if (loader.type === "remote" && !loader.fullReload) return false;
|
|
832
825
|
const { maybeLocale, isLocalePrefixed } = extractPathname({
|
|
833
826
|
config,
|
|
834
827
|
pathname: targetPathname
|
|
@@ -890,7 +883,7 @@ var Link = ({
|
|
|
890
883
|
onClick?.(e);
|
|
891
884
|
switchLocale({ href: formatted, locale });
|
|
892
885
|
};
|
|
893
|
-
return /* @__PURE__ */
|
|
886
|
+
return /* @__PURE__ */ React6__namespace.createElement(NextLink__default.default, { href: resolvedHref, onClick: handleClick, ...props }, children);
|
|
894
887
|
};
|
|
895
888
|
var useRouter = () => {
|
|
896
889
|
const { push, replace, ...rest } = navigation.useRouter();
|
package/dist/next/index.d.cts
CHANGED
|
@@ -47,28 +47,28 @@ type RouteNamespaces = {
|
|
|
47
47
|
[key: string]: string[];
|
|
48
48
|
default: string[];
|
|
49
49
|
};
|
|
50
|
-
interface
|
|
50
|
+
interface RemoteHeaders {
|
|
51
51
|
authorization?: string;
|
|
52
52
|
"x-api-key"?: string;
|
|
53
53
|
[key: string]: string | undefined;
|
|
54
54
|
}
|
|
55
55
|
type BaseLoaderOptions = {
|
|
56
|
-
|
|
56
|
+
rootDir?: string;
|
|
57
57
|
namespaces?: string[];
|
|
58
58
|
routeNamespaces?: RouteNamespaces;
|
|
59
59
|
concurrency?: number;
|
|
60
60
|
lazyLoad?: boolean;
|
|
61
61
|
};
|
|
62
|
-
type
|
|
63
|
-
type: "
|
|
62
|
+
type LocalLoader = BaseLoaderOptions & {
|
|
63
|
+
type: "local";
|
|
64
64
|
};
|
|
65
|
-
type
|
|
66
|
-
type: "
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
type RemoteLoader = BaseLoaderOptions & {
|
|
66
|
+
type: "remote";
|
|
67
|
+
remoteUrl: string;
|
|
68
|
+
remoteHeaders?: RemoteHeaders;
|
|
69
69
|
fullReload?: boolean;
|
|
70
70
|
};
|
|
71
|
-
type LoaderOptions =
|
|
71
|
+
type LoaderOptions = LocalLoader | RemoteLoader;
|
|
72
72
|
|
|
73
73
|
type LoggerOptions = {
|
|
74
74
|
level?: Level;
|
package/dist/next/index.d.ts
CHANGED
|
@@ -47,28 +47,28 @@ type RouteNamespaces = {
|
|
|
47
47
|
[key: string]: string[];
|
|
48
48
|
default: string[];
|
|
49
49
|
};
|
|
50
|
-
interface
|
|
50
|
+
interface RemoteHeaders {
|
|
51
51
|
authorization?: string;
|
|
52
52
|
"x-api-key"?: string;
|
|
53
53
|
[key: string]: string | undefined;
|
|
54
54
|
}
|
|
55
55
|
type BaseLoaderOptions = {
|
|
56
|
-
|
|
56
|
+
rootDir?: string;
|
|
57
57
|
namespaces?: string[];
|
|
58
58
|
routeNamespaces?: RouteNamespaces;
|
|
59
59
|
concurrency?: number;
|
|
60
60
|
lazyLoad?: boolean;
|
|
61
61
|
};
|
|
62
|
-
type
|
|
63
|
-
type: "
|
|
62
|
+
type LocalLoader = BaseLoaderOptions & {
|
|
63
|
+
type: "local";
|
|
64
64
|
};
|
|
65
|
-
type
|
|
66
|
-
type: "
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
type RemoteLoader = BaseLoaderOptions & {
|
|
66
|
+
type: "remote";
|
|
67
|
+
remoteUrl: string;
|
|
68
|
+
remoteHeaders?: RemoteHeaders;
|
|
69
69
|
fullReload?: boolean;
|
|
70
70
|
};
|
|
71
|
-
type LoaderOptions =
|
|
71
|
+
type LoaderOptions = LocalLoader | RemoteLoader;
|
|
72
72
|
|
|
73
73
|
type LoggerOptions = {
|
|
74
74
|
level?: Level;
|