next-intlayer 3.5.0 → 3.5.1
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/cjs/client/IntlayerClientProvider.cjs +36 -0
- package/dist/cjs/client/IntlayerClientProvider.cjs.map +1 -0
- package/dist/cjs/client/index.cjs +46 -0
- package/dist/cjs/client/index.cjs.map +1 -0
- package/dist/cjs/client/useLocale.cjs +101 -0
- package/dist/cjs/client/useLocale.cjs.map +1 -0
- package/dist/cjs/client/useLocalePageRouter.cjs +100 -0
- package/dist/cjs/client/useLocalePageRouter.cjs.map +1 -0
- package/dist/cjs/generateStaticParams.cjs +31 -0
- package/dist/cjs/generateStaticParams.cjs.map +1 -0
- package/dist/cjs/index.cjs +51 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/middleware/index.cjs +23 -0
- package/dist/cjs/middleware/index.cjs.map +1 -0
- package/dist/cjs/middleware/intlayerMiddleware.cjs +177 -0
- package/dist/cjs/middleware/intlayerMiddleware.cjs.map +1 -0
- package/dist/cjs/middleware/localeDetector.cjs +35 -0
- package/dist/cjs/middleware/localeDetector.cjs.map +1 -0
- package/dist/cjs/server/index.cjs +46 -0
- package/dist/cjs/server/index.cjs.map +1 -0
- package/dist/cjs/server/withIntlayer.cjs +91 -0
- package/dist/cjs/server/withIntlayer.cjs.map +1 -0
- package/dist/cjs/types/NextPage.cjs +17 -0
- package/dist/cjs/types/NextPage.cjs.map +1 -0
- package/dist/cjs/types/index.cjs +17 -0
- package/dist/cjs/types/index.cjs.map +1 -0
- package/dist/esm/client/IntlayerClientProvider.mjs +12 -0
- package/dist/esm/client/IntlayerClientProvider.mjs.map +1 -0
- package/dist/esm/client/index.mjs +23 -0
- package/dist/esm/client/index.mjs.map +1 -0
- package/dist/esm/client/useLocale.mjs +77 -0
- package/dist/esm/client/useLocale.mjs.map +1 -0
- package/dist/esm/client/useLocalePageRouter.mjs +76 -0
- package/dist/esm/client/useLocalePageRouter.mjs.map +1 -0
- package/dist/esm/generateStaticParams.mjs +7 -0
- package/dist/esm/generateStaticParams.mjs.map +1 -0
- package/dist/esm/index.mjs +29 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/middleware/index.mjs +2 -0
- package/dist/esm/middleware/index.mjs.map +1 -0
- package/dist/esm/middleware/intlayerMiddleware.mjs +155 -0
- package/dist/esm/middleware/intlayerMiddleware.mjs.map +1 -0
- package/dist/esm/middleware/localeDetector.mjs +11 -0
- package/dist/esm/middleware/localeDetector.mjs.map +1 -0
- package/dist/esm/server/index.mjs +23 -0
- package/dist/esm/server/index.mjs.map +1 -0
- package/dist/esm/server/withIntlayer.mjs +67 -0
- package/dist/esm/server/withIntlayer.mjs.map +1 -0
- package/dist/esm/types/NextPage.mjs +1 -0
- package/dist/esm/types/NextPage.mjs.map +1 -0
- package/dist/esm/types/index.mjs +1 -0
- package/dist/esm/types/index.mjs.map +1 -0
- package/dist/types/client/IntlayerClientProvider.d.ts +5 -0
- package/dist/types/client/IntlayerClientProvider.d.ts.map +1 -0
- package/dist/types/client/index.d.ts +5 -0
- package/dist/types/client/index.d.ts.map +1 -0
- package/dist/types/client/useLocale.d.ts +10 -0
- package/dist/types/client/useLocale.d.ts.map +1 -0
- package/dist/types/client/useLocalePageRouter.d.ts +10 -0
- package/dist/types/client/useLocalePageRouter.d.ts.map +1 -0
- package/dist/types/generateStaticParams.d.ts +4 -0
- package/dist/types/generateStaticParams.d.ts.map +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/middleware/index.d.ts +2 -0
- package/dist/types/middleware/index.d.ts.map +1 -0
- package/dist/types/middleware/intlayerMiddleware.d.ts +26 -0
- package/dist/types/middleware/intlayerMiddleware.d.ts.map +1 -0
- package/dist/types/middleware/localeDetector.d.ts +9 -0
- package/dist/types/middleware/localeDetector.d.ts.map +1 -0
- package/dist/types/server/index.d.ts +3 -0
- package/dist/types/server/index.d.ts.map +1 -0
- package/dist/types/server/withIntlayer.d.ts +15 -0
- package/dist/types/server/withIntlayer.d.ts.map +1 -0
- package/dist/types/types/NextPage.d.ts +20 -0
- package/dist/types/types/NextPage.d.ts.map +1 -0
- package/dist/types/types/index.d.ts +2 -0
- package/dist/types/types/index.d.ts.map +1 -0
- package/package.json +15 -15
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var IntlayerClientProvider_exports = {};
|
|
21
|
+
__export(IntlayerClientProvider_exports, {
|
|
22
|
+
IntlayerClientProvider: () => IntlayerClientProvider
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(IntlayerClientProvider_exports);
|
|
25
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
26
|
+
var import_react_intlayer = require("react-intlayer");
|
|
27
|
+
var import_useLocale = require('./useLocale.cjs');
|
|
28
|
+
const IntlayerClientProvider = (props) => {
|
|
29
|
+
const { setLocale } = (0, import_useLocale.useLocale)();
|
|
30
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_intlayer.IntlayerProvider, { setLocale, ...props });
|
|
31
|
+
};
|
|
32
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
33
|
+
0 && (module.exports = {
|
|
34
|
+
IntlayerClientProvider
|
|
35
|
+
});
|
|
36
|
+
//# sourceMappingURL=IntlayerClientProvider.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/client/IntlayerClientProvider.tsx"],"sourcesContent":["'use client';\n\nimport type { FC } from 'react';\nimport { IntlayerProvider, type IntlayerProviderProps } from 'react-intlayer';\nimport { useLocale } from './useLocale';\n\nexport type IntlayerClientProviderProps = IntlayerProviderProps;\n\nexport const IntlayerClientProvider: FC<IntlayerProviderProps> = (props) => {\n const { setLocale } = useLocale();\n return <IntlayerProvider setLocale={setLocale} {...props} />;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUS;AAPT,4BAA6D;AAC7D,uBAA0B;AAInB,MAAM,yBAAoD,CAAC,UAAU;AAC1E,QAAM,EAAE,UAAU,QAAI,4BAAU;AAChC,SAAO,4CAAC,0CAAiB,WAAuB,GAAG,OAAO;AAC5D;","names":[]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var client_exports = {};
|
|
20
|
+
__export(client_exports, {
|
|
21
|
+
IntlayerClientContext: () => import_react_intlayer.IntlayerClientContext,
|
|
22
|
+
IntlayerClientProvider: () => import_IntlayerClientProvider.IntlayerClientProvider,
|
|
23
|
+
getTranslation: () => import_react_intlayer.getTranslation,
|
|
24
|
+
useIntlayer: () => import_react_intlayer.useIntlayer,
|
|
25
|
+
useLocale: () => import_useLocale.useLocale,
|
|
26
|
+
useLocaleCookie: () => import_react_intlayer.useLocaleCookie,
|
|
27
|
+
useLocalePageRouter: () => import_useLocalePageRouter.useLocalePageRouter,
|
|
28
|
+
useTraduction: () => import_react_intlayer.useTraduction
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(client_exports);
|
|
31
|
+
var import_react_intlayer = require("react-intlayer");
|
|
32
|
+
var import_useLocale = require('./useLocale.cjs');
|
|
33
|
+
var import_useLocalePageRouter = require('./useLocalePageRouter.cjs');
|
|
34
|
+
var import_IntlayerClientProvider = require('./IntlayerClientProvider.cjs');
|
|
35
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
36
|
+
0 && (module.exports = {
|
|
37
|
+
IntlayerClientContext,
|
|
38
|
+
IntlayerClientProvider,
|
|
39
|
+
getTranslation,
|
|
40
|
+
useIntlayer,
|
|
41
|
+
useLocale,
|
|
42
|
+
useLocaleCookie,
|
|
43
|
+
useLocalePageRouter,
|
|
44
|
+
useTraduction
|
|
45
|
+
});
|
|
46
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/client/index.ts"],"sourcesContent":["export {\n getTranslation,\n IntlayerClientContext,\n useIntlayer,\n useTraduction,\n useLocaleCookie,\n} from 'react-intlayer';\nexport { useLocale } from './useLocale';\nexport { useLocalePageRouter } from './useLocalePageRouter';\nexport {\n IntlayerClientProvider,\n type IntlayerClientProviderProps,\n} from './IntlayerClientProvider';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAMO;AACP,uBAA0B;AAC1B,iCAAoC;AACpC,oCAGO;","names":[]}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var useLocale_exports = {};
|
|
20
|
+
__export(useLocale_exports, {
|
|
21
|
+
useLocale: () => useLocale
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(useLocale_exports);
|
|
24
|
+
var import_client = require("@intlayer/config/client");
|
|
25
|
+
var import_navigation = require("next/navigation.js");
|
|
26
|
+
var import_react = require("react");
|
|
27
|
+
var import_react_intlayer = require("react-intlayer");
|
|
28
|
+
const useLocale = () => {
|
|
29
|
+
const {
|
|
30
|
+
/**
|
|
31
|
+
* Prefix default prefix the default locale to the path as other locales.
|
|
32
|
+
*
|
|
33
|
+
* Example with prefixDefault = true and defaultLocale = 'en':
|
|
34
|
+
* path = /en/dashboard or /fr/dashboard
|
|
35
|
+
*
|
|
36
|
+
* Example with prefixDefault = false and defaultLocale = 'en':
|
|
37
|
+
* path = /dashboard or /fr/dashboard
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
prefixDefault
|
|
41
|
+
} = (0, import_client.getConfiguration)().middleware;
|
|
42
|
+
const { setLocaleCookie } = (0, import_react_intlayer.useLocaleCookie)();
|
|
43
|
+
const reactLocaleHook = (0, import_react_intlayer.useLocaleBase)();
|
|
44
|
+
const { push, refresh } = (0, import_navigation.useRouter)();
|
|
45
|
+
const pathname = (0, import_navigation.usePathname)();
|
|
46
|
+
const {
|
|
47
|
+
defaultLocale,
|
|
48
|
+
availableLocales,
|
|
49
|
+
locale: currentLocale,
|
|
50
|
+
setLocale: setLocaleState
|
|
51
|
+
} = reactLocaleHook;
|
|
52
|
+
const getPathWithoutLocale = () => {
|
|
53
|
+
if (
|
|
54
|
+
// If the locale is the default one and the prefixDefault is false, we don't need to add the locale to the path
|
|
55
|
+
!prefixDefault && currentLocale.toString() === defaultLocale.toString()
|
|
56
|
+
) {
|
|
57
|
+
return pathname;
|
|
58
|
+
}
|
|
59
|
+
const slicedPath = pathname.slice(`/${currentLocale}`.length);
|
|
60
|
+
if (slicedPath) {
|
|
61
|
+
return slicedPath;
|
|
62
|
+
}
|
|
63
|
+
return "/";
|
|
64
|
+
};
|
|
65
|
+
const pathWithoutLocale = getPathWithoutLocale();
|
|
66
|
+
const setLocale = (0, import_react.useCallback)(
|
|
67
|
+
(locale) => {
|
|
68
|
+
if (!availableLocales.includes(locale)) {
|
|
69
|
+
console.error(`Locale ${locale} is not available`);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
setLocaleCookie(locale);
|
|
73
|
+
setLocaleState(locale);
|
|
74
|
+
if (!prefixDefault && locale.toString() === defaultLocale.toString()) {
|
|
75
|
+
push(pathWithoutLocale);
|
|
76
|
+
} else {
|
|
77
|
+
push(`/${locale}${pathWithoutLocale}`);
|
|
78
|
+
}
|
|
79
|
+
return refresh();
|
|
80
|
+
},
|
|
81
|
+
[
|
|
82
|
+
currentLocale,
|
|
83
|
+
availableLocales,
|
|
84
|
+
setLocaleCookie,
|
|
85
|
+
setLocaleState,
|
|
86
|
+
prefixDefault,
|
|
87
|
+
defaultLocale,
|
|
88
|
+
pathWithoutLocale
|
|
89
|
+
]
|
|
90
|
+
);
|
|
91
|
+
return {
|
|
92
|
+
...reactLocaleHook,
|
|
93
|
+
setLocale,
|
|
94
|
+
pathWithoutLocale
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
98
|
+
0 && (module.exports = {
|
|
99
|
+
useLocale
|
|
100
|
+
});
|
|
101
|
+
//# sourceMappingURL=useLocale.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/client/useLocale.ts"],"sourcesContent":["import { type Locales, getConfiguration } from '@intlayer/config/client';\nimport { usePathname, useRouter } from 'next/navigation.js';\nimport { useCallback } from 'react';\nimport { useLocaleCookie, useLocaleBase } from 'react-intlayer';\n\nexport const useLocale = () => {\n const {\n /**\n * Prefix default prefix the default locale to the path as other locales.\n *\n * Example with prefixDefault = true and defaultLocale = 'en':\n * path = /en/dashboard or /fr/dashboard\n *\n * Example with prefixDefault = false and defaultLocale = 'en':\n * path = /dashboard or /fr/dashboard\n *\n */\n prefixDefault,\n } = getConfiguration().middleware;\n const { setLocaleCookie } = useLocaleCookie();\n const reactLocaleHook = useLocaleBase();\n const { push, refresh } = useRouter();\n const pathname = usePathname();\n\n const {\n defaultLocale,\n availableLocales,\n locale: currentLocale,\n setLocale: setLocaleState,\n } = reactLocaleHook;\n\n const getPathWithoutLocale = () => {\n if (\n // If the locale is the default one and the prefixDefault is false, we don't need to add the locale to the path\n !prefixDefault &&\n currentLocale.toString() === defaultLocale.toString()\n ) {\n return pathname;\n }\n\n const slicedPath = pathname.slice(`/${currentLocale}`.length);\n\n // If the path without locale is not empty, we return it\n if (slicedPath) {\n return slicedPath;\n }\n\n // If the path without locale is empty, we return the root path\n return '/';\n };\n\n const pathWithoutLocale = getPathWithoutLocale();\n\n const setLocale = useCallback(\n (locale: Locales) => {\n if (!availableLocales.includes(locale)) {\n console.error(`Locale ${locale} is not available`);\n return;\n }\n\n setLocaleCookie(locale);\n setLocaleState(locale);\n\n if (!prefixDefault && locale.toString() === defaultLocale.toString()) {\n push(pathWithoutLocale);\n } else {\n push(`/${locale}${pathWithoutLocale}`);\n }\n\n return refresh();\n },\n [\n currentLocale,\n availableLocales,\n setLocaleCookie,\n setLocaleState,\n prefixDefault,\n defaultLocale,\n pathWithoutLocale,\n ]\n );\n\n return {\n ...reactLocaleHook,\n setLocale,\n pathWithoutLocale,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAC/C,wBAAuC;AACvC,mBAA4B;AAC5B,4BAA+C;AAExC,MAAM,YAAY,MAAM;AAC7B,QAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAWJ;AAAA,EACF,QAAI,gCAAiB,EAAE;AACvB,QAAM,EAAE,gBAAgB,QAAI,uCAAgB;AAC5C,QAAM,sBAAkB,qCAAc;AACtC,QAAM,EAAE,MAAM,QAAQ,QAAI,6BAAU;AACpC,QAAM,eAAW,+BAAY;AAE7B,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,IACR,WAAW;AAAA,EACb,IAAI;AAEJ,QAAM,uBAAuB,MAAM;AACjC;AAAA;AAAA,MAEE,CAAC,iBACD,cAAc,SAAS,MAAM,cAAc,SAAS;AAAA,MACpD;AACA,aAAO;AAAA,IACT;AAEA,UAAM,aAAa,SAAS,MAAM,IAAI,aAAa,GAAG,MAAM;AAG5D,QAAI,YAAY;AACd,aAAO;AAAA,IACT;AAGA,WAAO;AAAA,EACT;AAEA,QAAM,oBAAoB,qBAAqB;AAE/C,QAAM,gBAAY;AAAA,IAChB,CAAC,WAAoB;AACnB,UAAI,CAAC,iBAAiB,SAAS,MAAM,GAAG;AACtC,gBAAQ,MAAM,UAAU,MAAM,mBAAmB;AACjD;AAAA,MACF;AAEA,sBAAgB,MAAM;AACtB,qBAAe,MAAM;AAErB,UAAI,CAAC,iBAAiB,OAAO,SAAS,MAAM,cAAc,SAAS,GAAG;AACpE,aAAK,iBAAiB;AAAA,MACxB,OAAO;AACL,aAAK,IAAI,MAAM,GAAG,iBAAiB,EAAE;AAAA,MACvC;AAEA,aAAO,QAAQ;AAAA,IACjB;AAAA,IACA;AAAA,MACE;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var useLocalePageRouter_exports = {};
|
|
20
|
+
__export(useLocalePageRouter_exports, {
|
|
21
|
+
useLocalePageRouter: () => useLocalePageRouter
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(useLocalePageRouter_exports);
|
|
24
|
+
var import_client = require("@intlayer/config/client");
|
|
25
|
+
var import_router = require("next/router.js");
|
|
26
|
+
var import_react = require("react");
|
|
27
|
+
var import_react_intlayer = require("react-intlayer");
|
|
28
|
+
const useLocalePageRouter = () => {
|
|
29
|
+
const {
|
|
30
|
+
/**
|
|
31
|
+
* Prefix default prefix the default locale to the path as other locales.
|
|
32
|
+
*
|
|
33
|
+
* Example with prefixDefault = true and defaultLocale = 'en':
|
|
34
|
+
* path = /en/dashboard or /fr/dashboard
|
|
35
|
+
*
|
|
36
|
+
* Example with prefixDefault = false and defaultLocale = 'en':
|
|
37
|
+
* path = /dashboard or /fr/dashboard
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
prefixDefault
|
|
41
|
+
} = (0, import_client.getConfiguration)().middleware;
|
|
42
|
+
const { setLocaleCookie } = (0, import_react_intlayer.useLocaleCookie)();
|
|
43
|
+
const reactLocaleHook = (0, import_react_intlayer.useLocaleBase)();
|
|
44
|
+
const { push, pathname, reload } = (0, import_router.useRouter)();
|
|
45
|
+
const {
|
|
46
|
+
defaultLocale,
|
|
47
|
+
availableLocales,
|
|
48
|
+
locale: currentLocale,
|
|
49
|
+
setLocale: setLocaleState
|
|
50
|
+
} = reactLocaleHook;
|
|
51
|
+
const getPathWithoutLocale = () => {
|
|
52
|
+
if (
|
|
53
|
+
// If the locale is the default one and the prefixDefault is false, we don't need to add the locale to the path
|
|
54
|
+
!prefixDefault && currentLocale.toString() === defaultLocale.toString()
|
|
55
|
+
) {
|
|
56
|
+
return pathname;
|
|
57
|
+
}
|
|
58
|
+
const slicedPath = pathname.slice(`/${currentLocale}`.length);
|
|
59
|
+
if (slicedPath) {
|
|
60
|
+
return slicedPath;
|
|
61
|
+
}
|
|
62
|
+
return "/";
|
|
63
|
+
};
|
|
64
|
+
const pathWithoutLocale = getPathWithoutLocale();
|
|
65
|
+
const setLocale = (0, import_react.useCallback)(
|
|
66
|
+
(locale) => {
|
|
67
|
+
if (!availableLocales.includes(locale)) {
|
|
68
|
+
console.error(`Locale ${locale} is not available`);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
setLocaleCookie(locale);
|
|
72
|
+
setLocaleState(locale);
|
|
73
|
+
if (!prefixDefault && locale.toString() === defaultLocale.toString()) {
|
|
74
|
+
push(pathWithoutLocale);
|
|
75
|
+
} else {
|
|
76
|
+
push(`/${locale}${pathWithoutLocale}`);
|
|
77
|
+
}
|
|
78
|
+
return reload();
|
|
79
|
+
},
|
|
80
|
+
[
|
|
81
|
+
currentLocale,
|
|
82
|
+
availableLocales,
|
|
83
|
+
setLocaleCookie,
|
|
84
|
+
setLocaleState,
|
|
85
|
+
prefixDefault,
|
|
86
|
+
defaultLocale,
|
|
87
|
+
pathWithoutLocale
|
|
88
|
+
]
|
|
89
|
+
);
|
|
90
|
+
return {
|
|
91
|
+
...reactLocaleHook,
|
|
92
|
+
setLocale,
|
|
93
|
+
pathWithoutLocale
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
97
|
+
0 && (module.exports = {
|
|
98
|
+
useLocalePageRouter
|
|
99
|
+
});
|
|
100
|
+
//# sourceMappingURL=useLocalePageRouter.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/client/useLocalePageRouter.ts"],"sourcesContent":["import { type Locales, getConfiguration } from '@intlayer/config/client';\nimport { useRouter } from 'next/router.js';\nimport { useCallback } from 'react';\nimport { useLocaleCookie, useLocaleBase } from 'react-intlayer';\n\nexport const useLocalePageRouter = () => {\n const {\n /**\n * Prefix default prefix the default locale to the path as other locales.\n *\n * Example with prefixDefault = true and defaultLocale = 'en':\n * path = /en/dashboard or /fr/dashboard\n *\n * Example with prefixDefault = false and defaultLocale = 'en':\n * path = /dashboard or /fr/dashboard\n *\n */\n prefixDefault,\n } = getConfiguration().middleware;\n const { setLocaleCookie } = useLocaleCookie();\n const reactLocaleHook = useLocaleBase();\n const { push, pathname, reload } = useRouter();\n\n const {\n defaultLocale,\n availableLocales,\n locale: currentLocale,\n setLocale: setLocaleState,\n } = reactLocaleHook;\n\n const getPathWithoutLocale = () => {\n if (\n // If the locale is the default one and the prefixDefault is false, we don't need to add the locale to the path\n !prefixDefault &&\n currentLocale.toString() === defaultLocale.toString()\n ) {\n return pathname;\n }\n\n const slicedPath = pathname.slice(`/${currentLocale}`.length);\n\n // If the path without locale is not empty, we return it\n if (slicedPath) {\n return slicedPath;\n }\n\n // If the path without locale is empty, we return the root path\n return '/';\n };\n\n const pathWithoutLocale = getPathWithoutLocale();\n\n const setLocale = useCallback(\n (locale: Locales) => {\n if (!availableLocales.includes(locale)) {\n console.error(`Locale ${locale} is not available`);\n return;\n }\n\n setLocaleCookie(locale);\n setLocaleState(locale);\n\n if (!prefixDefault && locale.toString() === defaultLocale.toString()) {\n push(pathWithoutLocale);\n } else {\n push(`/${locale}${pathWithoutLocale}`);\n }\n\n return reload();\n },\n [\n currentLocale,\n availableLocales,\n setLocaleCookie,\n setLocaleState,\n prefixDefault,\n defaultLocale,\n pathWithoutLocale,\n ]\n );\n\n return {\n ...reactLocaleHook,\n setLocale,\n pathWithoutLocale,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAC/C,oBAA0B;AAC1B,mBAA4B;AAC5B,4BAA+C;AAExC,MAAM,sBAAsB,MAAM;AACvC,QAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAWJ;AAAA,EACF,QAAI,gCAAiB,EAAE;AACvB,QAAM,EAAE,gBAAgB,QAAI,uCAAgB;AAC5C,QAAM,sBAAkB,qCAAc;AACtC,QAAM,EAAE,MAAM,UAAU,OAAO,QAAI,yBAAU;AAE7C,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,IACR,WAAW;AAAA,EACb,IAAI;AAEJ,QAAM,uBAAuB,MAAM;AACjC;AAAA;AAAA,MAEE,CAAC,iBACD,cAAc,SAAS,MAAM,cAAc,SAAS;AAAA,MACpD;AACA,aAAO;AAAA,IACT;AAEA,UAAM,aAAa,SAAS,MAAM,IAAI,aAAa,GAAG,MAAM;AAG5D,QAAI,YAAY;AACd,aAAO;AAAA,IACT;AAGA,WAAO;AAAA,EACT;AAEA,QAAM,oBAAoB,qBAAqB;AAE/C,QAAM,gBAAY;AAAA,IAChB,CAAC,WAAoB;AACnB,UAAI,CAAC,iBAAiB,SAAS,MAAM,GAAG;AACtC,gBAAQ,MAAM,UAAU,MAAM,mBAAmB;AACjD;AAAA,MACF;AAEA,sBAAgB,MAAM;AACtB,qBAAe,MAAM;AAErB,UAAI,CAAC,iBAAiB,OAAO,SAAS,MAAM,cAAc,SAAS,GAAG;AACpE,aAAK,iBAAiB;AAAA,MACxB,OAAO;AACL,aAAK,IAAI,MAAM,GAAG,iBAAiB,EAAE;AAAA,MACvC;AAEA,aAAO,OAAO;AAAA,IAChB;AAAA,IACA;AAAA,MACE;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var generateStaticParams_exports = {};
|
|
20
|
+
__export(generateStaticParams_exports, {
|
|
21
|
+
generateStaticParams: () => generateStaticParams
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(generateStaticParams_exports);
|
|
24
|
+
var import_client = require("@intlayer/config/client");
|
|
25
|
+
const { locales } = (0, import_client.getConfiguration)().internationalization;
|
|
26
|
+
const generateStaticParams = () => locales.map((locale) => ({ locale }));
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
generateStaticParams
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=generateStaticParams.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/generateStaticParams.ts"],"sourcesContent":["import { getConfiguration } from '@intlayer/config/client';\n\nconst { locales } = getConfiguration().internationalization;\n\nexport const generateStaticParams = () => locales.map((locale) => ({ locale }));\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAiC;AAEjC,MAAM,EAAE,QAAQ,QAAI,gCAAiB,EAAE;AAEhC,MAAM,uBAAuB,MAAM,QAAQ,IAAI,CAAC,YAAY,EAAE,OAAO,EAAE;","names":[]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var src_exports = {};
|
|
20
|
+
__export(src_exports, {
|
|
21
|
+
IntlayerClientContext: () => import_react_intlayer.IntlayerClientContext,
|
|
22
|
+
IntlayerClientProvider: () => import_client.IntlayerClientProvider,
|
|
23
|
+
generateStaticParams: () => import_generateStaticParams.generateStaticParams,
|
|
24
|
+
getTranslation: () => import_react_intlayer.getTranslation,
|
|
25
|
+
t: () => import_react_intlayer.t,
|
|
26
|
+
useDictionary: () => import_react_intlayer.useDictionary,
|
|
27
|
+
useIntlayer: () => import_react_intlayer.useIntlayer,
|
|
28
|
+
useLocale: () => import_client.useLocale,
|
|
29
|
+
useLocaleCookie: () => import_react_intlayer.useLocaleCookie,
|
|
30
|
+
useLocalePageRouter: () => import_client.useLocalePageRouter,
|
|
31
|
+
useTraduction: () => import_react_intlayer.useTraduction
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(src_exports);
|
|
34
|
+
var import_react_intlayer = require("react-intlayer");
|
|
35
|
+
var import_generateStaticParams = require('./generateStaticParams.cjs');
|
|
36
|
+
var import_client = require('./client/index.cjs');
|
|
37
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
38
|
+
0 && (module.exports = {
|
|
39
|
+
IntlayerClientContext,
|
|
40
|
+
IntlayerClientProvider,
|
|
41
|
+
generateStaticParams,
|
|
42
|
+
getTranslation,
|
|
43
|
+
t,
|
|
44
|
+
useDictionary,
|
|
45
|
+
useIntlayer,
|
|
46
|
+
useLocale,
|
|
47
|
+
useLocaleCookie,
|
|
48
|
+
useLocalePageRouter,
|
|
49
|
+
useTraduction
|
|
50
|
+
});
|
|
51
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export {\n getTranslation,\n IntlayerClientContext,\n useIntlayer,\n useTraduction,\n useDictionary,\n useLocaleCookie,\n type IntlayerNode,\n t,\n} from 'react-intlayer';\nexport { generateStaticParams } from './generateStaticParams';\nexport type {\n LocalParams,\n LocalPromiseParams,\n Next14PageIntlayer,\n Next15PageIntlayer,\n NextPageIntlayer,\n Next14LayoutIntlayer,\n Next15LayoutIntlayer,\n NextLayoutIntlayer,\n} from './types/index';\nexport {\n useLocale,\n useLocalePageRouter,\n IntlayerClientProvider,\n type IntlayerClientProviderProps,\n} from './client/index';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BASO;AACP,kCAAqC;AAWrC,oBAKO;","names":[]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var middleware_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(middleware_exports);
|
|
18
|
+
__reExport(middleware_exports, require('./intlayerMiddleware.cjs'), module.exports);
|
|
19
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
20
|
+
0 && (module.exports = {
|
|
21
|
+
...require('./intlayerMiddleware.cjs')
|
|
22
|
+
});
|
|
23
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/middleware/index.ts"],"sourcesContent":["export * from './intlayerMiddleware';\n"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,+BAAc,iCAAd;","names":[]}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var intlayerMiddleware_exports = {};
|
|
20
|
+
__export(intlayerMiddleware_exports, {
|
|
21
|
+
intlayerMiddleware: () => intlayerMiddleware
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(intlayerMiddleware_exports);
|
|
24
|
+
var import_client = require("@intlayer/config/client");
|
|
25
|
+
var import_server = require("next/server");
|
|
26
|
+
var import_localeDetector = require('./localeDetector.cjs');
|
|
27
|
+
const { internationalization, middleware } = (0, import_client.getConfiguration)();
|
|
28
|
+
const { locales, defaultLocale } = internationalization;
|
|
29
|
+
const {
|
|
30
|
+
headerName,
|
|
31
|
+
cookieName,
|
|
32
|
+
prefixDefault,
|
|
33
|
+
basePath,
|
|
34
|
+
serverSetCookie,
|
|
35
|
+
noPrefix
|
|
36
|
+
} = middleware;
|
|
37
|
+
const intlayerMiddleware = (request, _event, _response) => {
|
|
38
|
+
const pathname = request.nextUrl.pathname;
|
|
39
|
+
const cookieLocale = getCookieLocale(request);
|
|
40
|
+
const basePathTrailingSlash = basePath.endsWith("/");
|
|
41
|
+
if (noPrefix) {
|
|
42
|
+
return handleNoPrefix(
|
|
43
|
+
request,
|
|
44
|
+
cookieLocale,
|
|
45
|
+
pathname,
|
|
46
|
+
basePathTrailingSlash
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
const pathLocale = getPathLocale(pathname);
|
|
50
|
+
return handlePrefix(
|
|
51
|
+
request,
|
|
52
|
+
cookieLocale,
|
|
53
|
+
pathLocale,
|
|
54
|
+
pathname,
|
|
55
|
+
basePathTrailingSlash
|
|
56
|
+
);
|
|
57
|
+
};
|
|
58
|
+
const getCookieLocale = (request) => {
|
|
59
|
+
if (!cookieName) return void 0;
|
|
60
|
+
const cookieValue = request.cookies.get(cookieName)?.value;
|
|
61
|
+
if (cookieValue && locales.includes(cookieValue)) {
|
|
62
|
+
return cookieValue;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
const handleNoPrefix = (request, cookieLocale, pathname, basePathTrailingSlash) => {
|
|
66
|
+
const locale = cookieLocale ?? defaultLocale;
|
|
67
|
+
const newPath = constructPath(
|
|
68
|
+
locale,
|
|
69
|
+
pathname,
|
|
70
|
+
basePath,
|
|
71
|
+
basePathTrailingSlash
|
|
72
|
+
);
|
|
73
|
+
return rewriteUrl(request, newPath, locale);
|
|
74
|
+
};
|
|
75
|
+
const getPathLocale = (pathname) => locales.find(
|
|
76
|
+
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
|
|
77
|
+
);
|
|
78
|
+
const handlePrefix = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => {
|
|
79
|
+
if (!pathLocale) {
|
|
80
|
+
return handleMissingPathLocale(
|
|
81
|
+
request,
|
|
82
|
+
cookieLocale,
|
|
83
|
+
pathname,
|
|
84
|
+
basePathTrailingSlash
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return handleExistingPathLocale(
|
|
88
|
+
request,
|
|
89
|
+
cookieLocale,
|
|
90
|
+
pathLocale,
|
|
91
|
+
pathname,
|
|
92
|
+
basePathTrailingSlash
|
|
93
|
+
);
|
|
94
|
+
};
|
|
95
|
+
const handleMissingPathLocale = (request, cookieLocale, pathname, basePathTrailingSlash) => {
|
|
96
|
+
let locale = cookieLocale ?? (0, import_localeDetector.localeDetector)?.(request) ?? defaultLocale;
|
|
97
|
+
if (!locales.includes(locale)) {
|
|
98
|
+
console.warn(
|
|
99
|
+
"The localeDetector callback must return a locale included in your locales array. Reverting to using defaultLocale."
|
|
100
|
+
);
|
|
101
|
+
locale = defaultLocale;
|
|
102
|
+
}
|
|
103
|
+
const newPath = constructPath(
|
|
104
|
+
locale,
|
|
105
|
+
pathname,
|
|
106
|
+
basePath,
|
|
107
|
+
basePathTrailingSlash
|
|
108
|
+
);
|
|
109
|
+
return prefixDefault || locale !== defaultLocale ? redirectUrl(request, newPath) : rewriteUrl(request, newPath, locale);
|
|
110
|
+
};
|
|
111
|
+
const handleExistingPathLocale = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => {
|
|
112
|
+
if (
|
|
113
|
+
// If the cookie locale is set and differs from the locale in the URL, and server should not always set cookie
|
|
114
|
+
cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always"
|
|
115
|
+
) {
|
|
116
|
+
const newPath = handleCookieLocaleMismatch(
|
|
117
|
+
request,
|
|
118
|
+
pathname,
|
|
119
|
+
pathLocale,
|
|
120
|
+
cookieLocale,
|
|
121
|
+
basePath,
|
|
122
|
+
basePathTrailingSlash
|
|
123
|
+
);
|
|
124
|
+
return redirectUrl(request, newPath);
|
|
125
|
+
}
|
|
126
|
+
return handleDefaultLocaleRedirect(
|
|
127
|
+
request,
|
|
128
|
+
pathLocale,
|
|
129
|
+
pathname,
|
|
130
|
+
basePathTrailingSlash
|
|
131
|
+
);
|
|
132
|
+
};
|
|
133
|
+
const handleCookieLocaleMismatch = (request, pathname, pathLocale, cookieLocale, basePath2, basePathTrailingSlash) => {
|
|
134
|
+
const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);
|
|
135
|
+
return constructPath(
|
|
136
|
+
cookieLocale,
|
|
137
|
+
newPath,
|
|
138
|
+
basePath2,
|
|
139
|
+
basePathTrailingSlash,
|
|
140
|
+
request.nextUrl.search
|
|
141
|
+
);
|
|
142
|
+
};
|
|
143
|
+
const handleDefaultLocaleRedirect = (request, pathLocale, pathname, basePathTrailingSlash) => {
|
|
144
|
+
if (
|
|
145
|
+
// If default locale should not be prefixed and the pathLocale is the defaultLocale
|
|
146
|
+
!prefixDefault && pathLocale === defaultLocale
|
|
147
|
+
) {
|
|
148
|
+
let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) ?? "/";
|
|
149
|
+
if (basePathTrailingSlash) {
|
|
150
|
+
pathWithoutLocale = pathWithoutLocale.slice(1);
|
|
151
|
+
}
|
|
152
|
+
if (request.nextUrl.search) {
|
|
153
|
+
pathWithoutLocale += request.nextUrl.search;
|
|
154
|
+
}
|
|
155
|
+
return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);
|
|
156
|
+
}
|
|
157
|
+
return rewriteUrl(request, pathname, pathLocale);
|
|
158
|
+
};
|
|
159
|
+
const constructPath = (locale, path, basePath2, basePathTrailingSlash, search) => {
|
|
160
|
+
let newPath = `${locale}${path}`;
|
|
161
|
+
newPath = `${basePath2}${basePathTrailingSlash ? "" : "/"}${newPath}`;
|
|
162
|
+
if (search) {
|
|
163
|
+
newPath += search;
|
|
164
|
+
}
|
|
165
|
+
return newPath;
|
|
166
|
+
};
|
|
167
|
+
const rewriteUrl = (request, newPath, locale) => {
|
|
168
|
+
const response = import_server.NextResponse.rewrite(new URL(newPath, request.url));
|
|
169
|
+
response.headers.set(headerName, locale);
|
|
170
|
+
return response;
|
|
171
|
+
};
|
|
172
|
+
const redirectUrl = (request, newPath) => import_server.NextResponse.redirect(new URL(newPath, request.url));
|
|
173
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
174
|
+
0 && (module.exports = {
|
|
175
|
+
intlayerMiddleware
|
|
176
|
+
});
|
|
177
|
+
//# sourceMappingURL=intlayerMiddleware.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/middleware/intlayerMiddleware.ts"],"sourcesContent":["import { type Locales, getConfiguration } from '@intlayer/config/client';\nimport {\n type NextFetchEvent,\n type NextRequest,\n NextResponse,\n} from 'next/server';\nimport { localeDetector } from './localeDetector';\n\nconst { internationalization, middleware } = getConfiguration();\nconst { locales, defaultLocale } = internationalization;\nconst {\n headerName,\n cookieName,\n prefixDefault,\n basePath,\n serverSetCookie,\n noPrefix,\n} = middleware;\n\n/**\n * Middleware that handles the internationalization layer\n *\n * Usage:\n *\n * ```ts\n * // ./src/middleware.ts\n *\n * export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';\n *\n * // applies this middleware only to files in the app directory\n * export const config = {\n * matcher: '/((?!api|static|.*\\\\..*|_next).*)',\n * };\n * ```\n *\n * Main middleware function for handling internationalization.\n *\n * @param request - The incoming Next.js request object.\n * @param event - The Next.js fetch event (optional).\n * @param response - The Next.js response object (optional).\n * @returns - The response to be returned to the client.\n */\nexport const intlayerMiddleware = (\n request: NextRequest,\n _event?: NextFetchEvent,\n _response?: NextResponse\n): NextResponse => {\n const pathname = request.nextUrl.pathname;\n const cookieLocale = getCookieLocale(request);\n const basePathTrailingSlash = basePath.endsWith('/');\n\n if (\n noPrefix // If the application is configured not to use locale prefixes in URLs\n ) {\n return handleNoPrefix(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n const pathLocale = getPathLocale(pathname);\n\n return handlePrefix(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\n};\n\n/**\n * Retrieves the locale from the request cookies if available and valid.\n *\n * @param request - The incoming Next.js request object.\n * @returns - The locale found in the cookies, or undefined if not found or invalid.\n */\nconst getCookieLocale = (request: NextRequest): Locales | undefined => {\n if (!cookieName) return undefined;\n const cookieValue = request.cookies.get(cookieName)?.value as Locales;\n if (cookieValue && locales.includes(cookieValue)) {\n return cookieValue;\n }\n};\n\n/**\n * Handles the case where URLs do not have locale prefixes.\n *\n * @param request - The incoming Next.js request object.\n * @param cookieLocale - The locale from the cookie.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The rewritten response with the locale applied.\n */\nconst handleNoPrefix = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n const locale = cookieLocale ?? defaultLocale;\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash\n );\n return rewriteUrl(request, newPath, locale);\n};\n\n/**\n * Extracts the locale from the URL pathname if present.\n *\n * @param pathname - The pathname from the request URL.\n * @returns - The locale found in the pathname, or undefined if not found.\n */\nconst getPathLocale = (pathname: string): Locales | undefined =>\n locales.find(\n (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`\n );\n\n/**\n * Handles the case where URLs have locale prefixes.\n *\n * @param request - The incoming Next.js request object.\n * @param cookieLocale - The locale from the cookie.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The response to be returned to the client.\n */\nconst handlePrefix = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n !pathLocale // If the URL does not contain a locale prefix\n ) {\n return handleMissingPathLocale(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n // If the URL contains a locale prefix\n return handleExistingPathLocale(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\n};\n\n/**\n * Handles requests where the locale is missing from the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param cookieLocale - The locale from the cookie.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The response to be returned to the client.\n */\nconst handleMissingPathLocale = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n let locale = cookieLocale ?? localeDetector?.(request) ?? defaultLocale;\n if (!locales.includes(locale)) {\n console.warn(\n 'The localeDetector callback must return a locale included in your locales array. Reverting to using defaultLocale.'\n );\n locale = defaultLocale;\n }\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash\n );\n\n return prefixDefault || locale !== defaultLocale\n ? redirectUrl(request, newPath)\n : rewriteUrl(request, newPath, locale);\n};\n\n/**\n * Handles requests where the locale exists in the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param cookieLocale - The locale from the cookie.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The response to be returned to the client.\n */\nconst handleExistingPathLocale = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n // If the cookie locale is set and differs from the locale in the URL, and server should not always set cookie\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n const newPath = handleCookieLocaleMismatch(\n request,\n pathname,\n pathLocale,\n cookieLocale,\n basePath,\n basePathTrailingSlash\n );\n return redirectUrl(request, newPath);\n }\n\n // If the cookie locale matches the path locale, or cookie locale is not set, or serverSetCookie is 'always'\n return handleDefaultLocaleRedirect(\n request,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\n};\n\n/**\n * Handles the scenario where the locale in the cookie does not match the locale in the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param pathname - The pathname from the request URL.\n * @param pathLocale - The locale extracted from the pathname.\n * @param cookieLocale - The locale from the cookie.\n * @param basePath - The base path of the application.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The new URL path with the correct locale.\n */\nconst handleCookieLocaleMismatch = (\n request: NextRequest,\n pathname: string,\n pathLocale: Locales,\n cookieLocale: Locales,\n basePath: string,\n basePathTrailingSlash: boolean\n): string => {\n // Replace the pathLocale in the pathname with the cookieLocale\n const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);\n\n return constructPath(\n cookieLocale,\n newPath,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\n );\n};\n\n/**\n * Handles redirection when the default locale is used and prefixing is not required.\n *\n * @param request - The incoming Next.js request object.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The rewritten response without the locale prefix.\n */\nconst handleDefaultLocaleRedirect = (\n request: NextRequest,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n // If default locale should not be prefixed and the pathLocale is the defaultLocale\n !prefixDefault &&\n pathLocale === defaultLocale\n ) {\n let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) ?? '/';\n\n if (basePathTrailingSlash) {\n pathWithoutLocale = pathWithoutLocale.slice(1);\n }\n\n if (request.nextUrl.search) {\n pathWithoutLocale += request.nextUrl.search;\n }\n\n return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);\n }\n\n // If prefixing default locale is required or pathLocale is not the defaultLocale\n\n return rewriteUrl(request, pathname, pathLocale);\n};\n\n/**\n * Constructs a new path by combining the locale, path, basePath, and search parameters.\n *\n * @param locale - The locale to include in the path.\n * @param path - The original path from the request.\n * @param basePath - The base path of the application.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @param [search] - The query string from the request URL (optional).\n * @returns - The constructed new path.\n */\nconst constructPath = (\n locale: Locales,\n path: string,\n basePath: string,\n basePathTrailingSlash: boolean,\n search?: string\n): string => {\n let newPath = `${locale}${path}`;\n\n newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${newPath}`;\n if (search) {\n newPath += search;\n }\n return newPath;\n};\n\n/**\n * Rewrites the URL to the new path and sets the locale header.\n *\n * @param request - The incoming Next.js request object.\n * @param newPath - The new path to rewrite to.\n * @param locale - The locale to set in the response header.\n * @returns - The rewritten response.\n */\nconst rewriteUrl = (\n request: NextRequest,\n newPath: string,\n locale: Locales\n): NextResponse => {\n const response = NextResponse.rewrite(new URL(newPath, request.url));\n response.headers.set(headerName, locale);\n return response;\n};\n\n/**\n * Redirects the request to the new path.\n *\n * @param request - The incoming Next.js request object.\n * @param newPath - The new path to redirect to.\n * @returns - The redirect response.\n */\nconst redirectUrl = (request: NextRequest, newPath: string): NextResponse =>\n NextResponse.redirect(new URL(newPath, request.url));\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAC/C,oBAIO;AACP,4BAA+B;AAE/B,MAAM,EAAE,sBAAsB,WAAW,QAAI,gCAAiB;AAC9D,MAAM,EAAE,SAAS,cAAc,IAAI;AACnC,MAAM;AAAA,EACJ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,IAAI;AAyBG,MAAM,qBAAqB,CAChC,SACA,QACA,cACiB;AACjB,QAAM,WAAW,QAAQ,QAAQ;AACjC,QAAM,eAAe,gBAAgB,OAAO;AAC5C,QAAM,wBAAwB,SAAS,SAAS,GAAG;AAEnD,MACE,UACA;AACA,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,QAAM,aAAa,cAAc,QAAQ;AAEzC,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAQA,MAAM,kBAAkB,CAAC,YAA8C;AACrE,MAAI,CAAC,WAAY,QAAO;AACxB,QAAM,cAAc,QAAQ,QAAQ,IAAI,UAAU,GAAG;AACrD,MAAI,eAAe,QAAQ,SAAS,WAAW,GAAG;AAChD,WAAO;AAAA,EACT;AACF;AAWA,MAAM,iBAAiB,CACrB,SACA,cACA,UACA,0BACiB;AACjB,QAAM,SAAS,gBAAgB;AAC/B,QAAM,UAAU;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACA,SAAO,WAAW,SAAS,SAAS,MAAM;AAC5C;AAQA,MAAM,gBAAgB,CAAC,aACrB,QAAQ;AAAA,EACN,CAAC,WAAW,SAAS,WAAW,IAAI,MAAM,GAAG,KAAK,aAAa,IAAI,MAAM;AAC3E;AAYF,MAAM,eAAe,CACnB,SACA,cACA,YACA,UACA,0BACiB;AACjB,MACE,CAAC,YACD;AACA,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAGA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAWA,MAAM,0BAA0B,CAC9B,SACA,cACA,UACA,0BACiB;AACjB,MAAI,SAAS,oBAAgB,wCAAiB,OAAO,KAAK;AAC1D,MAAI,CAAC,QAAQ,SAAS,MAAM,GAAG;AAC7B,YAAQ;AAAA,MACN;AAAA,IACF;AACA,aAAS;AAAA,EACX;AACA,QAAM,UAAU;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAEA,SAAO,iBAAiB,WAAW,gBAC/B,YAAY,SAAS,OAAO,IAC5B,WAAW,SAAS,SAAS,MAAM;AACzC;AAYA,MAAM,2BAA2B,CAC/B,SACA,cACA,YACA,UACA,0BACiB;AACjB;AAAA;AAAA,IAEE,gBACA,iBAAiB,cACjB,oBAAoB;AAAA,IACpB;AACA,UAAM,UAAU;AAAA,MACd;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AACA,WAAO,YAAY,SAAS,OAAO;AAAA,EACrC;AAGA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAaA,MAAM,6BAA6B,CACjC,SACA,UACA,YACA,cACAA,WACA,0BACW;AAEX,QAAM,UAAU,SAAS,QAAQ,IAAI,UAAU,IAAI,IAAI,YAAY,EAAE;AAErE,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACAA;AAAA,IACA;AAAA,IACA,QAAQ,QAAQ;AAAA,EAClB;AACF;AAWA,MAAM,8BAA8B,CAClC,SACA,YACA,UACA,0BACiB;AACjB;AAAA;AAAA,IAEE,CAAC,iBACD,eAAe;AAAA,IACf;AACA,QAAI,oBAAoB,SAAS,MAAM,IAAI,UAAU,GAAG,MAAM,KAAK;AAEnE,QAAI,uBAAuB;AACzB,0BAAoB,kBAAkB,MAAM,CAAC;AAAA,IAC/C;AAEA,QAAI,QAAQ,QAAQ,QAAQ;AAC1B,2BAAqB,QAAQ,QAAQ;AAAA,IACvC;AAEA,WAAO,WAAW,SAAS,GAAG,QAAQ,GAAG,iBAAiB,IAAI,UAAU;AAAA,EAC1E;AAIA,SAAO,WAAW,SAAS,UAAU,UAAU;AACjD;AAYA,MAAM,gBAAgB,CACpB,QACA,MACAA,WACA,uBACA,WACW;AACX,MAAI,UAAU,GAAG,MAAM,GAAG,IAAI;AAE9B,YAAU,GAAGA,SAAQ,GAAG,wBAAwB,KAAK,GAAG,GAAG,OAAO;AAClE,MAAI,QAAQ;AACV,eAAW;AAAA,EACb;AACA,SAAO;AACT;AAUA,MAAM,aAAa,CACjB,SACA,SACA,WACiB;AACjB,QAAM,WAAW,2BAAa,QAAQ,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;AACnE,WAAS,QAAQ,IAAI,YAAY,MAAM;AACvC,SAAO;AACT;AASA,MAAM,cAAc,CAAC,SAAsB,YACzC,2BAAa,SAAS,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;","names":["basePath"]}
|