intor 1.0.9 → 1.0.11
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 +155 -75
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +154 -74
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
var logry = require('logry');
|
|
4
4
|
var intorTranslator = require('intor-translator');
|
|
5
|
-
var
|
|
5
|
+
var headers = require('next/headers');
|
|
6
|
+
var path = require('path');
|
|
6
7
|
var perf_hooks = require('perf_hooks');
|
|
7
8
|
var pLimit = require('p-limit');
|
|
8
9
|
var fs = require('fs/promises');
|
|
9
10
|
|
|
10
11
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
12
|
|
|
12
|
-
var
|
|
13
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
13
14
|
var pLimit__default = /*#__PURE__*/_interopDefault(pLimit);
|
|
14
15
|
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
15
16
|
|
|
@@ -42,6 +43,7 @@ var initializeLogger = ({
|
|
|
42
43
|
level: loggerOptions == null ? void 0 : loggerOptions.level,
|
|
43
44
|
scope
|
|
44
45
|
}, loggerOptions));
|
|
46
|
+
console.log(loggerOptions);
|
|
45
47
|
return logger;
|
|
46
48
|
};
|
|
47
49
|
|
|
@@ -261,35 +263,164 @@ var defineIntorConfig = (config) => {
|
|
|
261
263
|
prefixPlaceHolder: resolvedPrefixPlaceHolder
|
|
262
264
|
};
|
|
263
265
|
};
|
|
264
|
-
|
|
266
|
+
|
|
267
|
+
// src/adapters/next-client/constants/header-key-constants.ts
|
|
268
|
+
var DEFAULT_PATHNAME_HEADER_KEY = "x-intor-pathname";
|
|
269
|
+
|
|
270
|
+
// src/shared/utils/locale/normalize-locale.ts
|
|
271
|
+
var normalizeLocale = (locale = "", supportedLocales = []) => {
|
|
272
|
+
if (!locale || supportedLocales.length === 0) return void 0;
|
|
273
|
+
const toCanonical = (input) => {
|
|
274
|
+
var _a;
|
|
275
|
+
try {
|
|
276
|
+
return (_a = Intl.getCanonicalLocales(input)[0]) == null ? void 0 : _a.toLowerCase();
|
|
277
|
+
} catch (e) {
|
|
278
|
+
return void 0;
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
const canonicalLocale = toCanonical(locale);
|
|
282
|
+
if (!canonicalLocale) return void 0;
|
|
283
|
+
const supportedCanonicalMap = /* @__PURE__ */ new Map();
|
|
284
|
+
for (const l of supportedLocales) {
|
|
285
|
+
const normalized = toCanonical(l);
|
|
286
|
+
if (normalized) {
|
|
287
|
+
supportedCanonicalMap.set(normalized, l);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (supportedCanonicalMap.has(canonicalLocale)) {
|
|
291
|
+
return supportedCanonicalMap.get(canonicalLocale);
|
|
292
|
+
}
|
|
293
|
+
const baseLang = canonicalLocale.split("-")[0];
|
|
294
|
+
for (const [key, original] of supportedCanonicalMap) {
|
|
295
|
+
const supportedBase = key.split("-")[0];
|
|
296
|
+
if (supportedBase === baseLang) {
|
|
297
|
+
return original;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return void 0;
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
// src/shared/utils/locale/resolve-preferred-locale.ts
|
|
304
|
+
var resolvePreferredLocale = (acceptLanguageHeader, supportedLocales) => {
|
|
305
|
+
var _a;
|
|
306
|
+
if (!acceptLanguageHeader || !supportedLocales || supportedLocales.length === 0) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const supportedLocalesSet = new Set(supportedLocales);
|
|
310
|
+
const preferred = (_a = acceptLanguageHeader.split(",").map((part) => {
|
|
311
|
+
const [lang, qValue] = part.split(";");
|
|
312
|
+
const q = qValue ? parseFloat(qValue.split("=")[1]) : 1;
|
|
313
|
+
if (isNaN(q)) {
|
|
314
|
+
return { lang: lang.trim(), q: 0 };
|
|
315
|
+
}
|
|
316
|
+
return { lang: lang.trim(), q };
|
|
317
|
+
}).sort((a, b) => b.q - a.q).find(({ lang }) => supportedLocalesSet.has(lang))) == null ? void 0 : _a.lang;
|
|
318
|
+
return preferred;
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
// src/adapters/next-client/next-client-runtime/next-client-runtime.ts
|
|
322
|
+
var nextClientRuntime = async ({
|
|
265
323
|
config
|
|
266
324
|
}) => {
|
|
267
|
-
|
|
325
|
+
var _a;
|
|
268
326
|
const logger = logry.logry({
|
|
269
327
|
id: config.id,
|
|
270
|
-
scope: "
|
|
328
|
+
scope: "nextClientRuntime"
|
|
271
329
|
});
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
);
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
330
|
+
const { defaultLocale, supportedLocales = [], cookie, routing } = config;
|
|
331
|
+
let locale = void 0;
|
|
332
|
+
if (!cookie.disabled) {
|
|
333
|
+
const localeFromCookie = (_a = (await headers.cookies()).get(cookie.name)) == null ? void 0 : _a.value;
|
|
334
|
+
locale = normalizeLocale(localeFromCookie, supportedLocales);
|
|
335
|
+
if (locale) {
|
|
336
|
+
void logger.debug("Get locale from cookie:", { locale });
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (!locale && routing.firstVisit.localeSource === "browser") {
|
|
340
|
+
const aLHeader = (await headers.headers()).get("accept-language") || void 0;
|
|
341
|
+
const preferredLocale = resolvePreferredLocale(aLHeader, supportedLocales);
|
|
342
|
+
locale = normalizeLocale(preferredLocale, supportedLocales);
|
|
343
|
+
void logger.debug("Get locale from browser:", { locale });
|
|
344
|
+
}
|
|
345
|
+
const pathname = (await headers.headers()).get(DEFAULT_PATHNAME_HEADER_KEY);
|
|
346
|
+
if (pathname) {
|
|
347
|
+
void logger.debug("Get pathname from next headers:", { pathname });
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
locale: locale || defaultLocale,
|
|
351
|
+
pathname: pathname || ""
|
|
352
|
+
};
|
|
353
|
+
};
|
|
354
|
+
var nextServerRuntime = async ({
|
|
355
|
+
config,
|
|
356
|
+
request
|
|
357
|
+
}) => {
|
|
358
|
+
var _a;
|
|
359
|
+
const logger = logry.logry({
|
|
360
|
+
id: config.id,
|
|
361
|
+
scope: "nextServerRuntime"
|
|
362
|
+
});
|
|
363
|
+
const { defaultLocale, supportedLocales = [], cookie, routing } = config;
|
|
364
|
+
let locale = void 0;
|
|
365
|
+
if (!cookie.disabled) {
|
|
366
|
+
const localeFromCookie = (_a = (await headers.cookies()).get(cookie.name)) == null ? void 0 : _a.value;
|
|
367
|
+
locale = normalizeLocale(localeFromCookie, supportedLocales);
|
|
368
|
+
if (locale) {
|
|
369
|
+
void logger.debug("Get locale from cookie:", { locale });
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (!locale && routing.firstVisit.localeSource === "browser") {
|
|
373
|
+
const aLHeader = (await headers.headers()).get("accept-language") || void 0;
|
|
374
|
+
const preferredLocale = resolvePreferredLocale(aLHeader, supportedLocales);
|
|
375
|
+
locale = normalizeLocale(preferredLocale, supportedLocales);
|
|
376
|
+
void logger.debug("Get locale from browser:", { locale });
|
|
377
|
+
}
|
|
378
|
+
let pathname = "";
|
|
379
|
+
if (request && typeof request === "object" && "nextUrl" in request) {
|
|
380
|
+
pathname = request.nextUrl.pathname;
|
|
381
|
+
if (pathname) {
|
|
382
|
+
void logger.debug("Get pathname from next request:", { pathname });
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
if (!cookie.disabled && cookie.autoSetCookie) {
|
|
386
|
+
(await headers.cookies()).set(__spreadProps(__spreadValues({
|
|
387
|
+
name: cookie.name,
|
|
388
|
+
value: locale || defaultLocale
|
|
389
|
+
}, cookie.domain ? { domain: cookie.domain } : {}), {
|
|
390
|
+
path: cookie.path,
|
|
391
|
+
maxAge: cookie.maxAge,
|
|
392
|
+
httpOnly: cookie.httpOnly,
|
|
393
|
+
secure: cookie.secure,
|
|
394
|
+
sameSite: cookie.sameSite
|
|
395
|
+
}));
|
|
396
|
+
void logger.debug("Set locale to cookie:", {
|
|
397
|
+
cookie: { name: cookie.name, value: locale || defaultLocale }
|
|
281
398
|
});
|
|
282
399
|
}
|
|
283
|
-
return
|
|
400
|
+
return {
|
|
401
|
+
locale: locale || defaultLocale,
|
|
402
|
+
pathname: pathname || ""
|
|
403
|
+
};
|
|
284
404
|
};
|
|
285
|
-
|
|
286
|
-
|
|
405
|
+
|
|
406
|
+
// src/modules/intor-adapter/resolve-adapter-runtime-loader.ts
|
|
407
|
+
var resolceAdapterRuntimeLoader = async ({
|
|
408
|
+
config
|
|
409
|
+
}) => {
|
|
410
|
+
const { adapter } = config;
|
|
411
|
+
let loadedRuntime;
|
|
412
|
+
if (adapter === "next-client") {
|
|
413
|
+
loadedRuntime = nextClientRuntime;
|
|
414
|
+
} else if (adapter === "next-server") {
|
|
415
|
+
loadedRuntime = nextServerRuntime;
|
|
416
|
+
}
|
|
417
|
+
return loadedRuntime;
|
|
287
418
|
};
|
|
288
419
|
|
|
289
420
|
// src/modules/intor-config/types/intor-adapter-types.ts
|
|
290
421
|
var intorAdapters = ["next-client", "next-server"];
|
|
291
422
|
var readMessageRecordFile = async (filePath) => {
|
|
292
|
-
const fileName =
|
|
423
|
+
const fileName = path__default.default.basename(filePath, ".json");
|
|
293
424
|
const content = await fs__default.default.readFile(filePath, "utf-8");
|
|
294
425
|
const parsed = JSON.parse(content);
|
|
295
426
|
if (typeof parsed !== "object" || parsed === null) {
|
|
@@ -311,7 +442,7 @@ var parseMessageFile = async (filePath, loggerId) => {
|
|
|
311
442
|
logger == null ? void 0 : logger.warn("Invalid file path provided.", { filePath: trimmedPath });
|
|
312
443
|
return null;
|
|
313
444
|
}
|
|
314
|
-
const fileName =
|
|
445
|
+
const fileName = path__default.default.basename(trimmedPath);
|
|
315
446
|
if (!fileName.toLowerCase().endsWith(".json")) {
|
|
316
447
|
logger == null ? void 0 : logger.debug(`Skipped non-JSON file.`, { filePath: trimmedPath });
|
|
317
448
|
return null;
|
|
@@ -332,7 +463,7 @@ var mergeNamespaceMessages = async (filePaths, isAtRoot, loggerId) => {
|
|
|
332
463
|
const subEntries = {};
|
|
333
464
|
await Promise.all(
|
|
334
465
|
filePaths.map(async (filePath) => {
|
|
335
|
-
const fileName =
|
|
466
|
+
const fileName = path__default.default.basename(filePath);
|
|
336
467
|
const content = await parseMessageFile(filePath, loggerId);
|
|
337
468
|
if (!content) {
|
|
338
469
|
return;
|
|
@@ -396,7 +527,7 @@ var addToNamespaceGroup = ({
|
|
|
396
527
|
return;
|
|
397
528
|
}
|
|
398
529
|
const isAtRoot = namespacePathSegments.length === 0;
|
|
399
|
-
const nsKey = isAtRoot ?
|
|
530
|
+
const nsKey = isAtRoot ? path__default.default.basename(filePath, ".json") : namespacePathSegments.join(".");
|
|
400
531
|
if (namespaces && namespaces.size > 0 && !namespaces.has(nsKey)) {
|
|
401
532
|
return;
|
|
402
533
|
}
|
|
@@ -425,7 +556,7 @@ var traverseDirectory = async ({
|
|
|
425
556
|
const dirents = await fs__default.default.readdir(currentDirPath, { withFileTypes: true });
|
|
426
557
|
const dirPromises = dirents.map(
|
|
427
558
|
(dirent) => limit(async () => {
|
|
428
|
-
const filePath =
|
|
559
|
+
const filePath = path__default.default.join(currentDirPath, dirent.name);
|
|
429
560
|
if (dirent.isFile() && dirent.name.endsWith(".json")) {
|
|
430
561
|
addToNamespaceGroup({
|
|
431
562
|
namespaceGroups,
|
|
@@ -479,7 +610,7 @@ var loadSingleLocale = async ({
|
|
|
479
610
|
loggerId
|
|
480
611
|
}) => {
|
|
481
612
|
const logger = logry.logry({ id: loggerId, scope: "loadSingleLocale" });
|
|
482
|
-
const localePath =
|
|
613
|
+
const localePath = path__default.default.join(basePath, locale);
|
|
483
614
|
const validNamespaces = [];
|
|
484
615
|
try {
|
|
485
616
|
const stat = await fs__default.default.stat(localePath);
|
|
@@ -580,7 +711,7 @@ var loadLocalMessages = async ({
|
|
|
580
711
|
return {};
|
|
581
712
|
}
|
|
582
713
|
const logger = logry.logry({ id: loggerId, scope: "loadLocalMessages" });
|
|
583
|
-
const resolvedBasePath =
|
|
714
|
+
const resolvedBasePath = path__default.default.resolve(
|
|
584
715
|
process.cwd(),
|
|
585
716
|
normalizePathname(basePath, { removeLeadingSlash: true })
|
|
586
717
|
);
|
|
@@ -1017,57 +1148,6 @@ var intor = async ({
|
|
|
1017
1148
|
}
|
|
1018
1149
|
};
|
|
1019
1150
|
|
|
1020
|
-
// src/shared/utils/locale/normalize-locale.ts
|
|
1021
|
-
var normalizeLocale = (locale = "", supportedLocales = []) => {
|
|
1022
|
-
if (!locale || supportedLocales.length === 0) return void 0;
|
|
1023
|
-
const toCanonical = (input) => {
|
|
1024
|
-
var _a;
|
|
1025
|
-
try {
|
|
1026
|
-
return (_a = Intl.getCanonicalLocales(input)[0]) == null ? void 0 : _a.toLowerCase();
|
|
1027
|
-
} catch (e) {
|
|
1028
|
-
return void 0;
|
|
1029
|
-
}
|
|
1030
|
-
};
|
|
1031
|
-
const canonicalLocale = toCanonical(locale);
|
|
1032
|
-
if (!canonicalLocale) return void 0;
|
|
1033
|
-
const supportedCanonicalMap = /* @__PURE__ */ new Map();
|
|
1034
|
-
for (const l of supportedLocales) {
|
|
1035
|
-
const normalized = toCanonical(l);
|
|
1036
|
-
if (normalized) {
|
|
1037
|
-
supportedCanonicalMap.set(normalized, l);
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
if (supportedCanonicalMap.has(canonicalLocale)) {
|
|
1041
|
-
return supportedCanonicalMap.get(canonicalLocale);
|
|
1042
|
-
}
|
|
1043
|
-
const baseLang = canonicalLocale.split("-")[0];
|
|
1044
|
-
for (const [key, original] of supportedCanonicalMap) {
|
|
1045
|
-
const supportedBase = key.split("-")[0];
|
|
1046
|
-
if (supportedBase === baseLang) {
|
|
1047
|
-
return original;
|
|
1048
|
-
}
|
|
1049
|
-
}
|
|
1050
|
-
return void 0;
|
|
1051
|
-
};
|
|
1052
|
-
|
|
1053
|
-
// src/shared/utils/locale/resolve-preferred-locale.ts
|
|
1054
|
-
var resolvePreferredLocale = (acceptLanguageHeader, supportedLocales) => {
|
|
1055
|
-
var _a;
|
|
1056
|
-
if (!acceptLanguageHeader || !supportedLocales || supportedLocales.length === 0) {
|
|
1057
|
-
return;
|
|
1058
|
-
}
|
|
1059
|
-
const supportedLocalesSet = new Set(supportedLocales);
|
|
1060
|
-
const preferred = (_a = acceptLanguageHeader.split(",").map((part) => {
|
|
1061
|
-
const [lang, qValue] = part.split(";");
|
|
1062
|
-
const q = qValue ? parseFloat(qValue.split("=")[1]) : 1;
|
|
1063
|
-
if (isNaN(q)) {
|
|
1064
|
-
return { lang: lang.trim(), q: 0 };
|
|
1065
|
-
}
|
|
1066
|
-
return { lang: lang.trim(), q };
|
|
1067
|
-
}).sort((a, b) => b.q - a.q).find(({ lang }) => supportedLocalesSet.has(lang))) == null ? void 0 : _a.lang;
|
|
1068
|
-
return preferred;
|
|
1069
|
-
};
|
|
1070
|
-
|
|
1071
1151
|
exports.IntorError = IntorError;
|
|
1072
1152
|
exports.IntorErrorCode = IntorErrorCode;
|
|
1073
1153
|
exports.defineIntorConfig = defineIntorConfig;
|
package/dist/index.d.cts
CHANGED
|
@@ -117,7 +117,7 @@ type IntorOptions = {
|
|
|
117
117
|
translateHandlers?: TranslateHandlers;
|
|
118
118
|
};
|
|
119
119
|
type IntorNextClientClientResult = IntorRuntimeResult;
|
|
120
|
-
type IntorNextServerServerResult = Translator<
|
|
120
|
+
type IntorNextServerServerResult = Translator<unknown>;
|
|
121
121
|
type IntorResult = IntorNextClientClientResult & IntorNextServerServerResult;
|
|
122
122
|
|
|
123
123
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -117,7 +117,7 @@ type IntorOptions = {
|
|
|
117
117
|
translateHandlers?: TranslateHandlers;
|
|
118
118
|
};
|
|
119
119
|
type IntorNextClientClientResult = IntorRuntimeResult;
|
|
120
|
-
type IntorNextServerServerResult = Translator<
|
|
120
|
+
type IntorNextServerServerResult = Translator<unknown>;
|
|
121
121
|
type IntorResult = IntorNextClientClientResult & IntorNextServerServerResult;
|
|
122
122
|
|
|
123
123
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { logry } from 'logry';
|
|
2
2
|
import { Translator } from 'intor-translator';
|
|
3
|
-
import
|
|
3
|
+
import { cookies, headers } from 'next/headers';
|
|
4
|
+
import path from 'path';
|
|
4
5
|
import { performance } from 'perf_hooks';
|
|
5
6
|
import pLimit from 'p-limit';
|
|
6
7
|
import fs from 'fs/promises';
|
|
@@ -34,6 +35,7 @@ var initializeLogger = ({
|
|
|
34
35
|
level: loggerOptions == null ? void 0 : loggerOptions.level,
|
|
35
36
|
scope
|
|
36
37
|
}, loggerOptions));
|
|
38
|
+
console.log(loggerOptions);
|
|
37
39
|
return logger;
|
|
38
40
|
};
|
|
39
41
|
|
|
@@ -253,35 +255,164 @@ var defineIntorConfig = (config) => {
|
|
|
253
255
|
prefixPlaceHolder: resolvedPrefixPlaceHolder
|
|
254
256
|
};
|
|
255
257
|
};
|
|
256
|
-
|
|
258
|
+
|
|
259
|
+
// src/adapters/next-client/constants/header-key-constants.ts
|
|
260
|
+
var DEFAULT_PATHNAME_HEADER_KEY = "x-intor-pathname";
|
|
261
|
+
|
|
262
|
+
// src/shared/utils/locale/normalize-locale.ts
|
|
263
|
+
var normalizeLocale = (locale = "", supportedLocales = []) => {
|
|
264
|
+
if (!locale || supportedLocales.length === 0) return void 0;
|
|
265
|
+
const toCanonical = (input) => {
|
|
266
|
+
var _a;
|
|
267
|
+
try {
|
|
268
|
+
return (_a = Intl.getCanonicalLocales(input)[0]) == null ? void 0 : _a.toLowerCase();
|
|
269
|
+
} catch (e) {
|
|
270
|
+
return void 0;
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
const canonicalLocale = toCanonical(locale);
|
|
274
|
+
if (!canonicalLocale) return void 0;
|
|
275
|
+
const supportedCanonicalMap = /* @__PURE__ */ new Map();
|
|
276
|
+
for (const l of supportedLocales) {
|
|
277
|
+
const normalized = toCanonical(l);
|
|
278
|
+
if (normalized) {
|
|
279
|
+
supportedCanonicalMap.set(normalized, l);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (supportedCanonicalMap.has(canonicalLocale)) {
|
|
283
|
+
return supportedCanonicalMap.get(canonicalLocale);
|
|
284
|
+
}
|
|
285
|
+
const baseLang = canonicalLocale.split("-")[0];
|
|
286
|
+
for (const [key, original] of supportedCanonicalMap) {
|
|
287
|
+
const supportedBase = key.split("-")[0];
|
|
288
|
+
if (supportedBase === baseLang) {
|
|
289
|
+
return original;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return void 0;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
// src/shared/utils/locale/resolve-preferred-locale.ts
|
|
296
|
+
var resolvePreferredLocale = (acceptLanguageHeader, supportedLocales) => {
|
|
297
|
+
var _a;
|
|
298
|
+
if (!acceptLanguageHeader || !supportedLocales || supportedLocales.length === 0) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const supportedLocalesSet = new Set(supportedLocales);
|
|
302
|
+
const preferred = (_a = acceptLanguageHeader.split(",").map((part) => {
|
|
303
|
+
const [lang, qValue] = part.split(";");
|
|
304
|
+
const q = qValue ? parseFloat(qValue.split("=")[1]) : 1;
|
|
305
|
+
if (isNaN(q)) {
|
|
306
|
+
return { lang: lang.trim(), q: 0 };
|
|
307
|
+
}
|
|
308
|
+
return { lang: lang.trim(), q };
|
|
309
|
+
}).sort((a, b) => b.q - a.q).find(({ lang }) => supportedLocalesSet.has(lang))) == null ? void 0 : _a.lang;
|
|
310
|
+
return preferred;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
// src/adapters/next-client/next-client-runtime/next-client-runtime.ts
|
|
314
|
+
var nextClientRuntime = async ({
|
|
257
315
|
config
|
|
258
316
|
}) => {
|
|
259
|
-
|
|
317
|
+
var _a;
|
|
260
318
|
const logger = logry({
|
|
261
319
|
id: config.id,
|
|
262
|
-
scope: "
|
|
320
|
+
scope: "nextClientRuntime"
|
|
263
321
|
});
|
|
264
|
-
const
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
);
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
322
|
+
const { defaultLocale, supportedLocales = [], cookie, routing } = config;
|
|
323
|
+
let locale = void 0;
|
|
324
|
+
if (!cookie.disabled) {
|
|
325
|
+
const localeFromCookie = (_a = (await cookies()).get(cookie.name)) == null ? void 0 : _a.value;
|
|
326
|
+
locale = normalizeLocale(localeFromCookie, supportedLocales);
|
|
327
|
+
if (locale) {
|
|
328
|
+
void logger.debug("Get locale from cookie:", { locale });
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (!locale && routing.firstVisit.localeSource === "browser") {
|
|
332
|
+
const aLHeader = (await headers()).get("accept-language") || void 0;
|
|
333
|
+
const preferredLocale = resolvePreferredLocale(aLHeader, supportedLocales);
|
|
334
|
+
locale = normalizeLocale(preferredLocale, supportedLocales);
|
|
335
|
+
void logger.debug("Get locale from browser:", { locale });
|
|
336
|
+
}
|
|
337
|
+
const pathname = (await headers()).get(DEFAULT_PATHNAME_HEADER_KEY);
|
|
338
|
+
if (pathname) {
|
|
339
|
+
void logger.debug("Get pathname from next headers:", { pathname });
|
|
340
|
+
}
|
|
341
|
+
return {
|
|
342
|
+
locale: locale || defaultLocale,
|
|
343
|
+
pathname: pathname || ""
|
|
344
|
+
};
|
|
345
|
+
};
|
|
346
|
+
var nextServerRuntime = async ({
|
|
347
|
+
config,
|
|
348
|
+
request
|
|
349
|
+
}) => {
|
|
350
|
+
var _a;
|
|
351
|
+
const logger = logry({
|
|
352
|
+
id: config.id,
|
|
353
|
+
scope: "nextServerRuntime"
|
|
354
|
+
});
|
|
355
|
+
const { defaultLocale, supportedLocales = [], cookie, routing } = config;
|
|
356
|
+
let locale = void 0;
|
|
357
|
+
if (!cookie.disabled) {
|
|
358
|
+
const localeFromCookie = (_a = (await cookies()).get(cookie.name)) == null ? void 0 : _a.value;
|
|
359
|
+
locale = normalizeLocale(localeFromCookie, supportedLocales);
|
|
360
|
+
if (locale) {
|
|
361
|
+
void logger.debug("Get locale from cookie:", { locale });
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
if (!locale && routing.firstVisit.localeSource === "browser") {
|
|
365
|
+
const aLHeader = (await headers()).get("accept-language") || void 0;
|
|
366
|
+
const preferredLocale = resolvePreferredLocale(aLHeader, supportedLocales);
|
|
367
|
+
locale = normalizeLocale(preferredLocale, supportedLocales);
|
|
368
|
+
void logger.debug("Get locale from browser:", { locale });
|
|
369
|
+
}
|
|
370
|
+
let pathname = "";
|
|
371
|
+
if (request && typeof request === "object" && "nextUrl" in request) {
|
|
372
|
+
pathname = request.nextUrl.pathname;
|
|
373
|
+
if (pathname) {
|
|
374
|
+
void logger.debug("Get pathname from next request:", { pathname });
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (!cookie.disabled && cookie.autoSetCookie) {
|
|
378
|
+
(await cookies()).set(__spreadProps(__spreadValues({
|
|
379
|
+
name: cookie.name,
|
|
380
|
+
value: locale || defaultLocale
|
|
381
|
+
}, cookie.domain ? { domain: cookie.domain } : {}), {
|
|
382
|
+
path: cookie.path,
|
|
383
|
+
maxAge: cookie.maxAge,
|
|
384
|
+
httpOnly: cookie.httpOnly,
|
|
385
|
+
secure: cookie.secure,
|
|
386
|
+
sameSite: cookie.sameSite
|
|
387
|
+
}));
|
|
388
|
+
void logger.debug("Set locale to cookie:", {
|
|
389
|
+
cookie: { name: cookie.name, value: locale || defaultLocale }
|
|
273
390
|
});
|
|
274
391
|
}
|
|
275
|
-
return
|
|
392
|
+
return {
|
|
393
|
+
locale: locale || defaultLocale,
|
|
394
|
+
pathname: pathname || ""
|
|
395
|
+
};
|
|
276
396
|
};
|
|
277
|
-
|
|
278
|
-
|
|
397
|
+
|
|
398
|
+
// src/modules/intor-adapter/resolve-adapter-runtime-loader.ts
|
|
399
|
+
var resolceAdapterRuntimeLoader = async ({
|
|
400
|
+
config
|
|
401
|
+
}) => {
|
|
402
|
+
const { adapter } = config;
|
|
403
|
+
let loadedRuntime;
|
|
404
|
+
if (adapter === "next-client") {
|
|
405
|
+
loadedRuntime = nextClientRuntime;
|
|
406
|
+
} else if (adapter === "next-server") {
|
|
407
|
+
loadedRuntime = nextServerRuntime;
|
|
408
|
+
}
|
|
409
|
+
return loadedRuntime;
|
|
279
410
|
};
|
|
280
411
|
|
|
281
412
|
// src/modules/intor-config/types/intor-adapter-types.ts
|
|
282
413
|
var intorAdapters = ["next-client", "next-server"];
|
|
283
414
|
var readMessageRecordFile = async (filePath) => {
|
|
284
|
-
const fileName =
|
|
415
|
+
const fileName = path.basename(filePath, ".json");
|
|
285
416
|
const content = await fs.readFile(filePath, "utf-8");
|
|
286
417
|
const parsed = JSON.parse(content);
|
|
287
418
|
if (typeof parsed !== "object" || parsed === null) {
|
|
@@ -303,7 +434,7 @@ var parseMessageFile = async (filePath, loggerId) => {
|
|
|
303
434
|
logger == null ? void 0 : logger.warn("Invalid file path provided.", { filePath: trimmedPath });
|
|
304
435
|
return null;
|
|
305
436
|
}
|
|
306
|
-
const fileName =
|
|
437
|
+
const fileName = path.basename(trimmedPath);
|
|
307
438
|
if (!fileName.toLowerCase().endsWith(".json")) {
|
|
308
439
|
logger == null ? void 0 : logger.debug(`Skipped non-JSON file.`, { filePath: trimmedPath });
|
|
309
440
|
return null;
|
|
@@ -324,7 +455,7 @@ var mergeNamespaceMessages = async (filePaths, isAtRoot, loggerId) => {
|
|
|
324
455
|
const subEntries = {};
|
|
325
456
|
await Promise.all(
|
|
326
457
|
filePaths.map(async (filePath) => {
|
|
327
|
-
const fileName =
|
|
458
|
+
const fileName = path.basename(filePath);
|
|
328
459
|
const content = await parseMessageFile(filePath, loggerId);
|
|
329
460
|
if (!content) {
|
|
330
461
|
return;
|
|
@@ -388,7 +519,7 @@ var addToNamespaceGroup = ({
|
|
|
388
519
|
return;
|
|
389
520
|
}
|
|
390
521
|
const isAtRoot = namespacePathSegments.length === 0;
|
|
391
|
-
const nsKey = isAtRoot ?
|
|
522
|
+
const nsKey = isAtRoot ? path.basename(filePath, ".json") : namespacePathSegments.join(".");
|
|
392
523
|
if (namespaces && namespaces.size > 0 && !namespaces.has(nsKey)) {
|
|
393
524
|
return;
|
|
394
525
|
}
|
|
@@ -417,7 +548,7 @@ var traverseDirectory = async ({
|
|
|
417
548
|
const dirents = await fs.readdir(currentDirPath, { withFileTypes: true });
|
|
418
549
|
const dirPromises = dirents.map(
|
|
419
550
|
(dirent) => limit(async () => {
|
|
420
|
-
const filePath =
|
|
551
|
+
const filePath = path.join(currentDirPath, dirent.name);
|
|
421
552
|
if (dirent.isFile() && dirent.name.endsWith(".json")) {
|
|
422
553
|
addToNamespaceGroup({
|
|
423
554
|
namespaceGroups,
|
|
@@ -471,7 +602,7 @@ var loadSingleLocale = async ({
|
|
|
471
602
|
loggerId
|
|
472
603
|
}) => {
|
|
473
604
|
const logger = logry({ id: loggerId, scope: "loadSingleLocale" });
|
|
474
|
-
const localePath =
|
|
605
|
+
const localePath = path.join(basePath, locale);
|
|
475
606
|
const validNamespaces = [];
|
|
476
607
|
try {
|
|
477
608
|
const stat = await fs.stat(localePath);
|
|
@@ -572,7 +703,7 @@ var loadLocalMessages = async ({
|
|
|
572
703
|
return {};
|
|
573
704
|
}
|
|
574
705
|
const logger = logry({ id: loggerId, scope: "loadLocalMessages" });
|
|
575
|
-
const resolvedBasePath =
|
|
706
|
+
const resolvedBasePath = path.resolve(
|
|
576
707
|
process.cwd(),
|
|
577
708
|
normalizePathname(basePath, { removeLeadingSlash: true })
|
|
578
709
|
);
|
|
@@ -1009,55 +1140,4 @@ var intor = async ({
|
|
|
1009
1140
|
}
|
|
1010
1141
|
};
|
|
1011
1142
|
|
|
1012
|
-
// src/shared/utils/locale/normalize-locale.ts
|
|
1013
|
-
var normalizeLocale = (locale = "", supportedLocales = []) => {
|
|
1014
|
-
if (!locale || supportedLocales.length === 0) return void 0;
|
|
1015
|
-
const toCanonical = (input) => {
|
|
1016
|
-
var _a;
|
|
1017
|
-
try {
|
|
1018
|
-
return (_a = Intl.getCanonicalLocales(input)[0]) == null ? void 0 : _a.toLowerCase();
|
|
1019
|
-
} catch (e) {
|
|
1020
|
-
return void 0;
|
|
1021
|
-
}
|
|
1022
|
-
};
|
|
1023
|
-
const canonicalLocale = toCanonical(locale);
|
|
1024
|
-
if (!canonicalLocale) return void 0;
|
|
1025
|
-
const supportedCanonicalMap = /* @__PURE__ */ new Map();
|
|
1026
|
-
for (const l of supportedLocales) {
|
|
1027
|
-
const normalized = toCanonical(l);
|
|
1028
|
-
if (normalized) {
|
|
1029
|
-
supportedCanonicalMap.set(normalized, l);
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
if (supportedCanonicalMap.has(canonicalLocale)) {
|
|
1033
|
-
return supportedCanonicalMap.get(canonicalLocale);
|
|
1034
|
-
}
|
|
1035
|
-
const baseLang = canonicalLocale.split("-")[0];
|
|
1036
|
-
for (const [key, original] of supportedCanonicalMap) {
|
|
1037
|
-
const supportedBase = key.split("-")[0];
|
|
1038
|
-
if (supportedBase === baseLang) {
|
|
1039
|
-
return original;
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
return void 0;
|
|
1043
|
-
};
|
|
1044
|
-
|
|
1045
|
-
// src/shared/utils/locale/resolve-preferred-locale.ts
|
|
1046
|
-
var resolvePreferredLocale = (acceptLanguageHeader, supportedLocales) => {
|
|
1047
|
-
var _a;
|
|
1048
|
-
if (!acceptLanguageHeader || !supportedLocales || supportedLocales.length === 0) {
|
|
1049
|
-
return;
|
|
1050
|
-
}
|
|
1051
|
-
const supportedLocalesSet = new Set(supportedLocales);
|
|
1052
|
-
const preferred = (_a = acceptLanguageHeader.split(",").map((part) => {
|
|
1053
|
-
const [lang, qValue] = part.split(";");
|
|
1054
|
-
const q = qValue ? parseFloat(qValue.split("=")[1]) : 1;
|
|
1055
|
-
if (isNaN(q)) {
|
|
1056
|
-
return { lang: lang.trim(), q: 0 };
|
|
1057
|
-
}
|
|
1058
|
-
return { lang: lang.trim(), q };
|
|
1059
|
-
}).sort((a, b) => b.q - a.q).find(({ lang }) => supportedLocalesSet.has(lang))) == null ? void 0 : _a.lang;
|
|
1060
|
-
return preferred;
|
|
1061
|
-
};
|
|
1062
|
-
|
|
1063
1143
|
export { IntorError, IntorErrorCode, defineIntorConfig, extractPathname, fetchApiMessages, intor, loadLocalMessages, mergeStaticAndDynamicMessages, normalizeLocale, normalizePathname, resolveNamespaces, resolvePreferredLocale, standardizePathname };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "intor",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"description": "A modular and extensible i18n core designed for TypeScript and JavaScript projects. Intor enables custom translation logic with support for both frontend and backend environments, featuring runtime configuration, caching, adapters, and message loaders.",
|
|
5
5
|
"author": "Yiming Liao",
|
|
6
6
|
"license": "MIT",
|