next-intlayer 6.1.6 → 7.0.0-canary.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/cjs/client/IntlayerClientProvider.cjs +19 -33
- package/dist/cjs/client/IntlayerClientProvider.cjs.map +1 -1
- package/dist/cjs/client/format/index.cjs +50 -41
- package/dist/cjs/client/index.cjs +7 -35
- package/dist/cjs/client/useLocale.cjs +39 -54
- package/dist/cjs/client/useLocale.cjs.map +1 -1
- package/dist/cjs/client/useLocalePageRouter.cjs +26 -52
- package/dist/cjs/client/useLocalePageRouter.cjs.map +1 -1
- package/dist/cjs/generateStaticParams.cjs +9 -39
- package/dist/cjs/generateStaticParams.cjs.map +1 -1
- package/dist/cjs/index.cjs +102 -57
- package/dist/cjs/middleware/index.cjs +7 -27
- package/dist/cjs/middleware/intlayerMiddleware.cjs +206 -188
- package/dist/cjs/middleware/intlayerMiddleware.cjs.map +1 -1
- package/dist/cjs/middleware/localeDetector.cjs +18 -32
- package/dist/cjs/middleware/localeDetector.cjs.map +1 -1
- package/dist/cjs/middleware/multipleMiddlewares.cjs +55 -67
- package/dist/cjs/middleware/multipleMiddlewares.cjs.map +1 -1
- package/dist/cjs/proxy/index.cjs +9 -0
- package/dist/cjs/proxy/intlayerProxy.cjs +252 -0
- package/dist/cjs/proxy/intlayerProxy.cjs.map +1 -0
- package/dist/cjs/proxy/localeDetector.cjs +21 -0
- package/dist/cjs/proxy/localeDetector.cjs.map +1 -0
- package/dist/cjs/proxy/multipleProxies.cjs +60 -0
- package/dist/cjs/proxy/multipleProxies.cjs.map +1 -0
- package/dist/cjs/proxy/multipleProxy.cjs +60 -0
- package/dist/cjs/proxy/multipleProxy.cjs.map +1 -0
- package/dist/cjs/server/compareVersion.cjs +35 -54
- package/dist/cjs/server/compareVersion.cjs.map +1 -1
- package/dist/cjs/server/format/index.cjs +50 -41
- package/dist/cjs/server/getLocale.cjs +31 -53
- package/dist/cjs/server/getLocale.cjs.map +1 -1
- package/dist/cjs/server/index.cjs +69 -53
- package/dist/cjs/server/withIntlayer.cjs +181 -247
- package/dist/cjs/server/withIntlayer.cjs.map +1 -1
- package/dist/cjs/types/NextPage.cjs +0 -17
- package/dist/cjs/types/index.cjs +0 -17
- package/dist/esm/client/IntlayerClientProvider.mjs +15 -8
- package/dist/esm/client/IntlayerClientProvider.mjs.map +1 -1
- package/dist/esm/client/format/index.mjs +3 -21
- package/dist/esm/client/index.mjs +3 -9
- package/dist/esm/client/useLocale.mjs +31 -27
- package/dist/esm/client/useLocale.mjs.map +1 -1
- package/dist/esm/client/useLocalePageRouter.mjs +19 -26
- package/dist/esm/client/useLocalePageRouter.mjs.map +1 -1
- package/dist/esm/generateStaticParams.mjs +5 -3
- package/dist/esm/generateStaticParams.mjs.map +1 -1
- package/dist/esm/index.mjs +6 -34
- package/dist/esm/middleware/index.mjs +5 -4
- package/dist/esm/middleware/intlayerMiddleware.mjs +201 -155
- package/dist/esm/middleware/intlayerMiddleware.mjs.map +1 -1
- package/dist/esm/middleware/localeDetector.mjs +16 -8
- package/dist/esm/middleware/localeDetector.mjs.map +1 -1
- package/dist/esm/middleware/multipleMiddlewares.mjs +53 -45
- package/dist/esm/middleware/multipleMiddlewares.mjs.map +1 -1
- package/dist/esm/proxy/index.mjs +5 -0
- package/dist/esm/proxy/intlayerProxy.mjs +247 -0
- package/dist/esm/proxy/intlayerProxy.mjs.map +1 -0
- package/dist/esm/proxy/localeDetector.mjs +19 -0
- package/dist/esm/proxy/localeDetector.mjs.map +1 -0
- package/dist/esm/proxy/multipleProxies.mjs +57 -0
- package/dist/esm/proxy/multipleProxies.mjs.map +1 -0
- package/dist/esm/proxy/multipleProxy.mjs +57 -0
- package/dist/esm/proxy/multipleProxy.mjs.map +1 -0
- package/dist/esm/server/compareVersion.mjs +34 -30
- package/dist/esm/server/compareVersion.mjs.map +1 -1
- package/dist/esm/server/format/index.mjs +3 -21
- package/dist/esm/server/getLocale.mjs +23 -17
- package/dist/esm/server/getLocale.mjs.map +1 -1
- package/dist/esm/server/index.mjs +4 -28
- package/dist/esm/server/withIntlayer.mjs +166 -214
- package/dist/esm/server/withIntlayer.mjs.map +1 -1
- package/dist/esm/types/NextPage.mjs +0 -1
- package/dist/esm/types/index.mjs +0 -1
- package/dist/types/client/IntlayerClientProvider.d.ts +8 -4
- package/dist/types/client/IntlayerClientProvider.d.ts.map +1 -1
- package/dist/types/client/format/index.d.ts +2 -2
- package/dist/types/client/index.d.ts +4 -4
- package/dist/types/client/useLocale.d.ts +17 -7
- package/dist/types/client/useLocale.d.ts.map +1 -1
- package/dist/types/client/useLocalePageRouter.d.ts +12 -7
- package/dist/types/client/useLocalePageRouter.d.ts.map +1 -1
- package/dist/types/generateStaticParams.d.ts +7 -2
- package/dist/types/generateStaticParams.d.ts.map +1 -1
- package/dist/types/index.d.ts +7 -5
- package/dist/types/middleware/index.d.ts +4 -4
- package/dist/types/middleware/intlayerMiddleware.d.ts +7 -2
- package/dist/types/middleware/intlayerMiddleware.d.ts.map +1 -1
- package/dist/types/middleware/localeDetector.d.ts +8 -3
- package/dist/types/middleware/localeDetector.d.ts.map +1 -1
- package/dist/types/middleware/multipleMiddlewares.d.ts +7 -2
- package/dist/types/middleware/multipleMiddlewares.d.ts.map +1 -1
- package/dist/types/proxy/index.d.ts +4 -0
- package/dist/types/proxy/intlayerProxy.d.ts +55 -0
- package/dist/types/proxy/intlayerProxy.d.ts.map +1 -0
- package/dist/types/proxy/localeDetector.d.ts +14 -0
- package/dist/types/proxy/localeDetector.d.ts.map +1 -0
- package/dist/types/proxy/multipleProxies.d.ts +34 -0
- package/dist/types/proxy/multipleProxies.d.ts.map +1 -0
- package/dist/types/proxy/multipleProxy.d.ts +34 -0
- package/dist/types/proxy/multipleProxy.d.ts.map +1 -0
- package/dist/types/server/compareVersion.d.ts +4 -1
- package/dist/types/server/compareVersion.d.ts.map +1 -1
- package/dist/types/server/format/index.d.ts +2 -2
- package/dist/types/server/getLocale.d.ts +6 -2
- package/dist/types/server/getLocale.d.ts.map +1 -1
- package/dist/types/server/index.d.ts +4 -4
- package/dist/types/server/withIntlayer.d.ts +25 -3
- package/dist/types/server/withIntlayer.d.ts.map +1 -1
- package/dist/types/types/NextPage.d.ts +21 -17
- package/dist/types/types/NextPage.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +2 -2
- package/package.json +48 -50
- package/LICENSE +0 -202
- package/dist/cjs/client/format/index.cjs.map +0 -1
- package/dist/cjs/client/index.cjs.map +0 -1
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/middleware/index.cjs.map +0 -1
- package/dist/cjs/server/format/index.cjs.map +0 -1
- package/dist/cjs/server/getNextVertion.cjs +0 -39
- package/dist/cjs/server/getNextVertion.cjs.map +0 -1
- package/dist/cjs/server/index.cjs.map +0 -1
- package/dist/cjs/types/NextPage.cjs.map +0 -1
- package/dist/cjs/types/index.cjs.map +0 -1
- package/dist/esm/client/format/index.mjs.map +0 -1
- package/dist/esm/client/index.mjs.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm/middleware/index.mjs.map +0 -1
- package/dist/esm/server/format/index.mjs.map +0 -1
- package/dist/esm/server/getNextVertion.mjs +0 -15
- package/dist/esm/server/getNextVertion.mjs.map +0 -1
- package/dist/esm/server/index.mjs.map +0 -1
- package/dist/esm/types/NextPage.mjs.map +0 -1
- package/dist/esm/types/index.mjs.map +0 -1
- package/dist/types/client/format/index.d.ts.map +0 -1
- package/dist/types/client/index.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/middleware/index.d.ts.map +0 -1
- package/dist/types/server/format/index.d.ts.map +0 -1
- package/dist/types/server/getNextVertion.d.ts +0 -2
- package/dist/types/server/getNextVertion.d.ts.map +0 -1
- package/dist/types/server/index.d.ts.map +0 -1
- package/dist/types/types/index.d.ts.map +0 -1
|
@@ -1,32 +1,36 @@
|
|
|
1
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import { useLocale as useLocale$1 } from "react-intlayer";
|
|
2
5
|
import { getLocalizedUrl, getPathWithoutLocale } from "@intlayer/core";
|
|
3
6
|
import { usePathname, useRouter } from "next/navigation.js";
|
|
4
7
|
import { useCallback, useMemo } from "react";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
};
|
|
29
|
-
export {
|
|
30
|
-
useLocale
|
|
8
|
+
|
|
9
|
+
//#region src/client/useLocale.ts
|
|
10
|
+
const useLocale = ({ onChange } = {}) => {
|
|
11
|
+
const { replace, push } = useRouter();
|
|
12
|
+
const pathname = usePathname();
|
|
13
|
+
const pathWithoutLocale = useMemo(() => getPathWithoutLocale(pathname), [pathname]);
|
|
14
|
+
return {
|
|
15
|
+
...useLocale$1({ onLocaleChange: useCallback((locale) => {
|
|
16
|
+
if (!onChange) return;
|
|
17
|
+
if (typeof onChange === "function") {
|
|
18
|
+
onChange(locale);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const pathWithLocale = getLocalizedUrl(pathWithoutLocale, locale);
|
|
22
|
+
if (onChange === "replace") replace(pathWithLocale);
|
|
23
|
+
if (onChange === "push") push(pathWithLocale);
|
|
24
|
+
}, [
|
|
25
|
+
replace,
|
|
26
|
+
push,
|
|
27
|
+
pathWithoutLocale,
|
|
28
|
+
onChange
|
|
29
|
+
]) }),
|
|
30
|
+
pathWithoutLocale
|
|
31
|
+
};
|
|
31
32
|
};
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { useLocale };
|
|
32
36
|
//# sourceMappingURL=useLocale.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/client/useLocale.ts"],"sourcesContent":["'use client';\n\nimport {
|
|
1
|
+
{"version":3,"file":"useLocale.mjs","names":["useLocaleReact"],"sources":["../../../src/client/useLocale.ts"],"sourcesContent":["'use client';\n\nimport { getLocalizedUrl, getPathWithoutLocale } from '@intlayer/core';\nimport type { LocalesValues } from '@intlayer/types';\nimport { usePathname, useRouter } from 'next/navigation.js';\nimport { useCallback, useMemo } from 'react';\nimport { useLocale as useLocaleReact } from 'react-intlayer';\n\ntype UseLocaleProps = {\n onChange?: 'replace' | 'push' | ((locale: LocalesValues) => void);\n};\n\nexport const useLocale = ({ onChange }: UseLocaleProps = {}) => {\n const { replace, push } = useRouter();\n const pathname = usePathname();\n const pathWithoutLocale = useMemo(\n () => getPathWithoutLocale(pathname),\n [pathname]\n );\n\n const redirectionFunction = useCallback(\n (locale: LocalesValues) => {\n if (!onChange) return;\n\n if (typeof onChange === 'function') {\n onChange(locale);\n return;\n }\n\n const pathWithLocale = getLocalizedUrl(pathWithoutLocale, locale);\n\n if (onChange === 'replace') {\n replace(pathWithLocale);\n }\n if (onChange === 'push') {\n push(pathWithLocale);\n }\n },\n [replace, push, pathWithoutLocale, onChange]\n );\n\n const reactLocaleHook = useLocaleReact({\n onLocaleChange: redirectionFunction,\n });\n\n return {\n ...reactLocaleHook,\n pathWithoutLocale,\n };\n};\n"],"mappings":";;;;;;;;;AAYA,MAAa,aAAa,EAAE,aAA6B,EAAE,KAAK;CAC9D,MAAM,EAAE,SAAS,SAAS,WAAW;CACrC,MAAM,WAAW,aAAa;CAC9B,MAAM,oBAAoB,cAClB,qBAAqB,SAAS,EACpC,CAAC,SAAS,CACX;AA2BD,QAAO;EACL,GALsBA,YAAe,EACrC,gBAtB0B,aACzB,WAA0B;AACzB,OAAI,CAAC,SAAU;AAEf,OAAI,OAAO,aAAa,YAAY;AAClC,aAAS,OAAO;AAChB;;GAGF,MAAM,iBAAiB,gBAAgB,mBAAmB,OAAO;AAEjE,OAAI,aAAa,UACf,SAAQ,eAAe;AAEzB,OAAI,aAAa,OACf,MAAK,eAAe;KAGxB;GAAC;GAAS;GAAM;GAAmB;GAAS,CAC7C,EAIA,CAAC;EAIA;EACD"}
|
|
@@ -1,31 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import { useLocale } from "react-intlayer";
|
|
2
5
|
import { getLocalizedUrl, getPathWithoutLocale } from "@intlayer/core";
|
|
3
|
-
import { useRouter } from "next/router.js";
|
|
4
6
|
import { useCallback, useMemo } from "react";
|
|
5
|
-
import {
|
|
7
|
+
import { useRouter } from "next/router.js";
|
|
8
|
+
|
|
9
|
+
//#region src/client/useLocalePageRouter.ts
|
|
6
10
|
const useLocalePageRouter = () => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return reload();
|
|
17
|
-
},
|
|
18
|
-
[pathWithoutLocale]
|
|
19
|
-
);
|
|
20
|
-
const reactLocaleHook = useLocaleReact({
|
|
21
|
-
onLocaleChange: redirectionFunction
|
|
22
|
-
});
|
|
23
|
-
return {
|
|
24
|
-
...reactLocaleHook,
|
|
25
|
-
pathWithoutLocale
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
export {
|
|
29
|
-
useLocalePageRouter
|
|
11
|
+
const { push, pathname, reload } = useRouter();
|
|
12
|
+
const pathWithoutLocale = useMemo(() => getPathWithoutLocale(pathname), [pathname]);
|
|
13
|
+
return {
|
|
14
|
+
...useLocale({ onLocaleChange: useCallback((locale) => {
|
|
15
|
+
push(getLocalizedUrl(pathWithoutLocale, locale));
|
|
16
|
+
return reload();
|
|
17
|
+
}, [pathWithoutLocale]) }),
|
|
18
|
+
pathWithoutLocale
|
|
19
|
+
};
|
|
30
20
|
};
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { useLocalePageRouter };
|
|
31
24
|
//# sourceMappingURL=useLocalePageRouter.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/client/useLocalePageRouter.ts"],"sourcesContent":["'use client';\n\nimport {
|
|
1
|
+
{"version":3,"file":"useLocalePageRouter.mjs","names":["useLocaleReact"],"sources":["../../../src/client/useLocalePageRouter.ts"],"sourcesContent":["'use client';\n\nimport { getLocalizedUrl, getPathWithoutLocale } from '@intlayer/core';\nimport type { LocalesValues } from '@intlayer/types';\nimport { useRouter } from 'next/router.js';\nimport { useCallback, useMemo } from 'react';\nimport { useLocale as useLocaleReact } from 'react-intlayer';\n\nexport const useLocalePageRouter = () => {\n const { push, pathname, reload } = useRouter();\n const pathWithoutLocale = useMemo(\n () => getPathWithoutLocale(pathname),\n [pathname]\n );\n\n const redirectionFunction = useCallback(\n (locale: LocalesValues) => {\n const pathWithLocale = getLocalizedUrl(pathWithoutLocale, locale);\n\n push(pathWithLocale);\n\n return reload();\n },\n [pathWithoutLocale]\n );\n\n const reactLocaleHook = useLocaleReact({\n onLocaleChange: redirectionFunction,\n });\n\n return {\n ...reactLocaleHook,\n pathWithoutLocale,\n };\n};\n"],"mappings":";;;;;;;;;AAQA,MAAa,4BAA4B;CACvC,MAAM,EAAE,MAAM,UAAU,WAAW,WAAW;CAC9C,MAAM,oBAAoB,cAClB,qBAAqB,SAAS,EACpC,CAAC,SAAS,CACX;AAiBD,QAAO;EACL,GALsBA,UAAe,EACrC,gBAZ0B,aACzB,WAA0B;AAGzB,QAFuB,gBAAgB,mBAAmB,OAAO,CAE7C;AAEpB,UAAO,QAAQ;KAEjB,CAAC,kBAAkB,CACpB,EAIA,CAAC;EAIA;EACD"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import configuration from "@intlayer/config/built";
|
|
2
|
+
|
|
3
|
+
//#region src/generateStaticParams.ts
|
|
2
4
|
const { locales } = configuration.internationalization;
|
|
3
5
|
const generateStaticParams = () => locales.map((locale) => ({ locale }));
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
6
|
+
|
|
7
|
+
//#endregion
|
|
8
|
+
export { generateStaticParams };
|
|
7
9
|
//# sourceMappingURL=generateStaticParams.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/generateStaticParams.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\n\nconst { locales } = configuration.internationalization;\n\nexport const generateStaticParams = () => locales.map((locale) => ({ locale }));\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"generateStaticParams.mjs","names":[],"sources":["../../src/generateStaticParams.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\n\nconst { locales } = configuration.internationalization;\n\nexport const generateStaticParams = () => locales.map((locale) => ({ locale }));\n"],"mappings":";;;AAEA,MAAM,EAAE,YAAY,cAAc;AAElC,MAAa,6BAA6B,QAAQ,KAAK,YAAY,EAAE,QAAQ,EAAE"}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,35 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
IntlayerClientContext,
|
|
3
|
-
MarkdownProvider,
|
|
4
|
-
t,
|
|
5
|
-
useDictionary,
|
|
6
|
-
useDictionaryAsync,
|
|
7
|
-
useDictionaryDynamic,
|
|
8
|
-
useI18n,
|
|
9
|
-
useIntlayer,
|
|
10
|
-
useLoadDynamic,
|
|
11
|
-
useLocaleCookie
|
|
12
|
-
} from "react-intlayer";
|
|
13
|
-
import {
|
|
14
|
-
IntlayerClientProvider,
|
|
15
|
-
useLocale,
|
|
16
|
-
useLocalePageRouter
|
|
17
|
-
} from "./client/index.mjs";
|
|
18
1
|
import { generateStaticParams } from "./generateStaticParams.mjs";
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
useDictionary,
|
|
26
|
-
useDictionaryAsync,
|
|
27
|
-
useDictionaryDynamic,
|
|
28
|
-
useI18n,
|
|
29
|
-
useIntlayer,
|
|
30
|
-
useLoadDynamic,
|
|
31
|
-
useLocale,
|
|
32
|
-
useLocaleCookie,
|
|
33
|
-
useLocalePageRouter
|
|
34
|
-
};
|
|
35
|
-
//# sourceMappingURL=index.mjs.map
|
|
2
|
+
import { useLocale } from "./client/useLocale.mjs";
|
|
3
|
+
import { IntlayerClientProvider } from "./client/IntlayerClientProvider.mjs";
|
|
4
|
+
import { useLocalePageRouter } from "./client/useLocalePageRouter.mjs";
|
|
5
|
+
import { IntlayerClientContext, MarkdownProvider, localeCookie, localeInStorage, setLocaleCookie, setLocaleInStorage, t, useDictionary, useDictionaryAsync, useDictionaryDynamic, useI18n, useIntlayer, useLoadDynamic, useLocaleCookie, useLocaleStorage } from "react-intlayer";
|
|
6
|
+
|
|
7
|
+
export { IntlayerClientContext, IntlayerClientProvider, MarkdownProvider, generateStaticParams, localeCookie, localeInStorage, setLocaleCookie, setLocaleInStorage, t, useDictionary, useDictionaryAsync, useDictionaryDynamic, useI18n, useIntlayer, useLoadDynamic, useLocale, useLocaleCookie, useLocalePageRouter, useLocaleStorage };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { localeDetector } from "./localeDetector.mjs";
|
|
2
|
+
import { intlayerMiddleware } from "./intlayerMiddleware.mjs";
|
|
3
|
+
import { multipleMiddlewares } from "./multipleMiddlewares.mjs";
|
|
4
|
+
|
|
5
|
+
export { intlayerMiddleware, localeDetector, multipleMiddlewares };
|
|
@@ -1,177 +1,223 @@
|
|
|
1
|
+
import { localeDetector as localeDetector$1 } from "./localeDetector.mjs";
|
|
1
2
|
import configuration from "@intlayer/config/built";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const { internationalization,
|
|
7
|
-
const { locales, defaultLocale } = internationalization;
|
|
8
|
-
const {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
3
|
+
import { getLocaleFromStorage } from "@intlayer/core";
|
|
4
|
+
import { NextResponse } from "next/server";
|
|
5
|
+
|
|
6
|
+
//#region src/middleware/intlayerMiddleware.ts
|
|
7
|
+
const { internationalization, routing } = configuration ?? {};
|
|
8
|
+
const { locales, defaultLocale } = internationalization ?? {};
|
|
9
|
+
const { headerName, basePath, detectLocaleOnPrefetchNoPrefix, mode } = routing ?? {};
|
|
10
|
+
const noPrefix = mode === "no-prefix" || mode === "search-params";
|
|
11
|
+
const prefixDefault = mode === "prefix-all";
|
|
12
|
+
/**
|
|
13
|
+
* Detects if the request is a prefetch request from Next.js.
|
|
14
|
+
*
|
|
15
|
+
* Next.js prefetch requests can be identified by several headers:
|
|
16
|
+
* - purpose: 'prefetch' (standard prefetch header)
|
|
17
|
+
* - next-router-prefetch: '1' (Next.js router prefetch)
|
|
18
|
+
* - next-url: present (Next.js internal navigation)
|
|
19
|
+
*
|
|
20
|
+
* During prefetch, we should ignore cookie-based locale detection
|
|
21
|
+
* to prevent unwanted redirects when users are switching locales.
|
|
22
|
+
*
|
|
23
|
+
* @param request - The incoming Next.js request object.
|
|
24
|
+
* @returns - True if the request is a prefetch request, false otherwise.
|
|
25
|
+
*/
|
|
17
26
|
const isPrefetchRequest = (request) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
27
|
+
const purpose = request.headers.get("purpose");
|
|
28
|
+
const nextRouterPrefetch = request.headers.get("next-router-prefetch");
|
|
29
|
+
const nextUrl = request.headers.get("next-url");
|
|
30
|
+
const xNextjsData = request.headers.get("x-nextjs-data");
|
|
31
|
+
return purpose === "prefetch" || nextRouterPrefetch === "1" || !!nextUrl || !!xNextjsData;
|
|
23
32
|
};
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return handleNoPrefix(
|
|
30
|
-
request,
|
|
31
|
-
cookieLocale,
|
|
32
|
-
pathname,
|
|
33
|
-
basePathTrailingSlash
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
const pathLocale = getPathLocale(pathname);
|
|
37
|
-
return handlePrefix(
|
|
38
|
-
request,
|
|
39
|
-
cookieLocale,
|
|
40
|
-
pathLocale,
|
|
41
|
-
pathname,
|
|
42
|
-
basePathTrailingSlash
|
|
43
|
-
);
|
|
33
|
+
const appendLocaleSearchIfNeeded = (search, locale) => {
|
|
34
|
+
if (mode !== "search-params") return search;
|
|
35
|
+
const params = new URLSearchParams(search ?? "");
|
|
36
|
+
params.set("locale", locale);
|
|
37
|
+
return `?${params.toString()}`;
|
|
44
38
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Middleware that handles the internationalization layer
|
|
41
|
+
*
|
|
42
|
+
* Usage:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* // ./src/middleware.ts
|
|
46
|
+
*
|
|
47
|
+
* export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';
|
|
48
|
+
*
|
|
49
|
+
* // applies this middleware only to files in the app directory
|
|
50
|
+
* export const config = {
|
|
51
|
+
* matcher: '/((?!api|static|.*\\..*|_next).*)',
|
|
52
|
+
* };
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* Main middleware function for handling internationalization.
|
|
56
|
+
*
|
|
57
|
+
* @param request - The incoming Next.js request object.
|
|
58
|
+
* @param event - The Next.js fetch event (optional).
|
|
59
|
+
* @param response - The Next.js response object (optional).
|
|
60
|
+
* @returns - The response to be returned to the client.
|
|
61
|
+
*/
|
|
62
|
+
const intlayerMiddleware = (request, _event, _response) => {
|
|
63
|
+
const pathname = request.nextUrl.pathname;
|
|
64
|
+
const cookieLocale = getCookieLocale(request);
|
|
65
|
+
const basePathTrailingSlash = basePath.endsWith("/");
|
|
66
|
+
if (noPrefix) return handleNoPrefix(request, cookieLocale, pathname, basePathTrailingSlash);
|
|
67
|
+
return handlePrefix(request, cookieLocale, getPathLocale(pathname), pathname, basePathTrailingSlash);
|
|
51
68
|
};
|
|
69
|
+
/**
|
|
70
|
+
* Retrieves the locale from the request cookies if available and valid.
|
|
71
|
+
*
|
|
72
|
+
* @param request - The incoming Next.js request object.
|
|
73
|
+
* @returns - The locale found in the cookies, or undefined if not found or invalid.
|
|
74
|
+
*/
|
|
75
|
+
const getCookieLocale = (request) => getLocaleFromStorage({ getCookie: (name) => request.cookies.get(name)?.value ?? null });
|
|
76
|
+
/**
|
|
77
|
+
* Handles the case where URLs do not have locale prefixes.
|
|
78
|
+
*
|
|
79
|
+
* @param request - The incoming Next.js request object.
|
|
80
|
+
* @param cookieLocale - The locale from the cookie.
|
|
81
|
+
* @param pathname - The pathname from the request URL.
|
|
82
|
+
* @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
|
|
83
|
+
* @returns - The rewritten response with the locale applied.
|
|
84
|
+
*/
|
|
52
85
|
const handleNoPrefix = (request, cookieLocale, pathname, basePathTrailingSlash) => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
locale,
|
|
56
|
-
pathname,
|
|
57
|
-
basePath,
|
|
58
|
-
basePathTrailingSlash,
|
|
59
|
-
request.nextUrl.search
|
|
60
|
-
);
|
|
61
|
-
return rewriteUrl(request, newPath, locale);
|
|
86
|
+
const locale = cookieLocale ?? defaultLocale;
|
|
87
|
+
return rewriteUrl(request, constructPath(locale, pathname, basePath, basePathTrailingSlash, appendLocaleSearchIfNeeded(request.nextUrl.search, locale)), locale);
|
|
62
88
|
};
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Extracts the locale from the URL pathname if present.
|
|
91
|
+
*
|
|
92
|
+
* @param pathname - The pathname from the request URL.
|
|
93
|
+
* @returns - The locale found in the pathname, or undefined if not found.
|
|
94
|
+
*/
|
|
95
|
+
const getPathLocale = (pathname) => locales.find((locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`);
|
|
96
|
+
/**
|
|
97
|
+
* Handles the case where URLs have locale prefixes.
|
|
98
|
+
*
|
|
99
|
+
* @param request - The incoming Next.js request object.
|
|
100
|
+
* @param cookieLocale - The locale from the cookie.
|
|
101
|
+
* @param pathLocale - The locale extracted from the pathname.
|
|
102
|
+
* @param pathname - The pathname from the request URL.
|
|
103
|
+
* @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
|
|
104
|
+
* @returns - The response to be returned to the client.
|
|
105
|
+
*/
|
|
66
106
|
const handlePrefix = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
defaultLocale,
|
|
73
|
-
pathname,
|
|
74
|
-
basePathTrailingSlash
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
return handleMissingPathLocale(
|
|
78
|
-
request,
|
|
79
|
-
cookieLocale,
|
|
80
|
-
pathname,
|
|
81
|
-
basePathTrailingSlash
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
return handleExistingPathLocale(
|
|
85
|
-
request,
|
|
86
|
-
cookieLocale,
|
|
87
|
-
pathLocale,
|
|
88
|
-
pathname,
|
|
89
|
-
basePathTrailingSlash
|
|
90
|
-
);
|
|
107
|
+
if (!pathLocale) {
|
|
108
|
+
if (isPrefetchRequest(request) && !detectLocaleOnPrefetchNoPrefix) return handleMissingPathLocale(request, defaultLocale, pathname, basePathTrailingSlash);
|
|
109
|
+
return handleMissingPathLocale(request, cookieLocale, pathname, basePathTrailingSlash);
|
|
110
|
+
}
|
|
111
|
+
return handleExistingPathLocale(request, cookieLocale, pathLocale, pathname, basePathTrailingSlash);
|
|
91
112
|
};
|
|
113
|
+
/**
|
|
114
|
+
* Handles requests where the locale is missing from the URL pathname.
|
|
115
|
+
*
|
|
116
|
+
* @param request - The incoming Next.js request object.
|
|
117
|
+
* @param cookieLocale - The locale from the cookie.
|
|
118
|
+
* @param pathname - The pathname from the request URL.
|
|
119
|
+
* @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
|
|
120
|
+
* @returns - The response to be returned to the client.
|
|
121
|
+
*/
|
|
92
122
|
const handleMissingPathLocale = (request, cookieLocale, pathname, basePathTrailingSlash) => {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const newPath = constructPath(
|
|
98
|
-
locale,
|
|
99
|
-
pathname,
|
|
100
|
-
basePath,
|
|
101
|
-
basePathTrailingSlash,
|
|
102
|
-
request.nextUrl.search
|
|
103
|
-
);
|
|
104
|
-
return prefixDefault || locale !== defaultLocale ? redirectUrl(request, newPath) : rewriteUrl(request, newPath, locale);
|
|
123
|
+
let locale = cookieLocale ?? localeDetector$1?.(request) ?? defaultLocale;
|
|
124
|
+
if (!locales.includes(locale)) locale = defaultLocale;
|
|
125
|
+
const newPath = constructPath(locale, pathname, basePath, basePathTrailingSlash, appendLocaleSearchIfNeeded(request.nextUrl.search, locale));
|
|
126
|
+
return prefixDefault || locale !== defaultLocale ? redirectUrl(request, newPath) : rewriteUrl(request, newPath, locale);
|
|
105
127
|
};
|
|
128
|
+
/**
|
|
129
|
+
* Handles requests where the locale exists in the URL pathname.
|
|
130
|
+
*
|
|
131
|
+
* @param request - The incoming Next.js request object.
|
|
132
|
+
* @param cookieLocale - The locale from the cookie.
|
|
133
|
+
* @param pathLocale - The locale extracted from the pathname.
|
|
134
|
+
* @param pathname - The pathname from the request URL.
|
|
135
|
+
* @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
|
|
136
|
+
* @returns - The response to be returned to the client.
|
|
137
|
+
*/
|
|
106
138
|
const handleExistingPathLocale = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always"
|
|
110
|
-
) {
|
|
111
|
-
const newPath = handleCookieLocaleMismatch(
|
|
112
|
-
request,
|
|
113
|
-
pathname,
|
|
114
|
-
pathLocale,
|
|
115
|
-
cookieLocale,
|
|
116
|
-
basePath,
|
|
117
|
-
basePathTrailingSlash
|
|
118
|
-
);
|
|
119
|
-
return redirectUrl(request, newPath);
|
|
120
|
-
}
|
|
121
|
-
return handleDefaultLocaleRedirect(
|
|
122
|
-
request,
|
|
123
|
-
pathLocale,
|
|
124
|
-
pathname,
|
|
125
|
-
basePathTrailingSlash
|
|
126
|
-
);
|
|
139
|
+
if (cookieLocale && cookieLocale !== pathLocale) return redirectUrl(request, handleCookieLocaleMismatch(request, pathname, pathLocale, cookieLocale, basePath, basePathTrailingSlash));
|
|
140
|
+
return handleDefaultLocaleRedirect(request, pathLocale, pathname, basePathTrailingSlash);
|
|
127
141
|
};
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Handles the scenario where the locale in the cookie does not match the locale in the URL pathname.
|
|
144
|
+
*
|
|
145
|
+
* @param request - The incoming Next.js request object.
|
|
146
|
+
* @param pathname - The pathname from the request URL.
|
|
147
|
+
* @param pathLocale - The locale extracted from the pathname.
|
|
148
|
+
* @param cookieLocale - The locale from the cookie.
|
|
149
|
+
* @param basePath - The base path of the application.
|
|
150
|
+
* @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
|
|
151
|
+
* @returns - The new URL path with the correct locale.
|
|
152
|
+
*/
|
|
153
|
+
const handleCookieLocaleMismatch = (request, pathname, pathLocale, cookieLocale, basePath$1, basePathTrailingSlash) => {
|
|
154
|
+
return constructPath(cookieLocale, pathname.replace(`/${pathLocale}`, `/${cookieLocale}`), basePath$1, basePathTrailingSlash, appendLocaleSearchIfNeeded(request.nextUrl.search, cookieLocale));
|
|
137
155
|
};
|
|
156
|
+
/**
|
|
157
|
+
* Handles redirection when the default locale is used and prefixing is not required.
|
|
158
|
+
*
|
|
159
|
+
* @param request - The incoming Next.js request object.
|
|
160
|
+
* @param pathLocale - The locale extracted from the pathname.
|
|
161
|
+
* @param pathname - The pathname from the request URL.
|
|
162
|
+
* @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
|
|
163
|
+
* @returns - The rewritten response without the locale prefix.
|
|
164
|
+
*/
|
|
138
165
|
const handleDefaultLocaleRedirect = (request, pathLocale, pathname, basePathTrailingSlash) => {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);
|
|
151
|
-
}
|
|
152
|
-
return rewriteUrl(request, pathname, pathLocale);
|
|
166
|
+
if (!prefixDefault && pathLocale === defaultLocale) {
|
|
167
|
+
let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) ?? "/";
|
|
168
|
+
if (basePathTrailingSlash) pathWithoutLocale = pathWithoutLocale.slice(1);
|
|
169
|
+
const searchWithLocale$1 = appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale);
|
|
170
|
+
if (searchWithLocale$1) pathWithoutLocale += searchWithLocale$1;
|
|
171
|
+
else if (request.nextUrl.search) pathWithoutLocale += request.nextUrl.search;
|
|
172
|
+
return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);
|
|
173
|
+
}
|
|
174
|
+
const searchWithLocale = appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale);
|
|
175
|
+
return rewriteUrl(request, searchWithLocale ? `${pathname}${searchWithLocale}` : pathname, pathLocale);
|
|
153
176
|
};
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
177
|
+
/**
|
|
178
|
+
* Constructs a new path by combining the locale, path, basePath, and search parameters.
|
|
179
|
+
*
|
|
180
|
+
* @param locale - The locale to include in the path.
|
|
181
|
+
* @param path - The original path from the request.
|
|
182
|
+
* @param basePath - The base path of the application.
|
|
183
|
+
* @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
|
|
184
|
+
* @param [search] - The query string from the request URL (optional).
|
|
185
|
+
* @returns - The constructed new path.
|
|
186
|
+
*/
|
|
187
|
+
const constructPath = (locale, path, basePath$1, basePathTrailingSlash, search) => {
|
|
188
|
+
const pathWithLocalePrefix = mode === "search-params" ? path : `${locale}${path}`;
|
|
189
|
+
let newPath = `${basePath$1}${basePathTrailingSlash ? "" : "/"}${pathWithLocalePrefix}`;
|
|
190
|
+
if (search) newPath += search;
|
|
191
|
+
return newPath;
|
|
161
192
|
};
|
|
193
|
+
/**
|
|
194
|
+
* Rewrites the URL to the new path and sets the locale header.
|
|
195
|
+
*
|
|
196
|
+
* @param request - The incoming Next.js request object.
|
|
197
|
+
* @param newPath - The new path to rewrite to.
|
|
198
|
+
* @param locale - The locale to set in the response header.
|
|
199
|
+
* @returns - The rewritten response.
|
|
200
|
+
*/
|
|
162
201
|
const rewriteUrl = (request, newPath, locale) => {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
202
|
+
const search = request.nextUrl.search;
|
|
203
|
+
const pathWithSearch = search && !newPath.includes("?") ? `${newPath}${search}` : newPath;
|
|
204
|
+
const response = NextResponse.rewrite(new URL(pathWithSearch, request.url));
|
|
205
|
+
response.headers.set(headerName, locale);
|
|
206
|
+
return response;
|
|
168
207
|
};
|
|
208
|
+
/**
|
|
209
|
+
* Redirects the request to the new path.
|
|
210
|
+
*
|
|
211
|
+
* @param request - The incoming Next.js request object.
|
|
212
|
+
* @param newPath - The new path to redirect to.
|
|
213
|
+
* @returns - The redirect response.
|
|
214
|
+
*/
|
|
169
215
|
const redirectUrl = (request, newPath) => {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
};
|
|
174
|
-
export {
|
|
175
|
-
intlayerMiddleware
|
|
216
|
+
const search = request.nextUrl.search;
|
|
217
|
+
const pathWithSearch = search && !newPath.includes("?") ? `${newPath}${search}` : newPath;
|
|
218
|
+
return NextResponse.redirect(new URL(pathWithSearch, request.url));
|
|
176
219
|
};
|
|
220
|
+
|
|
221
|
+
//#endregion
|
|
222
|
+
export { intlayerMiddleware };
|
|
177
223
|
//# sourceMappingURL=intlayerMiddleware.mjs.map
|