intor 1.0.16 → 1.0.18
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/index.cjs +776 -0
- package/dist/index.d.cts +129 -3
- package/dist/index.d.ts +129 -3
- package/dist/index.js +761 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -7,12 +7,18 @@ var perf_hooks = require('perf_hooks');
|
|
|
7
7
|
var pLimit = require('p-limit');
|
|
8
8
|
var fs = require('fs/promises');
|
|
9
9
|
var intorTranslator = require('intor-translator');
|
|
10
|
+
var React6 = require('react');
|
|
11
|
+
var server = require('next/server');
|
|
12
|
+
var NextLink = require('next/link');
|
|
13
|
+
var navigation = require('next/navigation');
|
|
10
14
|
|
|
11
15
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
16
|
|
|
13
17
|
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
14
18
|
var pLimit__default = /*#__PURE__*/_interopDefault(pLimit);
|
|
15
19
|
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
20
|
+
var React6__default = /*#__PURE__*/_interopDefault(React6);
|
|
21
|
+
var NextLink__default = /*#__PURE__*/_interopDefault(NextLink);
|
|
16
22
|
|
|
17
23
|
var __defProp = Object.defineProperty;
|
|
18
24
|
var __defProps = Object.defineProperties;
|
|
@@ -33,6 +39,18 @@ var __spreadValues = (a, b) => {
|
|
|
33
39
|
return a;
|
|
34
40
|
};
|
|
35
41
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
42
|
+
var __objRest = (source, exclude) => {
|
|
43
|
+
var target = {};
|
|
44
|
+
for (var prop in source)
|
|
45
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
46
|
+
target[prop] = source[prop];
|
|
47
|
+
if (source != null && __getOwnPropSymbols)
|
|
48
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
49
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
50
|
+
target[prop] = source[prop];
|
|
51
|
+
}
|
|
52
|
+
return target;
|
|
53
|
+
};
|
|
36
54
|
var initializeLogger = ({
|
|
37
55
|
id,
|
|
38
56
|
scope,
|
|
@@ -1150,14 +1168,764 @@ var createIntor = (config, translateHandlers) => {
|
|
|
1150
1168
|
return translator;
|
|
1151
1169
|
};
|
|
1152
1170
|
};
|
|
1171
|
+
var IntorConfigContext = React6.createContext(void 0);
|
|
1172
|
+
var IntorConfigProvider = ({
|
|
1173
|
+
value: { config, pathname },
|
|
1174
|
+
children
|
|
1175
|
+
}) => {
|
|
1176
|
+
const value = React6.useMemo(
|
|
1177
|
+
() => ({
|
|
1178
|
+
config,
|
|
1179
|
+
pathname
|
|
1180
|
+
}),
|
|
1181
|
+
[config, pathname]
|
|
1182
|
+
);
|
|
1183
|
+
return /* @__PURE__ */ React6__default.default.createElement(IntorConfigContext.Provider, { value }, children);
|
|
1184
|
+
};
|
|
1185
|
+
var useIntorConfig = () => {
|
|
1186
|
+
const context = React6.useContext(IntorConfigContext);
|
|
1187
|
+
if (!context) {
|
|
1188
|
+
throw new Error("useIntorConfig must be used within IntorConfigProvider");
|
|
1189
|
+
}
|
|
1190
|
+
return context;
|
|
1191
|
+
};
|
|
1192
|
+
var IntorLocaleContext = React6.createContext(void 0);
|
|
1193
|
+
|
|
1194
|
+
// src/adapters/next-client/utils/build-cookie-string.ts
|
|
1195
|
+
var buildCookieString = (cookie, locale) => {
|
|
1196
|
+
var _a;
|
|
1197
|
+
const parts = [];
|
|
1198
|
+
parts.push(`${cookie.name}=${encodeURIComponent(locale)}`);
|
|
1199
|
+
if (cookie.maxAge) {
|
|
1200
|
+
const expires = new Date(Date.now() + cookie.maxAge * 1e3).toUTCString();
|
|
1201
|
+
parts.push(`expires=${expires}`);
|
|
1202
|
+
parts.push(`max-age=${cookie.maxAge}`);
|
|
1203
|
+
}
|
|
1204
|
+
parts.push(`path=${(_a = cookie.path) != null ? _a : "/"}`);
|
|
1205
|
+
if (cookie.domain) {
|
|
1206
|
+
parts.push(`domain=${cookie.domain}`);
|
|
1207
|
+
}
|
|
1208
|
+
if (cookie.sameSite) {
|
|
1209
|
+
parts.push(
|
|
1210
|
+
`SameSite=${cookie.sameSite[0].toUpperCase()}${cookie.sameSite.slice(1).toLowerCase()}`
|
|
1211
|
+
);
|
|
1212
|
+
}
|
|
1213
|
+
if (cookie.secure !== false) {
|
|
1214
|
+
parts.push(`Secure`);
|
|
1215
|
+
}
|
|
1216
|
+
return parts.join("; ");
|
|
1217
|
+
};
|
|
1218
|
+
|
|
1219
|
+
// src/adapters/next-client/utils/set-locale-cookie-client.ts
|
|
1220
|
+
var setLocaleCookieClient = ({
|
|
1221
|
+
cookie,
|
|
1222
|
+
locale
|
|
1223
|
+
}) => {
|
|
1224
|
+
if (typeof window === "undefined") {
|
|
1225
|
+
return;
|
|
1226
|
+
}
|
|
1227
|
+
if (cookie.disabled || !cookie.autoSetCookie) {
|
|
1228
|
+
return;
|
|
1229
|
+
}
|
|
1230
|
+
const cookieString = buildCookieString(cookie, locale);
|
|
1231
|
+
document.cookie = cookieString;
|
|
1232
|
+
};
|
|
1233
|
+
|
|
1234
|
+
// src/adapters/next-client/contexts/intor-locale/utils/change-locale.ts
|
|
1235
|
+
var changeLocale = ({
|
|
1236
|
+
currentLocale,
|
|
1237
|
+
newLocale,
|
|
1238
|
+
loaderOptions,
|
|
1239
|
+
cookie,
|
|
1240
|
+
setLocale,
|
|
1241
|
+
refetchMessages
|
|
1242
|
+
}) => {
|
|
1243
|
+
if (typeof document === "undefined") {
|
|
1244
|
+
return;
|
|
1245
|
+
}
|
|
1246
|
+
const loaderType = loaderOptions == null ? void 0 : loaderOptions.type;
|
|
1247
|
+
if (newLocale === currentLocale) {
|
|
1248
|
+
return;
|
|
1249
|
+
}
|
|
1250
|
+
if (loaderType === "import") {
|
|
1251
|
+
console.warn(
|
|
1252
|
+
`[Intor] You are using dynamic import 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.`
|
|
1253
|
+
);
|
|
1254
|
+
}
|
|
1255
|
+
setLocale(newLocale);
|
|
1256
|
+
setLocaleCookieClient({ cookie, locale: newLocale });
|
|
1257
|
+
document.documentElement.lang = newLocale;
|
|
1258
|
+
if (loaderType === "api" && refetchMessages) {
|
|
1259
|
+
void refetchMessages(newLocale);
|
|
1260
|
+
}
|
|
1261
|
+
};
|
|
1262
|
+
var IntorMessagesContext = React6.createContext(void 0);
|
|
1263
|
+
|
|
1264
|
+
// src/adapters/next-client/hooks/api/use-fetch-messages.ts
|
|
1265
|
+
var useFetchMessages = ({
|
|
1266
|
+
onError
|
|
1267
|
+
}) => {
|
|
1268
|
+
const fetcher2 = async (params) => {
|
|
1269
|
+
try {
|
|
1270
|
+
const data = await fetchApiMessages(params);
|
|
1271
|
+
return data;
|
|
1272
|
+
} catch (error) {
|
|
1273
|
+
onError == null ? void 0 : onError(error, params.apiUrl);
|
|
1274
|
+
return null;
|
|
1275
|
+
}
|
|
1276
|
+
};
|
|
1277
|
+
return { fetcher: fetcher2 };
|
|
1278
|
+
};
|
|
1279
|
+
|
|
1280
|
+
// src/adapters/next-client/hooks/api/use-refetch-messages.ts
|
|
1281
|
+
var useRefetchMessages = ({
|
|
1282
|
+
config,
|
|
1283
|
+
pathname,
|
|
1284
|
+
setLoadedMessages,
|
|
1285
|
+
setIsLoadingMessages,
|
|
1286
|
+
onError
|
|
1287
|
+
// Error handler
|
|
1288
|
+
}) => {
|
|
1289
|
+
const { messages: staticMessages } = config;
|
|
1290
|
+
const namespaces = React6.useMemo(() => {
|
|
1291
|
+
if (!config.loaderOptions) {
|
|
1292
|
+
return [];
|
|
1293
|
+
}
|
|
1294
|
+
return resolveNamespaces({ config, pathname });
|
|
1295
|
+
}, [config, pathname]);
|
|
1296
|
+
const { fetcher: fetcher2 } = useFetchMessages({ onError });
|
|
1297
|
+
const refetchMessages = React6.useCallback(
|
|
1298
|
+
async (newLocale) => {
|
|
1299
|
+
var _a;
|
|
1300
|
+
if (((_a = config.loaderOptions) == null ? void 0 : _a.type) === "api") {
|
|
1301
|
+
setIsLoadingMessages(true);
|
|
1302
|
+
const dynamicMessages = await fetcher2(__spreadProps(__spreadValues({}, config.loaderOptions), {
|
|
1303
|
+
locale: newLocale,
|
|
1304
|
+
fallbackLocales: config.fallbackLocales[newLocale] || [],
|
|
1305
|
+
namespaces,
|
|
1306
|
+
loggerId: config.id
|
|
1307
|
+
}));
|
|
1308
|
+
const messages = mergeStaticAndDynamicMessages(
|
|
1309
|
+
staticMessages,
|
|
1310
|
+
dynamicMessages
|
|
1311
|
+
);
|
|
1312
|
+
setLoadedMessages(messages);
|
|
1313
|
+
setIsLoadingMessages(false);
|
|
1314
|
+
}
|
|
1315
|
+
},
|
|
1316
|
+
[
|
|
1317
|
+
config.loaderOptions,
|
|
1318
|
+
config.fallbackLocales,
|
|
1319
|
+
config.id,
|
|
1320
|
+
setIsLoadingMessages,
|
|
1321
|
+
fetcher2,
|
|
1322
|
+
namespaces,
|
|
1323
|
+
staticMessages,
|
|
1324
|
+
setLoadedMessages
|
|
1325
|
+
]
|
|
1326
|
+
);
|
|
1327
|
+
return { refetchMessages };
|
|
1328
|
+
};
|
|
1329
|
+
|
|
1330
|
+
// src/adapters/next-client/contexts/intor-messages/intor-messages-provider.tsx
|
|
1331
|
+
var IntorMessagesProvider = ({
|
|
1332
|
+
value: { messages },
|
|
1333
|
+
children
|
|
1334
|
+
}) => {
|
|
1335
|
+
const { config, pathname } = useIntorConfig();
|
|
1336
|
+
const [loadedMessages, setLoadedMessages] = React6.useState(null);
|
|
1337
|
+
const [isLoadingMessages, setIsLoadingMessages] = React6.useState(false);
|
|
1338
|
+
const { refetchMessages } = useRefetchMessages({
|
|
1339
|
+
config,
|
|
1340
|
+
pathname,
|
|
1341
|
+
setLoadedMessages,
|
|
1342
|
+
setIsLoadingMessages
|
|
1343
|
+
});
|
|
1344
|
+
const value = React6.useMemo(
|
|
1345
|
+
() => ({
|
|
1346
|
+
messages: loadedMessages || messages,
|
|
1347
|
+
isLoading: isLoadingMessages,
|
|
1348
|
+
setLoadedMessages,
|
|
1349
|
+
setIsLoadingMessages,
|
|
1350
|
+
refetchMessages
|
|
1351
|
+
}),
|
|
1352
|
+
[loadedMessages, messages, isLoadingMessages, refetchMessages]
|
|
1353
|
+
);
|
|
1354
|
+
return /* @__PURE__ */ React6__default.default.createElement(IntorMessagesContext.Provider, { value }, children);
|
|
1355
|
+
};
|
|
1356
|
+
var useIntorMessages = () => {
|
|
1357
|
+
const context = React6.useContext(IntorMessagesContext);
|
|
1358
|
+
if (!context) {
|
|
1359
|
+
throw new Error(
|
|
1360
|
+
"useIntorMessages must be used within a IntorMessagesProvider"
|
|
1361
|
+
);
|
|
1362
|
+
}
|
|
1363
|
+
return context;
|
|
1364
|
+
};
|
|
1365
|
+
var useInitLazyLoad = ({
|
|
1366
|
+
loaderOptions,
|
|
1367
|
+
currentLocale
|
|
1368
|
+
}) => {
|
|
1369
|
+
const { refetchMessages } = useIntorMessages();
|
|
1370
|
+
const lazyLoad = !!(loaderOptions == null ? void 0 : loaderOptions.lazyLoad);
|
|
1371
|
+
const isFirstLoadedRef = React6.useRef(false);
|
|
1372
|
+
React6.useEffect(() => {
|
|
1373
|
+
if (lazyLoad && !isFirstLoadedRef.current) {
|
|
1374
|
+
void refetchMessages(currentLocale);
|
|
1375
|
+
isFirstLoadedRef.current = true;
|
|
1376
|
+
}
|
|
1377
|
+
}, [lazyLoad, currentLocale, refetchMessages, isFirstLoadedRef]);
|
|
1378
|
+
};
|
|
1379
|
+
var useInitLocaleCookie = ({
|
|
1380
|
+
config,
|
|
1381
|
+
locale
|
|
1382
|
+
}) => {
|
|
1383
|
+
React6.useEffect(() => {
|
|
1384
|
+
if (typeof document === "undefined") {
|
|
1385
|
+
return;
|
|
1386
|
+
}
|
|
1387
|
+
const { cookie, routing } = config;
|
|
1388
|
+
const { firstVisit } = routing;
|
|
1389
|
+
const cookies3 = document.cookie.split(";").map((c) => c.trim());
|
|
1390
|
+
const isCookieExists = cookies3.some((c) => c.startsWith(`${cookie.name}=`));
|
|
1391
|
+
if (isCookieExists) {
|
|
1392
|
+
return;
|
|
1393
|
+
}
|
|
1394
|
+
if (!firstVisit.redirect) {
|
|
1395
|
+
return;
|
|
1396
|
+
}
|
|
1397
|
+
if (cookie.disabled || !cookie.autoSetCookie) {
|
|
1398
|
+
return;
|
|
1399
|
+
}
|
|
1400
|
+
setLocaleCookieClient({ cookie, locale });
|
|
1401
|
+
}, []);
|
|
1402
|
+
};
|
|
1403
|
+
|
|
1404
|
+
// src/adapters/next-client/contexts/intor-locale/intor-locale-provider.tsx
|
|
1405
|
+
var IntorLocaleProvider = ({
|
|
1406
|
+
value: { initialLocale },
|
|
1407
|
+
children
|
|
1408
|
+
}) => {
|
|
1409
|
+
const { config } = useIntorConfig();
|
|
1410
|
+
const { refetchMessages } = useIntorMessages();
|
|
1411
|
+
const { loaderOptions, cookie } = config;
|
|
1412
|
+
const [currentLocale, setCurrentLocale] = React6.useState(initialLocale);
|
|
1413
|
+
useInitLazyLoad({ loaderOptions, currentLocale });
|
|
1414
|
+
useInitLocaleCookie({ config, locale: initialLocale });
|
|
1415
|
+
const setLocale = React6.useCallback(
|
|
1416
|
+
(newLocale) => {
|
|
1417
|
+
changeLocale({
|
|
1418
|
+
currentLocale,
|
|
1419
|
+
newLocale,
|
|
1420
|
+
loaderOptions,
|
|
1421
|
+
cookie,
|
|
1422
|
+
setLocale: setCurrentLocale,
|
|
1423
|
+
refetchMessages
|
|
1424
|
+
});
|
|
1425
|
+
},
|
|
1426
|
+
[currentLocale, loaderOptions, cookie, refetchMessages]
|
|
1427
|
+
);
|
|
1428
|
+
const value = React6.useMemo(
|
|
1429
|
+
() => ({
|
|
1430
|
+
locale: currentLocale,
|
|
1431
|
+
setLocale
|
|
1432
|
+
}),
|
|
1433
|
+
[currentLocale, setLocale]
|
|
1434
|
+
);
|
|
1435
|
+
return /* @__PURE__ */ React6__default.default.createElement(IntorLocaleContext.Provider, { value }, children);
|
|
1436
|
+
};
|
|
1437
|
+
var useIntorLocale = () => {
|
|
1438
|
+
const context = React6.useContext(IntorLocaleContext);
|
|
1439
|
+
if (!context) {
|
|
1440
|
+
throw new Error("useIntorLocale must be used within a IntorLocaleProvider");
|
|
1441
|
+
}
|
|
1442
|
+
return context;
|
|
1443
|
+
};
|
|
1444
|
+
var IntorTranslatorContext = React6.createContext(void 0);
|
|
1445
|
+
var TranslateHandlersContext = React6.createContext(void 0);
|
|
1446
|
+
var TranslateHandlersProvider = ({
|
|
1447
|
+
children,
|
|
1448
|
+
handlers
|
|
1449
|
+
}) => {
|
|
1450
|
+
const value = handlers;
|
|
1451
|
+
return /* @__PURE__ */ React6__default.default.createElement(TranslateHandlersContext.Provider, { value }, children);
|
|
1452
|
+
};
|
|
1453
|
+
var useTranslateHandlers = () => {
|
|
1454
|
+
const context = React6.useContext(TranslateHandlersContext);
|
|
1455
|
+
return context;
|
|
1456
|
+
};
|
|
1457
|
+
var useInitLoadingState = (config) => {
|
|
1458
|
+
var _a;
|
|
1459
|
+
const lazyLoad = !!((_a = config.loaderOptions) == null ? void 0 : _a.lazyLoad);
|
|
1460
|
+
const [isCsr, setIsCsr] = React6.useState(false);
|
|
1461
|
+
React6.useEffect(() => {
|
|
1462
|
+
setIsCsr(true);
|
|
1463
|
+
}, []);
|
|
1464
|
+
const isBeforeCSRLoading = lazyLoad && !isCsr;
|
|
1465
|
+
return isBeforeCSRLoading;
|
|
1466
|
+
};
|
|
1467
|
+
|
|
1468
|
+
// src/adapters/next-client/contexts/intor-translator/intor-translator-provider.tsx
|
|
1469
|
+
var EMPTY_OBJECT = Object.freeze({});
|
|
1470
|
+
var IntorTranslatorProvider = ({
|
|
1471
|
+
children
|
|
1472
|
+
}) => {
|
|
1473
|
+
const { config } = useIntorConfig();
|
|
1474
|
+
const { messages, isLoading } = useIntorMessages();
|
|
1475
|
+
const { locale } = useIntorLocale();
|
|
1476
|
+
const translatorHandlers = useTranslateHandlers();
|
|
1477
|
+
const { fallbackLocales, translator: translatorOptions } = config;
|
|
1478
|
+
const isBeforeCSRLoading = useInitLoadingState(config);
|
|
1479
|
+
const value = React6.useMemo(() => {
|
|
1480
|
+
const translator = new intorTranslator.Translator({
|
|
1481
|
+
messages: messages || EMPTY_OBJECT,
|
|
1482
|
+
locale,
|
|
1483
|
+
fallbackLocales,
|
|
1484
|
+
loadingMessage: translatorOptions == null ? void 0 : translatorOptions.loadingMessage,
|
|
1485
|
+
placeholder: translatorOptions == null ? void 0 : translatorOptions.placeholder,
|
|
1486
|
+
handlers: translatorHandlers
|
|
1487
|
+
});
|
|
1488
|
+
translator.setLoading(isBeforeCSRLoading || isLoading);
|
|
1489
|
+
return { translator };
|
|
1490
|
+
}, [
|
|
1491
|
+
fallbackLocales,
|
|
1492
|
+
isBeforeCSRLoading,
|
|
1493
|
+
isLoading,
|
|
1494
|
+
locale,
|
|
1495
|
+
messages,
|
|
1496
|
+
translatorHandlers,
|
|
1497
|
+
translatorOptions == null ? void 0 : translatorOptions.loadingMessage,
|
|
1498
|
+
translatorOptions == null ? void 0 : translatorOptions.placeholder
|
|
1499
|
+
]);
|
|
1500
|
+
return /* @__PURE__ */ React6__default.default.createElement(IntorTranslatorContext.Provider, { value }, children);
|
|
1501
|
+
};
|
|
1502
|
+
function useIntorTranslator() {
|
|
1503
|
+
const context = React6.useContext(IntorTranslatorContext);
|
|
1504
|
+
if (!context) {
|
|
1505
|
+
throw new Error(
|
|
1506
|
+
"useIntorTranslator must be used within IntorTranslatorProvider"
|
|
1507
|
+
);
|
|
1508
|
+
}
|
|
1509
|
+
return context;
|
|
1510
|
+
}
|
|
1511
|
+
var IntorProvider = ({
|
|
1512
|
+
value: { config, pathname, initialLocale, messages },
|
|
1513
|
+
children
|
|
1514
|
+
}) => {
|
|
1515
|
+
return /* @__PURE__ */ React6__default.default.createElement(IntorConfigProvider, { value: { config, pathname } }, /* @__PURE__ */ React6__default.default.createElement(IntorMessagesProvider, { value: { messages } }, /* @__PURE__ */ React6__default.default.createElement(IntorLocaleProvider, { value: { initialLocale } }, /* @__PURE__ */ React6__default.default.createElement(IntorTranslatorProvider, null, children))));
|
|
1516
|
+
};
|
|
1517
|
+
|
|
1518
|
+
// src/adapters/next-client/hooks/use-intor/use-intor.ts
|
|
1519
|
+
function useIntorWithPreKey(preKey) {
|
|
1520
|
+
const { translator } = useIntorTranslator();
|
|
1521
|
+
const { setLocale } = useIntorLocale();
|
|
1522
|
+
const scoped = translator.scoped(
|
|
1523
|
+
preKey
|
|
1524
|
+
);
|
|
1525
|
+
return {
|
|
1526
|
+
messages: translator.messages,
|
|
1527
|
+
setLocale,
|
|
1528
|
+
hasKey: scoped.hasKey,
|
|
1529
|
+
t: scoped.t
|
|
1530
|
+
};
|
|
1531
|
+
}
|
|
1532
|
+
function useIntorWithoutPreKey() {
|
|
1533
|
+
const { translator } = useIntorTranslator();
|
|
1534
|
+
const { setLocale } = useIntorLocale();
|
|
1535
|
+
const scoped = translator.scoped();
|
|
1536
|
+
return {
|
|
1537
|
+
messages: translator.messages,
|
|
1538
|
+
setLocale,
|
|
1539
|
+
hasKey: scoped.hasKey,
|
|
1540
|
+
t: scoped.t
|
|
1541
|
+
};
|
|
1542
|
+
}
|
|
1543
|
+
function useIntor(preKey) {
|
|
1544
|
+
if (preKey) {
|
|
1545
|
+
return useIntorWithPreKey(preKey);
|
|
1546
|
+
}
|
|
1547
|
+
return useIntorWithoutPreKey();
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
// src/adapters/next-client/routing/utils/set-locale-cookie-edge.ts
|
|
1551
|
+
function setLocaleCookieEdge({
|
|
1552
|
+
request,
|
|
1553
|
+
response,
|
|
1554
|
+
cookie,
|
|
1555
|
+
locale,
|
|
1556
|
+
override = false
|
|
1557
|
+
// Default to not override existed cookie
|
|
1558
|
+
}) {
|
|
1559
|
+
if (cookie.disabled || !cookie.autoSetCookie) {
|
|
1560
|
+
return;
|
|
1561
|
+
}
|
|
1562
|
+
const isCookieExists = request.cookies.has(cookie.name);
|
|
1563
|
+
if (isCookieExists && !override) {
|
|
1564
|
+
return;
|
|
1565
|
+
}
|
|
1566
|
+
response.cookies.set(cookie.name, locale, __spreadProps(__spreadValues({
|
|
1567
|
+
maxAge: cookie.maxAge,
|
|
1568
|
+
path: cookie.path
|
|
1569
|
+
}, cookie.domain ? { domain: cookie.domain } : {}), {
|
|
1570
|
+
secure: cookie.secure,
|
|
1571
|
+
httpOnly: cookie.httpOnly,
|
|
1572
|
+
sameSite: cookie.sameSite
|
|
1573
|
+
}));
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
// src/adapters/next-client/routing/utils/set-pathname-header.ts
|
|
1577
|
+
var setPathnameHeader = ({
|
|
1578
|
+
request,
|
|
1579
|
+
response,
|
|
1580
|
+
key = DEFAULT_PATHNAME_HEADER_KEY
|
|
1581
|
+
}) => {
|
|
1582
|
+
const pathname = request.nextUrl.pathname;
|
|
1583
|
+
response.headers.set(key, pathname);
|
|
1584
|
+
return response;
|
|
1585
|
+
};
|
|
1586
|
+
|
|
1587
|
+
// src/adapters/next-client/utils/locale-prefix-pathname.ts
|
|
1588
|
+
var localePrefixPathname = ({
|
|
1589
|
+
config,
|
|
1590
|
+
pathname: standardizedPathname,
|
|
1591
|
+
locale
|
|
1592
|
+
}) => {
|
|
1593
|
+
const { routing, prefixPlaceHolder } = config;
|
|
1594
|
+
const { prefix } = routing;
|
|
1595
|
+
if (prefix !== "none" && !locale) {
|
|
1596
|
+
throw new Error('No locale when using prefix "all", "except-default"');
|
|
1597
|
+
}
|
|
1598
|
+
if (prefix === "all") {
|
|
1599
|
+
return standardizedPathname.replace(prefixPlaceHolder, locale);
|
|
1600
|
+
}
|
|
1601
|
+
if (prefix === "except-default") {
|
|
1602
|
+
if (locale === config.defaultLocale) {
|
|
1603
|
+
return standardizedPathname.replace(`/${prefixPlaceHolder}`, "");
|
|
1604
|
+
} else {
|
|
1605
|
+
return standardizedPathname.replace(prefixPlaceHolder, locale);
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
return standardizedPathname.replace(`/${prefixPlaceHolder}`, "");
|
|
1609
|
+
};
|
|
1610
|
+
|
|
1611
|
+
// src/adapters/next-client/utils/localize-pathname.ts
|
|
1612
|
+
var localizePathname = ({
|
|
1613
|
+
config,
|
|
1614
|
+
pathname: rawPathname,
|
|
1615
|
+
locale
|
|
1616
|
+
}) => {
|
|
1617
|
+
const { unprefixedPathname } = extractPathname({
|
|
1618
|
+
config,
|
|
1619
|
+
pathname: rawPathname
|
|
1620
|
+
});
|
|
1621
|
+
const standardizedPathname = standardizePathname({
|
|
1622
|
+
config,
|
|
1623
|
+
pathname: unprefixedPathname
|
|
1624
|
+
});
|
|
1625
|
+
const localePrefixedPathname = localePrefixPathname({
|
|
1626
|
+
config,
|
|
1627
|
+
pathname: standardizedPathname,
|
|
1628
|
+
locale
|
|
1629
|
+
});
|
|
1630
|
+
return {
|
|
1631
|
+
unprefixedPathname,
|
|
1632
|
+
standardizedPathname,
|
|
1633
|
+
localePrefixedPathname
|
|
1634
|
+
};
|
|
1635
|
+
};
|
|
1636
|
+
|
|
1637
|
+
// src/adapters/next-client/routing/utils/create-response.ts
|
|
1638
|
+
var createResponse = ({
|
|
1639
|
+
request,
|
|
1640
|
+
config,
|
|
1641
|
+
locale,
|
|
1642
|
+
responseType = "next",
|
|
1643
|
+
setCookieOptions = { override: false }
|
|
1644
|
+
}) => {
|
|
1645
|
+
const { cookie } = config;
|
|
1646
|
+
const { override } = setCookieOptions;
|
|
1647
|
+
const url = request.nextUrl.clone();
|
|
1648
|
+
const { localePrefixedPathname } = localizePathname({
|
|
1649
|
+
config,
|
|
1650
|
+
pathname: url.pathname,
|
|
1651
|
+
locale
|
|
1652
|
+
});
|
|
1653
|
+
url.pathname = localePrefixedPathname;
|
|
1654
|
+
let response;
|
|
1655
|
+
switch (responseType) {
|
|
1656
|
+
case "next": {
|
|
1657
|
+
response = server.NextResponse.next();
|
|
1658
|
+
break;
|
|
1659
|
+
}
|
|
1660
|
+
case "redirect": {
|
|
1661
|
+
response = server.NextResponse.redirect(url);
|
|
1662
|
+
break;
|
|
1663
|
+
}
|
|
1664
|
+
default: {
|
|
1665
|
+
throw new Error(`Unsupported responseType: ${responseType}`);
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
if (locale) {
|
|
1669
|
+
setLocaleCookieEdge({
|
|
1670
|
+
request,
|
|
1671
|
+
response,
|
|
1672
|
+
locale,
|
|
1673
|
+
cookie,
|
|
1674
|
+
override
|
|
1675
|
+
});
|
|
1676
|
+
}
|
|
1677
|
+
const finalResponse = setPathnameHeader({ request, response });
|
|
1678
|
+
return finalResponse;
|
|
1679
|
+
};
|
|
1680
|
+
var determineInitialLocale = async (config) => {
|
|
1681
|
+
const { defaultLocale, supportedLocales, routing } = config;
|
|
1682
|
+
let initialLocale = defaultLocale;
|
|
1683
|
+
if (routing.firstVisit.localeSource === "browser") {
|
|
1684
|
+
const acceptLanguageHeader = (await headers.headers()).get("accept-language") || void 0;
|
|
1685
|
+
const preferredLocale = resolvePreferredLocale(
|
|
1686
|
+
acceptLanguageHeader,
|
|
1687
|
+
supportedLocales
|
|
1688
|
+
);
|
|
1689
|
+
initialLocale = normalizeLocale(preferredLocale, supportedLocales) || defaultLocale;
|
|
1690
|
+
}
|
|
1691
|
+
return initialLocale;
|
|
1692
|
+
};
|
|
1693
|
+
|
|
1694
|
+
// src/adapters/next-client/routing/handle-prefix/handle-prefix-all.ts
|
|
1695
|
+
var handlePrefixAll = async ({
|
|
1696
|
+
request,
|
|
1697
|
+
config
|
|
1698
|
+
}) => {
|
|
1699
|
+
var _a;
|
|
1700
|
+
const { cookie, routing } = config;
|
|
1701
|
+
const { maybeLocale, isLocalePrefixed } = extractPathname({
|
|
1702
|
+
config,
|
|
1703
|
+
pathname: request.nextUrl.pathname
|
|
1704
|
+
});
|
|
1705
|
+
const localeFromCookie = (_a = request.cookies.get(cookie.name)) == null ? void 0 : _a.value;
|
|
1706
|
+
if (isLocalePrefixed) {
|
|
1707
|
+
return createResponse({
|
|
1708
|
+
request,
|
|
1709
|
+
config,
|
|
1710
|
+
locale: maybeLocale,
|
|
1711
|
+
setCookieOptions: { override: true }
|
|
1712
|
+
});
|
|
1713
|
+
}
|
|
1714
|
+
if (!localeFromCookie) {
|
|
1715
|
+
if (!routing.firstVisit.redirect) {
|
|
1716
|
+
return createResponse({ request, config });
|
|
1717
|
+
}
|
|
1718
|
+
const initialLocale = await determineInitialLocale(config);
|
|
1719
|
+
return createResponse({
|
|
1720
|
+
request,
|
|
1721
|
+
config,
|
|
1722
|
+
locale: initialLocale,
|
|
1723
|
+
// Use locale from 'browser' | 'default'
|
|
1724
|
+
responseType: "redirect"
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
return createResponse({
|
|
1728
|
+
request,
|
|
1729
|
+
config,
|
|
1730
|
+
locale: localeFromCookie,
|
|
1731
|
+
// Use locale from cookie
|
|
1732
|
+
responseType: "redirect"
|
|
1733
|
+
});
|
|
1734
|
+
};
|
|
1735
|
+
|
|
1736
|
+
// src/adapters/next-client/routing/handle-prefix/handle-prefix-except-default.ts
|
|
1737
|
+
var handlePrefixExceptDefault = async ({
|
|
1738
|
+
request,
|
|
1739
|
+
config
|
|
1740
|
+
}) => {
|
|
1741
|
+
var _a;
|
|
1742
|
+
const { defaultLocale, cookie, routing } = config;
|
|
1743
|
+
const { maybeLocale, isLocalePrefixed } = extractPathname({
|
|
1744
|
+
config,
|
|
1745
|
+
pathname: request.nextUrl.pathname
|
|
1746
|
+
});
|
|
1747
|
+
const localeFromCookie = (_a = request.cookies.get(cookie.name)) == null ? void 0 : _a.value;
|
|
1748
|
+
if (isLocalePrefixed && maybeLocale !== defaultLocale) {
|
|
1749
|
+
return createResponse({
|
|
1750
|
+
request,
|
|
1751
|
+
config,
|
|
1752
|
+
locale: maybeLocale,
|
|
1753
|
+
setCookieOptions: { override: true }
|
|
1754
|
+
});
|
|
1755
|
+
}
|
|
1756
|
+
if (!localeFromCookie) {
|
|
1757
|
+
if (!routing.firstVisit.redirect) {
|
|
1758
|
+
return createResponse({ request, config });
|
|
1759
|
+
}
|
|
1760
|
+
const initialLocale = await determineInitialLocale(config);
|
|
1761
|
+
const isDefaultLocale2 = initialLocale === defaultLocale;
|
|
1762
|
+
if (isDefaultLocale2) {
|
|
1763
|
+
return createResponse({
|
|
1764
|
+
request,
|
|
1765
|
+
config,
|
|
1766
|
+
locale: defaultLocale
|
|
1767
|
+
});
|
|
1768
|
+
}
|
|
1769
|
+
return createResponse({
|
|
1770
|
+
request,
|
|
1771
|
+
config,
|
|
1772
|
+
locale: initialLocale,
|
|
1773
|
+
// Use locale from 'browser' | 'default'
|
|
1774
|
+
responseType: "redirect"
|
|
1775
|
+
});
|
|
1776
|
+
}
|
|
1777
|
+
const isDefaultLocale = localeFromCookie === defaultLocale;
|
|
1778
|
+
if (isDefaultLocale) {
|
|
1779
|
+
return createResponse({ request, config, locale: localeFromCookie });
|
|
1780
|
+
}
|
|
1781
|
+
return createResponse({
|
|
1782
|
+
request,
|
|
1783
|
+
config,
|
|
1784
|
+
locale: localeFromCookie,
|
|
1785
|
+
// Use locale from cookie
|
|
1786
|
+
responseType: "redirect"
|
|
1787
|
+
});
|
|
1788
|
+
};
|
|
1789
|
+
|
|
1790
|
+
// src/adapters/next-client/routing/handle-prefix/handle-prefix-none.ts
|
|
1791
|
+
var handlePrefixNone = async ({
|
|
1792
|
+
request,
|
|
1793
|
+
config
|
|
1794
|
+
}) => {
|
|
1795
|
+
var _a;
|
|
1796
|
+
let locale = (_a = request.cookies.get(config.cookie.name)) == null ? void 0 : _a.value;
|
|
1797
|
+
if (!locale) {
|
|
1798
|
+
locale = await determineInitialLocale(config);
|
|
1799
|
+
}
|
|
1800
|
+
return createResponse({ request, config, locale });
|
|
1801
|
+
};
|
|
1802
|
+
|
|
1803
|
+
// src/adapters/next-client/routing/intor-middleware.ts
|
|
1804
|
+
async function intorMiddleware({
|
|
1805
|
+
request,
|
|
1806
|
+
config
|
|
1807
|
+
}) {
|
|
1808
|
+
const { prefix } = config.routing;
|
|
1809
|
+
if (prefix === "none") {
|
|
1810
|
+
return handlePrefixNone({ request, config });
|
|
1811
|
+
}
|
|
1812
|
+
if (prefix === "except-default") {
|
|
1813
|
+
return await handlePrefixExceptDefault({ request, config });
|
|
1814
|
+
}
|
|
1815
|
+
return await handlePrefixAll({ request, config });
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
// src/adapters/next-client/utils/should-use-full-reload.ts
|
|
1819
|
+
var shouldUseFullReload = (loaderOptions) => {
|
|
1820
|
+
return (loaderOptions == null ? void 0 : loaderOptions.type) === "import" || (loaderOptions == null ? void 0 : loaderOptions.type) === "api" && loaderOptions.fullReload === true;
|
|
1821
|
+
};
|
|
1822
|
+
var Link = (_a) => {
|
|
1823
|
+
var _b = _a, {
|
|
1824
|
+
href,
|
|
1825
|
+
locale,
|
|
1826
|
+
children
|
|
1827
|
+
} = _b, props = __objRest(_b, [
|
|
1828
|
+
"href",
|
|
1829
|
+
"locale",
|
|
1830
|
+
"children"
|
|
1831
|
+
]);
|
|
1832
|
+
const { config } = useIntorConfig();
|
|
1833
|
+
const { locale: currentLocale, setLocale } = useIntorLocale();
|
|
1834
|
+
const targetLocale = locale || currentLocale;
|
|
1835
|
+
const { localePrefixedPathname } = localizePathname({
|
|
1836
|
+
config,
|
|
1837
|
+
pathname: href,
|
|
1838
|
+
locale: targetLocale
|
|
1839
|
+
});
|
|
1840
|
+
href = localePrefixedPathname;
|
|
1841
|
+
const handleClick = (e) => {
|
|
1842
|
+
if (props.onClick) {
|
|
1843
|
+
props.onClick(e);
|
|
1844
|
+
}
|
|
1845
|
+
if (e.defaultPrevented) {
|
|
1846
|
+
return;
|
|
1847
|
+
}
|
|
1848
|
+
if (shouldUseFullReload(config.loaderOptions)) {
|
|
1849
|
+
window.location.href = href;
|
|
1850
|
+
return;
|
|
1851
|
+
} else {
|
|
1852
|
+
setLocale(targetLocale);
|
|
1853
|
+
}
|
|
1854
|
+
};
|
|
1855
|
+
return /* @__PURE__ */ React6__default.default.createElement(NextLink__default.default, __spreadValues({ href, onClick: handleClick }, props), children);
|
|
1856
|
+
};
|
|
1857
|
+
var usePathname = () => {
|
|
1858
|
+
const { config } = useIntorConfig();
|
|
1859
|
+
const { locale } = useIntorLocale();
|
|
1860
|
+
const rawPathname = navigation.usePathname();
|
|
1861
|
+
const { localePrefixedPathname } = localizePathname({
|
|
1862
|
+
config,
|
|
1863
|
+
pathname: rawPathname,
|
|
1864
|
+
locale
|
|
1865
|
+
});
|
|
1866
|
+
return localePrefixedPathname;
|
|
1867
|
+
};
|
|
1868
|
+
var useRouter = () => {
|
|
1869
|
+
const { config } = useIntorConfig();
|
|
1870
|
+
const { locale: currentLocale, setLocale } = useIntorLocale();
|
|
1871
|
+
const _a = navigation.useRouter(), { push, replace } = _a, rest = __objRest(_a, ["push", "replace"]);
|
|
1872
|
+
const pushWithLocale = (href, options) => {
|
|
1873
|
+
const targetLocale = (options == null ? void 0 : options.locale) || currentLocale;
|
|
1874
|
+
if (options == null ? void 0 : options.locale) {
|
|
1875
|
+
const { localePrefixedPathname } = localizePathname({
|
|
1876
|
+
config,
|
|
1877
|
+
pathname: href,
|
|
1878
|
+
locale: targetLocale
|
|
1879
|
+
});
|
|
1880
|
+
href = localePrefixedPathname;
|
|
1881
|
+
}
|
|
1882
|
+
if (shouldUseFullReload(config.loaderOptions)) {
|
|
1883
|
+
window.location.href = href;
|
|
1884
|
+
return;
|
|
1885
|
+
} else {
|
|
1886
|
+
setLocale(targetLocale);
|
|
1887
|
+
}
|
|
1888
|
+
push(href, options);
|
|
1889
|
+
};
|
|
1890
|
+
const replaceWithLocale = (href, options) => {
|
|
1891
|
+
const targetLocale = (options == null ? void 0 : options.locale) || currentLocale;
|
|
1892
|
+
if (options == null ? void 0 : options.locale) {
|
|
1893
|
+
const { localePrefixedPathname } = localizePathname({
|
|
1894
|
+
config,
|
|
1895
|
+
pathname: href,
|
|
1896
|
+
locale: targetLocale
|
|
1897
|
+
});
|
|
1898
|
+
href = localePrefixedPathname;
|
|
1899
|
+
}
|
|
1900
|
+
if (shouldUseFullReload(config.loaderOptions)) {
|
|
1901
|
+
window.location.href = href;
|
|
1902
|
+
return;
|
|
1903
|
+
} else {
|
|
1904
|
+
setLocale(targetLocale);
|
|
1905
|
+
}
|
|
1906
|
+
replace(href, options);
|
|
1907
|
+
};
|
|
1908
|
+
return __spreadValues({
|
|
1909
|
+
push: pushWithLocale,
|
|
1910
|
+
replace: replaceWithLocale
|
|
1911
|
+
}, rest);
|
|
1912
|
+
};
|
|
1153
1913
|
|
|
1914
|
+
Object.defineProperty(exports, "Translator", {
|
|
1915
|
+
enumerable: true,
|
|
1916
|
+
get: function () { return intorTranslator.Translator; }
|
|
1917
|
+
});
|
|
1154
1918
|
exports.IntorError = IntorError;
|
|
1155
1919
|
exports.IntorErrorCode = IntorErrorCode;
|
|
1920
|
+
exports.IntorProvider = IntorProvider;
|
|
1921
|
+
exports.Link = Link;
|
|
1922
|
+
exports.TranslateHandlersProvider = TranslateHandlersProvider;
|
|
1156
1923
|
exports.createIntor = createIntor;
|
|
1157
1924
|
exports.defineIntorConfig = defineIntorConfig;
|
|
1158
1925
|
exports.extractPathname = extractPathname;
|
|
1159
1926
|
exports.fetchApiMessages = fetchApiMessages;
|
|
1160
1927
|
exports.intor = intor;
|
|
1928
|
+
exports.intorMiddleware = intorMiddleware;
|
|
1161
1929
|
exports.loadLocalMessages = loadLocalMessages;
|
|
1162
1930
|
exports.mergeStaticAndDynamicMessages = mergeStaticAndDynamicMessages;
|
|
1163
1931
|
exports.normalizeLocale = normalizeLocale;
|
|
@@ -1165,3 +1933,11 @@ exports.normalizePathname = normalizePathname;
|
|
|
1165
1933
|
exports.resolveNamespaces = resolveNamespaces;
|
|
1166
1934
|
exports.resolvePreferredLocale = resolvePreferredLocale;
|
|
1167
1935
|
exports.standardizePathname = standardizePathname;
|
|
1936
|
+
exports.useIntor = useIntor;
|
|
1937
|
+
exports.useIntorConfig = useIntorConfig;
|
|
1938
|
+
exports.useIntorLocale = useIntorLocale;
|
|
1939
|
+
exports.useIntorMessages = useIntorMessages;
|
|
1940
|
+
exports.useIntorTranslator = useIntorTranslator;
|
|
1941
|
+
exports.usePathname = usePathname;
|
|
1942
|
+
exports.useRouter = useRouter;
|
|
1943
|
+
exports.useTranslateHandlers = useTranslateHandlers;
|