next-intlayer 2.0.13 → 3.0.0
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/README.md +31 -28
- package/dist/cjs/client/useLocale.cjs +55 -13
- package/dist/cjs/client/useLocale.cjs.map +1 -1
- package/dist/cjs/index.cjs +4 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/middleware/intlayerMiddleware.cjs +9 -3
- package/dist/cjs/middleware/intlayerMiddleware.cjs.map +1 -1
- package/dist/cjs/middleware/localeDetector.cjs +3 -24
- package/dist/cjs/middleware/localeDetector.cjs.map +1 -1
- package/dist/cjs/server/index.cjs +4 -0
- package/dist/cjs/server/index.cjs.map +1 -1
- package/dist/esm/client/useLocale.mjs +55 -13
- package/dist/esm/client/useLocale.mjs.map +1 -1
- package/dist/esm/index.mjs +5 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/middleware/intlayerMiddleware.mjs +12 -4
- package/dist/esm/middleware/intlayerMiddleware.mjs.map +1 -1
- package/dist/esm/middleware/localeDetector.mjs +3 -14
- package/dist/esm/middleware/localeDetector.mjs.map +1 -1
- package/dist/esm/server/index.mjs +5 -1
- package/dist/esm/server/index.mjs.map +1 -1
- 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 +4 -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/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/{cjs → types}/server/withIntlayer.d.ts +3 -6
- package/dist/types/server/withIntlayer.d.ts.map +1 -0
- package/dist/types/types/NextPage.d.ts +11 -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 +36 -29
- package/dist/cjs/client/IntlayerClientProvider.d.ts +0 -7
- package/dist/cjs/client/index.d.ts +0 -5
- package/dist/cjs/client/useLocale.d.ts +0 -11
- package/dist/cjs/generateStaticParams.d.ts +0 -7
- package/dist/cjs/index.d.ts +0 -9
- package/dist/cjs/middleware/index.d.ts +0 -2
- package/dist/cjs/middleware/intlayerMiddleware.d.ts +0 -22
- package/dist/cjs/middleware/localeDetector.d.ts +0 -11
- package/dist/cjs/server/index.d.ts +0 -3
- package/dist/cjs/types/NextPage.d.ts +0 -13
- package/dist/cjs/types/index.d.ts +0 -4
- package/dist/esm/client/IntlayerClientProvider.d.mts +0 -7
- package/dist/esm/client/index.d.mts +0 -5
- package/dist/esm/client/useLocale.d.mts +0 -11
- package/dist/esm/generateStaticParams.d.mts +0 -7
- package/dist/esm/index.d.mts +0 -9
- package/dist/esm/middleware/index.d.mts +0 -2
- package/dist/esm/middleware/intlayerMiddleware.d.mts +0 -22
- package/dist/esm/middleware/localeDetector.d.mts +0 -11
- package/dist/esm/server/index.d.mts +0 -3
- package/dist/esm/server/withIntlayer.d.mts +0 -18
- package/dist/esm/types/NextPage.d.mts +0 -13
- package/dist/esm/types/index.d.mts +0 -4
package/README.md
CHANGED
|
@@ -38,14 +38,16 @@
|
|
|
38
38
|
|
|
39
39
|
import { DeclarationContent, t } from "intlayer";
|
|
40
40
|
|
|
41
|
-
const clientComponentContent
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
41
|
+
const clientComponentContent = {
|
|
42
|
+
key: "client-component",
|
|
43
|
+
content: {
|
|
44
|
+
myTranslatedContent: t({
|
|
45
|
+
en: "Hello World",
|
|
46
|
+
fr: "Bonjour le monde",
|
|
47
|
+
es: "Hola Mundo",
|
|
48
|
+
}),
|
|
49
|
+
},
|
|
50
|
+
} satisfies DeclarationContent;
|
|
49
51
|
|
|
50
52
|
export default clientComponentContent;
|
|
51
53
|
```
|
|
@@ -85,11 +87,11 @@ npm install intlayer next-intlayer
|
|
|
85
87
|
```
|
|
86
88
|
|
|
87
89
|
```bash
|
|
88
|
-
yarn
|
|
90
|
+
yarn add intlayer next-intlayer
|
|
89
91
|
```
|
|
90
92
|
|
|
91
93
|
```bash
|
|
92
|
-
pnpm
|
|
94
|
+
pnpm add intlayer next-intlayer
|
|
93
95
|
```
|
|
94
96
|
|
|
95
97
|
## Step 2: Configure Your Project
|
|
@@ -116,7 +118,7 @@ const config: IntlayerConfig = {
|
|
|
116
118
|
export default config;
|
|
117
119
|
```
|
|
118
120
|
|
|
119
|
-
To see all available parameters, refer to the [configuration documentation here](https://github.com/
|
|
121
|
+
To see all available parameters, refer to the [configuration documentation here](https://github.com/aymericzip/intlayer/blob/main/docs/docs/configuration_en.md).
|
|
120
122
|
|
|
121
123
|
## Step 3: Integrate Intlayer in Your Next.js Configuration
|
|
122
124
|
|
|
@@ -177,11 +179,12 @@ Then add a new layout in your `[locale]` directory:
|
|
|
177
179
|
|
|
178
180
|
import { NextLayoutIntlayer } from "next-intlayer";
|
|
179
181
|
import { Inter } from "next/font/google";
|
|
182
|
+
import { getHTMLTextDir } from "intlayer";
|
|
180
183
|
|
|
181
184
|
const inter = Inter({ subsets: ["latin"] });
|
|
182
185
|
|
|
183
186
|
const LocaleLayout: NextLayoutIntlayer = ({ children, params: { locale } }) => (
|
|
184
|
-
<html lang={locale}>
|
|
187
|
+
<html lang={locale} dir={getHTMLTextDir(locale)}>
|
|
185
188
|
<body className={inter.className}>{children}</body>
|
|
186
189
|
</html>
|
|
187
190
|
);
|
|
@@ -197,24 +200,24 @@ Create and manage your content dictionaries:
|
|
|
197
200
|
// src/app/[locale]/page.content.ts
|
|
198
201
|
import { t, type DeclarationContent } from "intlayer";
|
|
199
202
|
|
|
200
|
-
const pageContent
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
203
|
+
const pageContent = {
|
|
204
|
+
key: "page",
|
|
205
|
+
content: {
|
|
206
|
+
getStarted: {
|
|
207
|
+
main: t({
|
|
208
|
+
en: "Get started by editing",
|
|
209
|
+
fr: "Commencez par éditer",
|
|
210
|
+
es: "Comience por editar",
|
|
211
|
+
}),
|
|
212
|
+
pageLink: "src/app/page.tsx",
|
|
213
|
+
},
|
|
209
214
|
},
|
|
210
|
-
};
|
|
215
|
+
} satisfies DeclarationContent;
|
|
211
216
|
|
|
212
217
|
export default pageContent;
|
|
213
218
|
```
|
|
214
219
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
[See how to declare your Intlayer declaration files](https://github.com/aypineau/intlayer/blob/main/docs/docs/content_declaration/get_started_en.md).
|
|
220
|
+
[See how to declare your Intlayer declaration files](https://github.com/aymericzip/intlayer/blob/main/docs/docs/content_declaration/get_started_en.md).
|
|
218
221
|
|
|
219
222
|
## Step 7: Utilize Content in Your Code
|
|
220
223
|
|
|
@@ -302,7 +305,7 @@ export const ServerComponentExample = () => {
|
|
|
302
305
|
> <img src={content.image.src.value} alt={content.image.value} />
|
|
303
306
|
> ```
|
|
304
307
|
|
|
305
|
-
For more detailed usage of intlayer into Client, or Server component, see the [nextJS example here](https://github.com/
|
|
308
|
+
For more detailed usage of intlayer into Client, or Server component, see the [nextJS example here](https://github.com/aymericzip/intlayer/blob/main/examples/nextjs-app/src/app/%5Blocale%5D/demo-usage-components/page.tsx).
|
|
306
309
|
|
|
307
310
|
## (Optional) Step 8: Internationalization of your metadata
|
|
308
311
|
|
|
@@ -357,9 +360,9 @@ const MyComponent = () => {
|
|
|
357
360
|
|
|
358
361
|
Intlayer use module augmentation to get benefits of TypeScript and make your codebase stronger.
|
|
359
362
|
|
|
360
|
-

|
|
361
364
|
|
|
362
|
-

|
|
363
366
|
|
|
364
367
|
Ensure your TypeScript configuration includes the autogenerated types.
|
|
365
368
|
|
|
@@ -23,33 +23,75 @@ __export(useLocale_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(useLocale_exports);
|
|
24
24
|
var import_client = require("@intlayer/config/client");
|
|
25
25
|
var import_navigation = require("next/navigation.js");
|
|
26
|
+
var import_react = require("react");
|
|
26
27
|
var import_react_intlayer = require("react-intlayer");
|
|
27
28
|
const useLocale = () => {
|
|
28
|
-
const {
|
|
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;
|
|
29
42
|
const { setLocaleCookie } = (0, import_react_intlayer.useLocaleCookie)();
|
|
30
43
|
const reactLocaleHook = (0, import_react_intlayer.useLocaleBase)();
|
|
31
|
-
const
|
|
44
|
+
const { push, refresh } = (0, import_navigation.useRouter)();
|
|
32
45
|
const pathname = (0, import_navigation.usePathname)();
|
|
33
46
|
const {
|
|
34
47
|
defaultLocale,
|
|
35
48
|
availableLocales,
|
|
36
|
-
locale: currentLocale
|
|
49
|
+
locale: currentLocale,
|
|
50
|
+
setLocale: setLocaleState
|
|
37
51
|
} = reactLocaleHook;
|
|
38
|
-
const
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
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;
|
|
42
58
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return router.push(pathWithoutLocale);
|
|
59
|
+
const slicedPath = pathname.slice(`/${currentLocale}`.length);
|
|
60
|
+
if (slicedPath) {
|
|
61
|
+
return slicedPath;
|
|
47
62
|
}
|
|
48
|
-
return
|
|
63
|
+
return "/";
|
|
49
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
|
+
);
|
|
50
91
|
return {
|
|
51
92
|
...reactLocaleHook,
|
|
52
|
-
setLocale
|
|
93
|
+
setLocale,
|
|
94
|
+
pathWithoutLocale
|
|
53
95
|
};
|
|
54
96
|
};
|
|
55
97
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1 +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 { useLocaleCookie, useLocaleBase } from 'react-intlayer';\n\nexport const useLocale = () => {\n const { prefixDefault } = getConfiguration().middleware;\n const { setLocaleCookie } = useLocaleCookie();\n const reactLocaleHook = useLocaleBase();\n const
|
|
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":[]}
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -22,6 +22,8 @@ __export(src_exports, {
|
|
|
22
22
|
IntlayerClientProvider: () => import_client.IntlayerClientProvider,
|
|
23
23
|
generateStaticParams: () => import_generateStaticParams.generateStaticParams,
|
|
24
24
|
getTranslation: () => import_react_intlayer.getTranslation,
|
|
25
|
+
t: () => import_react_intlayer.t,
|
|
26
|
+
useDictionary: () => import_react_intlayer.useDictionary,
|
|
25
27
|
useIntlayer: () => import_react_intlayer.useIntlayer,
|
|
26
28
|
useLocale: () => import_client.useLocale,
|
|
27
29
|
useLocaleCookie: () => import_react_intlayer.useLocaleCookie,
|
|
@@ -37,6 +39,8 @@ var import_client = require('./client/index.cjs');
|
|
|
37
39
|
IntlayerClientProvider,
|
|
38
40
|
generateStaticParams,
|
|
39
41
|
getTranslation,
|
|
42
|
+
t,
|
|
43
|
+
useDictionary,
|
|
40
44
|
useIntlayer,
|
|
41
45
|
useLocale,
|
|
42
46
|
useLocaleCookie,
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export {\n getTranslation,\n IntlayerClientContext,\n useIntlayer,\n useTraduction,\n useLocaleCookie,\n type IntlayerNode,\n} from 'react-intlayer';\nexport { generateStaticParams } from './generateStaticParams';\nexport type {\n LocalParams,\n NextPageIntlayer,\n NextLayoutIntlayer,\n} from './types/index';\nexport {\n useLocale,\n IntlayerClientProvider,\n type IntlayerClientProviderProps,\n} from './client/index';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,
|
|
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 NextPageIntlayer,\n NextLayoutIntlayer,\n} from './types/index';\nexport {\n useLocale,\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,4BASO;AACP,kCAAqC;AAMrC,oBAIO;","names":[]}
|
|
@@ -34,7 +34,7 @@ const {
|
|
|
34
34
|
serverSetCookie,
|
|
35
35
|
noPrefix
|
|
36
36
|
} = middleware;
|
|
37
|
-
const intlayerMiddleware = (request) => {
|
|
37
|
+
const intlayerMiddleware = (request, _event, _response) => {
|
|
38
38
|
const pathname = request.nextUrl.pathname;
|
|
39
39
|
const cookieLocale = getCookieLocale(request);
|
|
40
40
|
const basePathTrailingSlash = basePath.endsWith("/");
|
|
@@ -109,7 +109,10 @@ const handleMissingPathLocale = (request, cookieLocale, pathname, basePathTraili
|
|
|
109
109
|
return prefixDefault || locale !== defaultLocale ? redirectUrl(request, newPath) : rewriteUrl(request, newPath, locale);
|
|
110
110
|
};
|
|
111
111
|
const handleExistingPathLocale = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => {
|
|
112
|
-
if (
|
|
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
|
+
) {
|
|
113
116
|
const newPath = handleCookieLocaleMismatch(
|
|
114
117
|
request,
|
|
115
118
|
pathname,
|
|
@@ -138,7 +141,10 @@ const handleCookieLocaleMismatch = (request, pathname, pathLocale, cookieLocale,
|
|
|
138
141
|
);
|
|
139
142
|
};
|
|
140
143
|
const handleDefaultLocaleRedirect = (request, pathLocale, pathname, basePathTrailingSlash) => {
|
|
141
|
-
if (
|
|
144
|
+
if (
|
|
145
|
+
// If default locale should not be prefixed and the pathLocale is the defaultLocale
|
|
146
|
+
!prefixDefault && pathLocale === defaultLocale
|
|
147
|
+
) {
|
|
142
148
|
let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || "/";
|
|
143
149
|
if (basePathTrailingSlash) {
|
|
144
150
|
pathWithoutLocale = pathWithoutLocale.slice(1);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/middleware/intlayerMiddleware.ts"],"sourcesContent":["import { type Locales, getConfiguration } from '@intlayer/config/client';\nimport { type NextRequest, NextResponse } 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 * // ./src/middleware.ts\n *\n * ```ts\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 */\nexport const intlayerMiddleware = (request: NextRequest): NextResponse => {\n const pathname = request.nextUrl.pathname;\n const cookieLocale = getCookieLocale(request);\n const basePathTrailingSlash = basePath.endsWith('/');\n\n if (noPrefix) {\n return handleNoPrefix(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n const pathLocale = getPathLocale(pathname);\n return handlePrefix(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\n};\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\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\nconst getPathLocale = (pathname: string): Locales | undefined =>\n locales.find(\n (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`\n );\n\nconst handlePrefix = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (!pathLocale) {\n return handleMissingPathLocale(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n return handleExistingPathLocale(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\n};\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 return prefixDefault || locale !== defaultLocale\n ? redirectUrl(request, newPath)\n : rewriteUrl(request, newPath, locale);\n};\n\nconst handleExistingPathLocale = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\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 return handleDefaultLocaleRedirect(\n request,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\n};\n\nconst handleCookieLocaleMismatch = (\n request: NextRequest,\n\n pathname: string,\n pathLocale: Locales,\n cookieLocale: Locales,\n basePath: string,\n basePathTrailingSlash: boolean\n): string => {\n const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);\n return constructPath(\n cookieLocale,\n newPath,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\n );\n};\n\nconst handleDefaultLocaleRedirect = (\n request: NextRequest,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (!prefixDefault && pathLocale === defaultLocale) {\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 return rewriteUrl(request, pathname, pathLocale);\n};\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 newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${newPath}`;\n if (search) {\n newPath += search;\n }\n return newPath;\n};\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\nconst redirectUrl = (request: NextRequest, newPath: string): NextResponse =>\n NextResponse.redirect(new URL(newPath, request.url));\n"],"mappings":";;;;;;;;;;;;;;;;;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AAQ;AAC1B,2BAAqB,QAAQ,QAAQ;AAAA,IACvC;AAEA,WAAO,WAAW,SAAS,GAAG,QAAQ,GAAG,iBAAiB,IAAI,UAAU;AAAA,EAC1E;AACA,SAAO,WAAW,SAAS,UAAU,UAAU;AACjD;AAEA,MAAM,gBAAgB,CACpB,QACA,MACAA,WACA,uBACA,WACW;AACX,MAAI,UAAU,GAAG,MAAM,GAAG,IAAI;AAC9B,YAAU,GAAGA,SAAQ,GAAG,wBAAwB,KAAK,GAAG,GAAG,OAAO;AAClE,MAAI,QAAQ;AACV,eAAW;AAAA,EACb;AACA,SAAO;AACT;AAEA,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;AAEA,MAAM,cAAc,CAAC,SAAsB,YACzC,2BAAa,SAAS,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;","names":["basePath"]}
|
|
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,EAC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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"]}
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,37 +15,18 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
var localeDetector_exports = {};
|
|
30
20
|
__export(localeDetector_exports, {
|
|
31
21
|
localeDetector: () => localeDetector
|
|
32
22
|
});
|
|
33
23
|
module.exports = __toCommonJS(localeDetector_exports);
|
|
34
|
-
var
|
|
35
|
-
var import_client = require("@intlayer/config/client");
|
|
36
|
-
var import_negotiator = __toESM(require("negotiator"));
|
|
37
|
-
const { locales, defaultLocale } = (0, import_client.getConfiguration)().internationalization;
|
|
24
|
+
var import_core = require("@intlayer/core");
|
|
38
25
|
const localeDetector = (request) => {
|
|
39
26
|
const negotiatorHeaders = {};
|
|
40
27
|
request.headers.forEach((value, key) => negotiatorHeaders[key] = value);
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
return (0, import_intl_localematcher.match)(languages, locales, defaultLocale);
|
|
44
|
-
} catch (e) {
|
|
45
|
-
console.warn(
|
|
46
|
-
`No valid locales in accept-language header: ${languages.join(", ")}`
|
|
47
|
-
);
|
|
48
|
-
console.warn(`Reverting to using defaultLocale: ${defaultLocale}`);
|
|
49
|
-
return defaultLocale;
|
|
50
|
-
}
|
|
28
|
+
const locale = (0, import_core.localeDetector)(negotiatorHeaders);
|
|
29
|
+
return locale;
|
|
51
30
|
};
|
|
52
31
|
// Annotate the CommonJS export names for ESM import in node:
|
|
53
32
|
0 && (module.exports = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/middleware/localeDetector.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"sources":["../../../src/middleware/localeDetector.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config';\nimport { localeDetector as localeDetectorCore } from '@intlayer/core';\nimport type { NextRequest } from 'next/server.js';\n\n/**\n * Detects the locale from the request headers\n *\n * Headers are provided by the browser and can be used to determine the user's preferred language\n */\nexport const localeDetector = (request: NextRequest): Locales => {\n const negotiatorHeaders: Record<string, string> = {};\n\n request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));\n\n const locale = localeDetectorCore(negotiatorHeaders);\n\n return locale;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,kBAAqD;AAQ9C,MAAM,iBAAiB,CAAC,YAAkC;AAC/D,QAAM,oBAA4C,CAAC;AAEnD,UAAQ,QAAQ,QAAQ,CAAC,OAAO,QAAS,kBAAkB,GAAG,IAAI,KAAM;AAExE,QAAM,aAAS,YAAAA,gBAAmB,iBAAiB;AAEnD,SAAO;AACT;","names":["localeDetectorCore"]}
|
|
@@ -22,6 +22,8 @@ __export(server_exports, {
|
|
|
22
22
|
IntlayerServerProvider: () => import_server.IntlayerServerProvider,
|
|
23
23
|
getLocaleContent: () => import_server.getLocaleContent,
|
|
24
24
|
locale: () => import_server.locale,
|
|
25
|
+
t: () => import_server.t,
|
|
26
|
+
useDictionary: () => import_server.useDictionary,
|
|
25
27
|
useIntlayer: () => import_server.useIntlayer,
|
|
26
28
|
useTraduction: () => import_server.useTraduction,
|
|
27
29
|
withIntlayer: () => import_withIntlayer.withIntlayer
|
|
@@ -35,6 +37,8 @@ var import_withIntlayer = require('./withIntlayer.cjs');
|
|
|
35
37
|
IntlayerServerProvider,
|
|
36
38
|
getLocaleContent,
|
|
37
39
|
locale,
|
|
40
|
+
t,
|
|
41
|
+
useDictionary,
|
|
38
42
|
useIntlayer,
|
|
39
43
|
useTraduction,
|
|
40
44
|
withIntlayer
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/index.ts"],"sourcesContent":["export {\n getLocaleContent,\n useTraduction,\n IntlayerServer,\n IntlayerServerProvider,\n locale,\n useIntlayer,\n} from 'react-intlayer/server';\nexport { withIntlayer } from './withIntlayer';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,
|
|
1
|
+
{"version":3,"sources":["../../../src/server/index.ts"],"sourcesContent":["export {\n getLocaleContent,\n useDictionary,\n useTraduction,\n IntlayerServer,\n IntlayerServerProvider,\n locale,\n useIntlayer,\n t,\n} from 'react-intlayer/server';\nexport { withIntlayer } from './withIntlayer';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBASO;AACP,0BAA6B;","names":[]}
|
|
@@ -1,32 +1,74 @@
|
|
|
1
1
|
import { getConfiguration } from "@intlayer/config/client";
|
|
2
2
|
import { usePathname, useRouter } from "next/navigation.js";
|
|
3
|
+
import { useCallback } from "react";
|
|
3
4
|
import { useLocaleCookie, useLocaleBase } from "react-intlayer";
|
|
4
5
|
const useLocale = () => {
|
|
5
|
-
const {
|
|
6
|
+
const {
|
|
7
|
+
/**
|
|
8
|
+
* Prefix default prefix the default locale to the path as other locales.
|
|
9
|
+
*
|
|
10
|
+
* Example with prefixDefault = true and defaultLocale = 'en':
|
|
11
|
+
* path = /en/dashboard or /fr/dashboard
|
|
12
|
+
*
|
|
13
|
+
* Example with prefixDefault = false and defaultLocale = 'en':
|
|
14
|
+
* path = /dashboard or /fr/dashboard
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
prefixDefault
|
|
18
|
+
} = getConfiguration().middleware;
|
|
6
19
|
const { setLocaleCookie } = useLocaleCookie();
|
|
7
20
|
const reactLocaleHook = useLocaleBase();
|
|
8
|
-
const
|
|
21
|
+
const { push, refresh } = useRouter();
|
|
9
22
|
const pathname = usePathname();
|
|
10
23
|
const {
|
|
11
24
|
defaultLocale,
|
|
12
25
|
availableLocales,
|
|
13
|
-
locale: currentLocale
|
|
26
|
+
locale: currentLocale,
|
|
27
|
+
setLocale: setLocaleState
|
|
14
28
|
} = reactLocaleHook;
|
|
15
|
-
const
|
|
16
|
-
if (
|
|
17
|
-
|
|
18
|
-
|
|
29
|
+
const getPathWithoutLocale = () => {
|
|
30
|
+
if (
|
|
31
|
+
// If the locale is the default one and the prefixDefault is false, we don't need to add the locale to the path
|
|
32
|
+
!prefixDefault && currentLocale.toString() === defaultLocale.toString()
|
|
33
|
+
) {
|
|
34
|
+
return pathname;
|
|
19
35
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return router.push(pathWithoutLocale);
|
|
36
|
+
const slicedPath = pathname.slice(`/${currentLocale}`.length);
|
|
37
|
+
if (slicedPath) {
|
|
38
|
+
return slicedPath;
|
|
24
39
|
}
|
|
25
|
-
return
|
|
40
|
+
return "/";
|
|
26
41
|
};
|
|
42
|
+
const pathWithoutLocale = getPathWithoutLocale();
|
|
43
|
+
const setLocale = useCallback(
|
|
44
|
+
(locale) => {
|
|
45
|
+
if (!availableLocales.includes(locale)) {
|
|
46
|
+
console.error(`Locale ${locale} is not available`);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
setLocaleCookie(locale);
|
|
50
|
+
setLocaleState(locale);
|
|
51
|
+
if (!prefixDefault && locale.toString() === defaultLocale.toString()) {
|
|
52
|
+
push(pathWithoutLocale);
|
|
53
|
+
} else {
|
|
54
|
+
push(`/${locale}${pathWithoutLocale}`);
|
|
55
|
+
}
|
|
56
|
+
return refresh();
|
|
57
|
+
},
|
|
58
|
+
[
|
|
59
|
+
currentLocale,
|
|
60
|
+
availableLocales,
|
|
61
|
+
setLocaleCookie,
|
|
62
|
+
setLocaleState,
|
|
63
|
+
prefixDefault,
|
|
64
|
+
defaultLocale,
|
|
65
|
+
pathWithoutLocale
|
|
66
|
+
]
|
|
67
|
+
);
|
|
27
68
|
return {
|
|
28
69
|
...reactLocaleHook,
|
|
29
|
-
setLocale
|
|
70
|
+
setLocale,
|
|
71
|
+
pathWithoutLocale
|
|
30
72
|
};
|
|
31
73
|
};
|
|
32
74
|
export {
|
|
@@ -1 +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 { useLocaleCookie, useLocaleBase } from 'react-intlayer';\n\nexport const useLocale = () => {\n const { prefixDefault } = getConfiguration().middleware;\n const { setLocaleCookie } = useLocaleCookie();\n const reactLocaleHook = useLocaleBase();\n const
|
|
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,SAAuB,wBAAwB;AAC/C,SAAS,aAAa,iBAAiB;AACvC,SAAS,mBAAmB;AAC5B,SAAS,iBAAiB,qBAAqB;AAExC,MAAM,YAAY,MAAM;AAC7B,QAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAWJ;AAAA,EACF,IAAI,iBAAiB,EAAE;AACvB,QAAM,EAAE,gBAAgB,IAAI,gBAAgB;AAC5C,QAAM,kBAAkB,cAAc;AACtC,QAAM,EAAE,MAAM,QAAQ,IAAI,UAAU;AACpC,QAAM,WAAW,YAAY;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,YAAY;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":[]}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -3,7 +3,9 @@ import {
|
|
|
3
3
|
IntlayerClientContext,
|
|
4
4
|
useIntlayer,
|
|
5
5
|
useTraduction,
|
|
6
|
-
|
|
6
|
+
useDictionary,
|
|
7
|
+
useLocaleCookie,
|
|
8
|
+
t
|
|
7
9
|
} from "react-intlayer";
|
|
8
10
|
import { generateStaticParams } from './generateStaticParams.mjs';
|
|
9
11
|
import {
|
|
@@ -15,6 +17,8 @@ export {
|
|
|
15
17
|
IntlayerClientProvider,
|
|
16
18
|
generateStaticParams,
|
|
17
19
|
getTranslation,
|
|
20
|
+
t,
|
|
21
|
+
useDictionary,
|
|
18
22
|
useIntlayer,
|
|
19
23
|
useLocale,
|
|
20
24
|
useLocaleCookie,
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export {\n getTranslation,\n IntlayerClientContext,\n useIntlayer,\n useTraduction,\n useLocaleCookie,\n type IntlayerNode,\n} from 'react-intlayer';\nexport { generateStaticParams } from './generateStaticParams';\nexport type {\n LocalParams,\n NextPageIntlayer,\n NextLayoutIntlayer,\n} from './types/index';\nexport {\n useLocale,\n IntlayerClientProvider,\n type IntlayerClientProviderProps,\n} from './client/index';\n"],"mappings":"AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,
|
|
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 NextPageIntlayer,\n NextLayoutIntlayer,\n} from './types/index';\nexport {\n useLocale,\n IntlayerClientProvider,\n type IntlayerClientProviderProps,\n} from './client/index';\n"],"mappings":"AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,OACK;AACP,SAAS,4BAA4B;AAMrC;AAAA,EACE;AAAA,EACA;AAAA,OAEK;","names":[]}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { getConfiguration } from "@intlayer/config/client";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
NextResponse
|
|
4
|
+
} from "next/server";
|
|
3
5
|
import { localeDetector } from './localeDetector.mjs';
|
|
4
6
|
const { internationalization, middleware } = getConfiguration();
|
|
5
7
|
const { locales, defaultLocale } = internationalization;
|
|
@@ -11,7 +13,7 @@ const {
|
|
|
11
13
|
serverSetCookie,
|
|
12
14
|
noPrefix
|
|
13
15
|
} = middleware;
|
|
14
|
-
const intlayerMiddleware = (request) => {
|
|
16
|
+
const intlayerMiddleware = (request, _event, _response) => {
|
|
15
17
|
const pathname = request.nextUrl.pathname;
|
|
16
18
|
const cookieLocale = getCookieLocale(request);
|
|
17
19
|
const basePathTrailingSlash = basePath.endsWith("/");
|
|
@@ -86,7 +88,10 @@ const handleMissingPathLocale = (request, cookieLocale, pathname, basePathTraili
|
|
|
86
88
|
return prefixDefault || locale !== defaultLocale ? redirectUrl(request, newPath) : rewriteUrl(request, newPath, locale);
|
|
87
89
|
};
|
|
88
90
|
const handleExistingPathLocale = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => {
|
|
89
|
-
if (
|
|
91
|
+
if (
|
|
92
|
+
// If the cookie locale is set and differs from the locale in the URL, and server should not always set cookie
|
|
93
|
+
cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always"
|
|
94
|
+
) {
|
|
90
95
|
const newPath = handleCookieLocaleMismatch(
|
|
91
96
|
request,
|
|
92
97
|
pathname,
|
|
@@ -115,7 +120,10 @@ const handleCookieLocaleMismatch = (request, pathname, pathLocale, cookieLocale,
|
|
|
115
120
|
);
|
|
116
121
|
};
|
|
117
122
|
const handleDefaultLocaleRedirect = (request, pathLocale, pathname, basePathTrailingSlash) => {
|
|
118
|
-
if (
|
|
123
|
+
if (
|
|
124
|
+
// If default locale should not be prefixed and the pathLocale is the defaultLocale
|
|
125
|
+
!prefixDefault && pathLocale === defaultLocale
|
|
126
|
+
) {
|
|
119
127
|
let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || "/";
|
|
120
128
|
if (basePathTrailingSlash) {
|
|
121
129
|
pathWithoutLocale = pathWithoutLocale.slice(1);
|