next-intlayer 1.0.1 → 1.2.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 +199 -0
- package/dist/cjs/client/index.cjs +4 -4
- package/dist/cjs/client/index.cjs.map +1 -1
- package/dist/cjs/client/index.d.ts +1 -1
- package/dist/cjs/index.cjs +4 -4
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/middleware/intlayerMiddleware.cjs.map +1 -1
- package/dist/cjs/middleware/intlayerMiddleware.d.ts +15 -0
- package/dist/cjs/middleware/localeDetector.cjs.map +1 -1
- package/dist/cjs/middleware/localeDetector.d.ts +5 -0
- package/dist/cjs/server/index.cjs +4 -4
- package/dist/cjs/server/index.cjs.map +1 -1
- package/dist/cjs/server/index.d.ts +1 -1
- package/dist/cjs/server/withIntlayer.cjs +12 -15
- package/dist/cjs/server/withIntlayer.cjs.map +1 -1
- package/dist/cjs/server/withIntlayer.d.ts +10 -0
- package/dist/esm/client/index.d.mts +1 -1
- package/dist/esm/client/index.mjs +4 -4
- package/dist/esm/client/index.mjs.map +1 -1
- package/dist/esm/index.d.mts +1 -1
- package/dist/esm/index.mjs +4 -4
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/middleware/intlayerMiddleware.d.mts +15 -0
- package/dist/esm/middleware/intlayerMiddleware.mjs.map +1 -1
- package/dist/esm/middleware/localeDetector.d.mts +5 -0
- package/dist/esm/middleware/localeDetector.mjs.map +1 -1
- package/dist/esm/server/index.d.mts +1 -1
- package/dist/esm/server/index.mjs +4 -4
- package/dist/esm/server/index.mjs.map +1 -1
- package/dist/esm/server/withIntlayer.d.mts +10 -0
- package/dist/esm/server/withIntlayer.mjs +12 -15
- package/dist/esm/server/withIntlayer.mjs.map +1 -1
- package/package.json +11 -9
- package/src/client/index.ts +2 -2
- package/src/index.ts +2 -2
- package/src/middleware/intlayerMiddleware.ts +15 -0
- package/src/middleware/localeDetector.ts +5 -0
- package/src/server/index.ts +2 -2
- package/src/server/withIntlayer.ts +33 -27
package/README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# Intlayer: Next-Level Content Management in JavaScript
|
|
2
|
+
|
|
3
|
+
**Intlayer** is an innovative Content Management System (CMS) designed specifically for JavaScript developers. It enables seamless transpilation of JavaScript content into structured dictionaries, making integration into your codebase straightforward and efficient.
|
|
4
|
+
|
|
5
|
+
## Why Choose Intlayer?
|
|
6
|
+
|
|
7
|
+
- **JavaScript-Powered Content Management**: Harness the flexibility of JavaScript to define and manage your content efficiently.
|
|
8
|
+
- **Type-Safe Environment**: Leverage TypeScript to ensure all your content definitions are precise and error-free.
|
|
9
|
+
- **Integrated Content Files**: Keep your translations close to their respective components, enhancing maintainability and clarity.
|
|
10
|
+
- **Simplified Setup**: Get up and running quickly with minimal configuration, especially optimized for Next.js projects.
|
|
11
|
+
- **Server Component Support**: Perfectly suited for Next.js server components, ensuring smooth server-side rendering.
|
|
12
|
+
- **Enhanced Routing**: Full support for Next.js app routing, adapting seamlessly to complex application structures.
|
|
13
|
+
|
|
14
|
+
## Getting Started with Intlayer
|
|
15
|
+
|
|
16
|
+
Setting up Intlayer in a Next.js application is straightforward:
|
|
17
|
+
|
|
18
|
+
### Step 1: Install Dependencies
|
|
19
|
+
|
|
20
|
+
Install the necessary packages using npm:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install intlayer next-intlayer
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
yarn install intlayer next-intlayer
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm install intlayer next-intlayer
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Step 2: Integrate Intlayer in Your Next.js Configuration
|
|
35
|
+
|
|
36
|
+
Configure your Next.js setup to use Intlayer:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
// next.config.mjs
|
|
40
|
+
import { withIntlayer } from "next-intlayer/server";
|
|
41
|
+
|
|
42
|
+
const nextConfig = {};
|
|
43
|
+
|
|
44
|
+
export default withIntlayer(nextConfig);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Step 3: Configure Middleware for Locale Detection
|
|
48
|
+
|
|
49
|
+
Set up middleware to detect the user's preferred locale:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
// src/middleware.ts
|
|
53
|
+
export { intlayerMiddleware as middleware } from 'next-intlayer/middleware';
|
|
54
|
+
|
|
55
|
+
export const config = {
|
|
56
|
+
matcher: '/((?!api|static|._\\.._|\_next).*),
|
|
57
|
+
};
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Step 4: Define Dynamic Locale Routes
|
|
61
|
+
|
|
62
|
+
Implement dynamic routing for localized content:
|
|
63
|
+
|
|
64
|
+
Change `src/app/page.ts` to `src/app/[locale]/page.ts`
|
|
65
|
+
|
|
66
|
+
### Step 5: Manage Your Content
|
|
67
|
+
|
|
68
|
+
Create and manage your content dictionaries:
|
|
69
|
+
|
|
70
|
+
```tsx
|
|
71
|
+
// src/app/[locale]/page.content.ts
|
|
72
|
+
import { t, type ContentModule } from "intlayer";
|
|
73
|
+
|
|
74
|
+
const pageContent: ContentModule = {
|
|
75
|
+
id: "page",
|
|
76
|
+
getStarted: {
|
|
77
|
+
main: t({
|
|
78
|
+
en: "Get started by editing",
|
|
79
|
+
fr: "Commencez par éditer",
|
|
80
|
+
es: "Comience por editar",
|
|
81
|
+
}),
|
|
82
|
+
pageLink: "src/app/page.tsx",
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export default pageContent;
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
[See how to declare your Intlayer declaration files](https://github.com/aypineau/intlayer/blob/main/packages/intlayer/readme.md).
|
|
90
|
+
|
|
91
|
+
### Step 6: Utilize Content in Your Code
|
|
92
|
+
|
|
93
|
+
Access your content dictionaries throughout your application:
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
// src/app/[locale]/page.ts
|
|
97
|
+
|
|
98
|
+
import { ClientComponentExample } from "@component/components/ClientComponentExample";
|
|
99
|
+
import { LocaleSwitcher } from "@component/components/LangSwitcherDropDown";
|
|
100
|
+
import { NestedServerComponentExample } from "@component/components/NestedServerComponentExample";
|
|
101
|
+
import { ServerComponentExample } from "@component/components/ServerComponentExample";
|
|
102
|
+
import { type NextPageIntlayer, IntlayerClientProvider } from "next-intlayer";
|
|
103
|
+
import { IntlayerServerProvider, useIntlayer } from "next-intlayer/server";
|
|
104
|
+
|
|
105
|
+
const Page: NextPageIntlayer = ({ params: { locale } }) => {
|
|
106
|
+
const content = useIntlayer("page", locale);
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<>
|
|
110
|
+
<p>
|
|
111
|
+
{content.getStarted.main}
|
|
112
|
+
<code>{content.getStarted.pageLink}</code>
|
|
113
|
+
</p>
|
|
114
|
+
{/**
|
|
115
|
+
* IntlayerServerProvider is used to provide the locale to the server children
|
|
116
|
+
* Don't work if set in the layout
|
|
117
|
+
*/}
|
|
118
|
+
<IntlayerServerProvider locale={locale}>
|
|
119
|
+
<ServerComponentExample />
|
|
120
|
+
</IntlayerServerProvider>
|
|
121
|
+
{/**
|
|
122
|
+
* IntlayerClientProvider is used to provide the locale to the client children
|
|
123
|
+
* Can be set in any parent component, including the layout
|
|
124
|
+
*/}
|
|
125
|
+
<IntlayerClientProvider locale={locale}>
|
|
126
|
+
<ClientComponentExample />
|
|
127
|
+
</IntlayerClientProvider>
|
|
128
|
+
</>
|
|
129
|
+
);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export default Page;
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
```tsx
|
|
136
|
+
// src/components/ClientComponentExample.tsx
|
|
137
|
+
|
|
138
|
+
"use client";
|
|
139
|
+
|
|
140
|
+
import { useIntlayer } from "next-intlayer";
|
|
141
|
+
|
|
142
|
+
export const ClientComponentExample = () => {
|
|
143
|
+
const content = useIntlayer("client-component-example"); // Create related content declaration
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<div>
|
|
147
|
+
<h2>{content.title} </h2>
|
|
148
|
+
<p>{content.content}</p>
|
|
149
|
+
</div>
|
|
150
|
+
);
|
|
151
|
+
};
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
```tsx
|
|
155
|
+
// src/components/ServerComponentExample.tsx
|
|
156
|
+
|
|
157
|
+
import { useIntlayer } from "next-intlayer/server";
|
|
158
|
+
|
|
159
|
+
export const ServerComponentExample = () => {
|
|
160
|
+
const content = useIntlayer("server-component-example"); // Create related content declaration
|
|
161
|
+
|
|
162
|
+
return (
|
|
163
|
+
<div>
|
|
164
|
+
<h2>{content.title} </h2>
|
|
165
|
+
<p>{content.content}</p>
|
|
166
|
+
</div>
|
|
167
|
+
);
|
|
168
|
+
};
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
For more detailed usage of intlayer into Client, or Server component, see the [nextJS example here](https://github.com/aypineau/intlayer/blob/main/examples/nextjs-app/src/app/%5Blocale%5D/demo-usage-components/page.tsx).
|
|
172
|
+
|
|
173
|
+
## Configuration of your project
|
|
174
|
+
|
|
175
|
+
Create a config file to configure the languages of your application:
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
// intlayer.config.ts
|
|
179
|
+
|
|
180
|
+
import { Locales, type IntlayerConfig } from "intlayer";
|
|
181
|
+
|
|
182
|
+
const config: IntlayerConfig = {
|
|
183
|
+
internationalization: {
|
|
184
|
+
locales: [
|
|
185
|
+
Locales.ENGLISH,
|
|
186
|
+
// Your other locales
|
|
187
|
+
],
|
|
188
|
+
defaultLocale: Locales.ENGLISH,
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export default config;
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
To see all available parameters, refer to the [configuration documentation here](https://github.com/aypineau/intlayer/blob/main/docs/configuration.md).
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
This version emphasizes ease of use, practical steps, and the professional application of Intlayer in a Next.js environment.
|
|
@@ -18,8 +18,8 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var client_exports = {};
|
|
20
20
|
__export(client_exports, {
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
IntlayerClient: () => import_react_intlayer.IntlayerClient,
|
|
22
|
+
IntlayerClientProvider: () => import_react_intlayer.IntlayerClientProvider,
|
|
23
23
|
getTranslation: () => import_react_intlayer.getTranslation,
|
|
24
24
|
useIntlayer: () => import_react_intlayer.useIntlayer,
|
|
25
25
|
useLocale: () => import_useLocale.useLocale,
|
|
@@ -31,8 +31,8 @@ var import_react_intlayer = require("react-intlayer");
|
|
|
31
31
|
var import_useLocale = require('./useLocale.cjs');
|
|
32
32
|
// Annotate the CommonJS export names for ESM import in node:
|
|
33
33
|
0 && (module.exports = {
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
IntlayerClient,
|
|
35
|
+
IntlayerClientProvider,
|
|
36
36
|
getTranslation,
|
|
37
37
|
useIntlayer,
|
|
38
38
|
useLocale,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/client/index.ts"],"sourcesContent":["export {\n getTranslation,\n
|
|
1
|
+
{"version":3,"sources":["../../../src/client/index.ts"],"sourcesContent":["export {\n getTranslation,\n IntlayerClientProvider,\n IntlayerClient,\n useIntlayer,\n useTraduction,\n useLocaleCookie,\n} from 'react-intlayer';\nexport { useLocale } from './useLocale';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAOO;AACP,uBAA0B;","names":[]}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { IntlayerClient, IntlayerClientProvider, getTranslation, useIntlayer, useLocaleCookie, useTraduction } from 'react-intlayer';
|
|
2
2
|
export { useLocale } from './useLocale.js';
|
|
3
3
|
import '@intlayer/config/client';
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -18,8 +18,8 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var src_exports = {};
|
|
20
20
|
__export(src_exports, {
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
IntlayerClient: () => import_react_intlayer.IntlayerClient,
|
|
22
|
+
IntlayerClientProvider: () => import_react_intlayer.IntlayerClientProvider,
|
|
23
23
|
generateStaticParams: () => import_generateStaticParams.generateStaticParams,
|
|
24
24
|
getTranslation: () => import_react_intlayer.getTranslation,
|
|
25
25
|
useIntlayer: () => import_react_intlayer.useIntlayer,
|
|
@@ -33,8 +33,8 @@ var import_generateStaticParams = require('./generateStaticParams.cjs');
|
|
|
33
33
|
var import_useLocale = require('./client/useLocale.cjs');
|
|
34
34
|
// Annotate the CommonJS export names for ESM import in node:
|
|
35
35
|
0 && (module.exports = {
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
IntlayerClient,
|
|
37
|
+
IntlayerClientProvider,
|
|
38
38
|
generateStaticParams,
|
|
39
39
|
getTranslation,
|
|
40
40
|
useIntlayer,
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export {\n getTranslation,\n
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export {\n getTranslation,\n IntlayerClientProvider,\n IntlayerClient,\n useIntlayer,\n useTraduction,\n useLocaleCookie,\n} from 'react-intlayer';\nexport { generateStaticParams } from './generateStaticParams';\nexport type { LocalParams, NextPageIntlayer } from './types/index';\nexport { useLocale } from './client/useLocale';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAOO;AACP,kCAAqC;AAErC,uBAA0B;","names":[]}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { IntlayerClient, IntlayerClientProvider, getTranslation, useIntlayer, useLocaleCookie, useTraduction } from 'react-intlayer';
|
|
2
2
|
export { generateStaticParams } from './generateStaticParams.js';
|
|
3
3
|
export { LocalParams, NextPageIntlayer } from './types/NextPage.js';
|
|
4
4
|
export { useLocale } from './client/useLocale.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/middleware/intlayerMiddleware.ts"],"sourcesContent":["import { type Locales, intlayerConfiguration } from '@intlayer/config/client';\nimport { type NextRequest, NextResponse } from 'next/server';\nimport { localeDetector } from './localeDetector';\n\nconst { internationalization, middleware } = intlayerConfiguration;\nconst { locales, defaultLocale } = internationalization;\nconst {\n headerName,\n cookieName,\n prefixDefault,\n basePath,\n serverSetCookie,\n noPrefix,\n} = middleware;\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":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAoD;AACpD,oBAA+C;AAC/C,4BAA+B;AAE/B,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAC7C,MAAM,EAAE,SAAS,cAAc,IAAI;AACnC,MAAM;AAAA,EACJ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,IAAI;
|
|
1
|
+
{"version":3,"sources":["../../../src/middleware/intlayerMiddleware.ts"],"sourcesContent":["import { type Locales, intlayerConfiguration } from '@intlayer/config/client';\nimport { type NextRequest, NextResponse } from 'next/server';\nimport { localeDetector } from './localeDetector';\n\nconst { internationalization, middleware } = intlayerConfiguration;\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 * 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 */\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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","names":["basePath"]}
|
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Middleware that handles the internationalization layer
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
*
|
|
8
|
+
* // ./src/middleware.ts
|
|
9
|
+
*
|
|
10
|
+
* export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';
|
|
11
|
+
*
|
|
12
|
+
* // applies this middleware only to files in the app directory
|
|
13
|
+
* export const config = {
|
|
14
|
+
* matcher: '/((?!api|static|.*\\..*|_next).*)',
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
3
18
|
declare const intlayerMiddleware: (request: NextRequest) => NextResponse;
|
|
4
19
|
|
|
5
20
|
export { intlayerMiddleware };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/middleware/localeDetector.ts"],"sourcesContent":["import { match } from '@formatjs/intl-localematcher';\nimport type { Locales } from '@intlayer/config';\nimport { getConfiguration } from '@intlayer/config/client';\nimport Negotiator from 'negotiator';\nimport type { NextRequest } from 'next/server.js';\n\nconst { locales, defaultLocale } = getConfiguration().internationalization;\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 languages = new Negotiator({ headers: negotiatorHeaders }).languages();\n\n // match can only use specifically formatted locales\n // https://stackoverflow.com/questions/76447732/nextjs-13-i18n-incorrect-locale-information-provided\n try {\n return match(languages, locales, defaultLocale) as Locales;\n } catch (e) {\n console.warn(\n `No valid locales in accept-language header: ${languages.join(', ')}`\n );\n console.warn(`Reverting to using defaultLocale: ${defaultLocale}`);\n\n return defaultLocale;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gCAAsB;AAEtB,oBAAiC;AACjC,wBAAuB;AAGvB,MAAM,EAAE,SAAS,cAAc,QAAI,gCAAiB,EAAE;
|
|
1
|
+
{"version":3,"sources":["../../../src/middleware/localeDetector.ts"],"sourcesContent":["import { match } from '@formatjs/intl-localematcher';\nimport type { Locales } from '@intlayer/config';\nimport { getConfiguration } from '@intlayer/config/client';\nimport Negotiator from 'negotiator';\nimport type { NextRequest } from 'next/server.js';\n\nconst { locales, defaultLocale } = getConfiguration().internationalization;\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 languages = new Negotiator({ headers: negotiatorHeaders }).languages();\n\n // match can only use specifically formatted locales\n // https://stackoverflow.com/questions/76447732/nextjs-13-i18n-incorrect-locale-information-provided\n try {\n return match(languages, locales, defaultLocale) as Locales;\n } catch (e) {\n console.warn(\n `No valid locales in accept-language header: ${languages.join(', ')}`\n );\n console.warn(`Reverting to using defaultLocale: ${defaultLocale}`);\n\n return defaultLocale;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gCAAsB;AAEtB,oBAAiC;AACjC,wBAAuB;AAGvB,MAAM,EAAE,SAAS,cAAc,QAAI,gCAAiB,EAAE;AAO/C,MAAM,iBAAiB,CAAC,YAAkC;AAC/D,QAAM,oBAA4C,CAAC;AAEnD,UAAQ,QAAQ,QAAQ,CAAC,OAAO,QAAS,kBAAkB,GAAG,IAAI,KAAM;AAExE,QAAM,YAAY,IAAI,kBAAAA,QAAW,EAAE,SAAS,kBAAkB,CAAC,EAAE,UAAU;AAI3E,MAAI;AACF,eAAO,iCAAM,WAAW,SAAS,aAAa;AAAA,EAChD,SAAS,GAAG;AACV,YAAQ;AAAA,MACN,+CAA+C,UAAU,KAAK,IAAI,CAAC;AAAA,IACrE;AACA,YAAQ,KAAK,qCAAqC,aAAa,EAAE;AAEjE,WAAO;AAAA,EACT;AACF;","names":["Negotiator"]}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Locales } from '@intlayer/config';
|
|
2
2
|
import { NextRequest } from 'next/server.js';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Detects the locale from the request headers
|
|
6
|
+
*
|
|
7
|
+
* Headers are provided by the browser and can be used to determine the user's preferred language
|
|
8
|
+
*/
|
|
4
9
|
declare const localeDetector: (request: NextRequest) => Locales;
|
|
5
10
|
|
|
6
11
|
export { localeDetector };
|
|
@@ -18,8 +18,8 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var server_exports = {};
|
|
20
20
|
__export(server_exports, {
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
IntlayerServer: () => import_server.IntlayerServer,
|
|
22
|
+
IntlayerServerProvider: () => import_server.IntlayerServerProvider,
|
|
23
23
|
getLocaleContent: () => import_server.getLocaleContent,
|
|
24
24
|
locale: () => import_server.locale,
|
|
25
25
|
useIntlayer: () => import_server.useIntlayer,
|
|
@@ -31,8 +31,8 @@ var import_server = require("react-intlayer/server");
|
|
|
31
31
|
var import_withIntlayer = require('./withIntlayer.cjs');
|
|
32
32
|
// Annotate the CommonJS export names for ESM import in node:
|
|
33
33
|
0 && (module.exports = {
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
IntlayerServer,
|
|
35
|
+
IntlayerServerProvider,
|
|
36
36
|
getLocaleContent,
|
|
37
37
|
locale,
|
|
38
38
|
useIntlayer,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/index.ts"],"sourcesContent":["export {\n getLocaleContent,\n useTraduction,\n
|
|
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,oBAOO;AACP,0BAA6B;","names":[]}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { IntlayerServer, IntlayerServerProvider, getLocaleContent, locale, useIntlayer, useTraduction } from 'react-intlayer/server';
|
|
2
2
|
export { withIntlayer } from './withIntlayer.js';
|
|
3
3
|
import 'next';
|
|
@@ -23,36 +23,33 @@ __export(withIntlayer_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(withIntlayer_exports);
|
|
24
24
|
var import_path = require("path");
|
|
25
25
|
var import_config = require("@intlayer/config");
|
|
26
|
-
|
|
27
|
-
verbose: true
|
|
28
|
-
});
|
|
29
|
-
const env = (0, import_config.formatEnvVariable)("NEXT_PUBLIC_INTLAYER_");
|
|
30
|
-
const { mainDir, baseDir } = intlayerConfig.content;
|
|
31
|
-
const mergeEnvVariable = (nextEnv = {}) => Object.assign({}, nextEnv, env);
|
|
32
|
-
const mergeStats = (nextStats = {}) => Object.assign({}, nextStats, {
|
|
33
|
-
warnings: false
|
|
34
|
-
});
|
|
26
|
+
var import_webpack = require("@intlayer/webpack");
|
|
35
27
|
const withIntlayer = (_pluginOptions = {}) => (nextConfig = {}) => {
|
|
36
28
|
if (typeof nextConfig !== "object")
|
|
37
29
|
nextConfig = {};
|
|
30
|
+
const intlayerConfig = (0, import_config.getConfiguration)();
|
|
31
|
+
const env = (0, import_config.formatEnvVariable)("next");
|
|
32
|
+
const { mainDir, baseDir } = intlayerConfig.content;
|
|
38
33
|
return Object.assign({}, nextConfig, {
|
|
39
|
-
env:
|
|
40
|
-
|
|
41
|
-
webpack: (config) => {
|
|
34
|
+
env: { ...nextConfig.env, ...env },
|
|
35
|
+
webpack: (config, { isServer, nextRuntime }) => {
|
|
42
36
|
const dictionariesPath = (0, import_path.join)(mainDir, "dictionaries.cjs");
|
|
43
37
|
const relativeDictionariesPath = (0, import_path.relative)(baseDir, dictionariesPath);
|
|
38
|
+
config.resolve.alias["@intlayer/dictionaries-entry"] = (0, import_path.resolve)(
|
|
39
|
+
relativeDictionariesPath
|
|
40
|
+
);
|
|
44
41
|
config.externals.push({
|
|
45
42
|
esbuild: "esbuild",
|
|
46
43
|
module: "module",
|
|
47
44
|
fs: "fs"
|
|
48
45
|
});
|
|
49
|
-
config.resolve.alias["@intlayer/dictionaries-entry"] = (0, import_path.resolve)(
|
|
50
|
-
relativeDictionariesPath
|
|
51
|
-
);
|
|
52
46
|
config.module.rules.push({
|
|
53
47
|
test: /\.node$/,
|
|
54
48
|
loader: "node-loader"
|
|
55
49
|
});
|
|
50
|
+
if (isServer && nextRuntime === "nodejs") {
|
|
51
|
+
config.plugins.push(new import_webpack.IntLayerPlugin());
|
|
52
|
+
}
|
|
56
53
|
return config;
|
|
57
54
|
}
|
|
58
55
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/withIntlayer.ts"],"sourcesContent":["import { resolve, relative, join } from 'path';\nimport { getConfiguration, formatEnvVariable } from '@intlayer/config';\nimport type { NextConfig } from 'next';\nimport type { NextJsWebpackConfig } from 'next/dist/server/config-shared';\n\ntype PluginOptions = {\n // TODO: add options\n};\n\
|
|
1
|
+
{"version":3,"sources":["../../../src/server/withIntlayer.ts"],"sourcesContent":["import { resolve, relative, join } from 'path';\nimport { getConfiguration, formatEnvVariable } from '@intlayer/config';\nimport { IntLayerPlugin } from '@intlayer/webpack';\nimport type { NextConfig } from 'next';\nimport type { NextJsWebpackConfig } from 'next/dist/server/config-shared';\n\ntype PluginOptions = {\n // TODO: add options\n};\n\ntype WebpackParams = Parameters<NextJsWebpackConfig>;\n\n/**\n * A Next.js plugin that adds the intlayer configuration to the webpack configuration\n * and sets the environment variables\n *\n * Usage:\n *\n * // next.config.js\n * export default withIntlayer(nextConfig)\n *\n */\nexport const withIntlayer =\n (_pluginOptions: PluginOptions = {}) =>\n (nextConfig: Partial<NextConfig> = {}): Partial<NextConfig> => {\n if (typeof nextConfig !== 'object') nextConfig = {};\n\n const intlayerConfig = getConfiguration();\n\n // Set all configuration values as environment variables\n const env = formatEnvVariable('next');\n\n const { mainDir, baseDir } = intlayerConfig.content;\n\n return Object.assign({}, nextConfig, {\n env: { ...nextConfig.env, ...env },\n\n webpack: (\n config: WebpackParams['0'],\n { isServer, nextRuntime }: WebpackParams[1]\n ) => {\n const dictionariesPath = join(mainDir, 'dictionaries.cjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n config.resolve.alias['@intlayer/dictionaries-entry'] = resolve(\n relativeDictionariesPath\n );\n\n config.externals.push({\n esbuild: 'esbuild',\n module: 'module',\n fs: 'fs',\n });\n config.module.rules.push({\n test: /\\.node$/,\n loader: 'node-loader',\n });\n\n // Apply IntLayerPlugin only on the server-side\n if (isServer && nextRuntime === 'nodejs') {\n config.plugins.push(new IntLayerPlugin());\n }\n\n return config;\n },\n });\n };\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAwC;AACxC,oBAAoD;AACpD,qBAA+B;AAoBxB,MAAM,eACX,CAAC,iBAAgC,CAAC,MAClC,CAAC,aAAkC,CAAC,MAA2B;AAC7D,MAAI,OAAO,eAAe;AAAU,iBAAa,CAAC;AAElD,QAAM,qBAAiB,gCAAiB;AAGxC,QAAM,UAAM,iCAAkB,MAAM;AAEpC,QAAM,EAAE,SAAS,QAAQ,IAAI,eAAe;AAE5C,SAAO,OAAO,OAAO,CAAC,GAAG,YAAY;AAAA,IACnC,KAAK,EAAE,GAAG,WAAW,KAAK,GAAG,IAAI;AAAA,IAEjC,SAAS,CACP,QACA,EAAE,UAAU,YAAY,MACrB;AACH,YAAM,uBAAmB,kBAAK,SAAS,kBAAkB;AACzD,YAAM,+BAA2B,sBAAS,SAAS,gBAAgB;AAEnE,aAAO,QAAQ,MAAM,8BAA8B,QAAI;AAAA,QACrD;AAAA,MACF;AAEA,aAAO,UAAU,KAAK;AAAA,QACpB,SAAS;AAAA,QACT,QAAQ;AAAA,QACR,IAAI;AAAA,MACN,CAAC;AACD,aAAO,OAAO,MAAM,KAAK;AAAA,QACvB,MAAM;AAAA,QACN,QAAQ;AAAA,MACV,CAAC;AAGD,UAAI,YAAY,gBAAgB,UAAU;AACxC,eAAO,QAAQ,KAAK,IAAI,8BAAe,CAAC;AAAA,MAC1C;AAEA,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
|
|
@@ -2,6 +2,16 @@ import 'react-intlayer/server';
|
|
|
2
2
|
import { NextConfig } from 'next';
|
|
3
3
|
|
|
4
4
|
type PluginOptions = {};
|
|
5
|
+
/**
|
|
6
|
+
* A Next.js plugin that adds the intlayer configuration to the webpack configuration
|
|
7
|
+
* and sets the environment variables
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
*
|
|
11
|
+
* // next.config.js
|
|
12
|
+
* export default withIntlayer(nextConfig)
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
5
15
|
declare const withIntlayer: (_pluginOptions?: PluginOptions) => (nextConfig?: Partial<NextConfig>) => Partial<NextConfig>;
|
|
6
16
|
|
|
7
17
|
export { withIntlayer };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { IntlayerClient, IntlayerClientProvider, getTranslation, useIntlayer, useLocaleCookie, useTraduction } from 'react-intlayer';
|
|
2
2
|
export { useLocale } from './useLocale.mjs';
|
|
3
3
|
import '@intlayer/config/client';
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getTranslation,
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
IntlayerClientProvider,
|
|
4
|
+
IntlayerClient,
|
|
5
5
|
useIntlayer,
|
|
6
6
|
useTraduction,
|
|
7
7
|
useLocaleCookie
|
|
8
8
|
} from "react-intlayer";
|
|
9
9
|
import { useLocale } from './useLocale.mjs';
|
|
10
10
|
export {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
IntlayerClient,
|
|
12
|
+
IntlayerClientProvider,
|
|
13
13
|
getTranslation,
|
|
14
14
|
useIntlayer,
|
|
15
15
|
useLocale,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/client/index.ts"],"sourcesContent":["export {\n getTranslation,\n
|
|
1
|
+
{"version":3,"sources":["../../../src/client/index.ts"],"sourcesContent":["export {\n getTranslation,\n IntlayerClientProvider,\n IntlayerClient,\n useIntlayer,\n useTraduction,\n useLocaleCookie,\n} from 'react-intlayer';\nexport { useLocale } from './useLocale';\n"],"mappings":"AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,iBAAiB;","names":[]}
|
package/dist/esm/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { IntlayerClient, IntlayerClientProvider, getTranslation, useIntlayer, useLocaleCookie, useTraduction } from 'react-intlayer';
|
|
2
2
|
export { generateStaticParams } from './generateStaticParams.mjs';
|
|
3
3
|
export { LocalParams, NextPageIntlayer } from './types/NextPage.mjs';
|
|
4
4
|
export { useLocale } from './client/useLocale.mjs';
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getTranslation,
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
IntlayerClientProvider,
|
|
4
|
+
IntlayerClient,
|
|
5
5
|
useIntlayer,
|
|
6
6
|
useTraduction,
|
|
7
7
|
useLocaleCookie
|
|
@@ -9,8 +9,8 @@ import {
|
|
|
9
9
|
import { generateStaticParams } from './generateStaticParams.mjs';
|
|
10
10
|
import { useLocale } from './client/useLocale.mjs';
|
|
11
11
|
export {
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
IntlayerClient,
|
|
13
|
+
IntlayerClientProvider,
|
|
14
14
|
generateStaticParams,
|
|
15
15
|
getTranslation,
|
|
16
16
|
useIntlayer,
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export {\n getTranslation,\n
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export {\n getTranslation,\n IntlayerClientProvider,\n IntlayerClient,\n useIntlayer,\n useTraduction,\n useLocaleCookie,\n} from 'react-intlayer';\nexport { generateStaticParams } from './generateStaticParams';\nexport type { LocalParams, NextPageIntlayer } from './types/index';\nexport { useLocale } from './client/useLocale';\n"],"mappings":"AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,4BAA4B;AAErC,SAAS,iBAAiB;","names":[]}
|
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Middleware that handles the internationalization layer
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
*
|
|
8
|
+
* // ./src/middleware.ts
|
|
9
|
+
*
|
|
10
|
+
* export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';
|
|
11
|
+
*
|
|
12
|
+
* // applies this middleware only to files in the app directory
|
|
13
|
+
* export const config = {
|
|
14
|
+
* matcher: '/((?!api|static|.*\\..*|_next).*)',
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
3
18
|
declare const intlayerMiddleware: (request: NextRequest) => NextResponse;
|
|
4
19
|
|
|
5
20
|
export { intlayerMiddleware };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/middleware/intlayerMiddleware.ts"],"sourcesContent":["import { type Locales, intlayerConfiguration } from '@intlayer/config/client';\nimport { type NextRequest, NextResponse } from 'next/server';\nimport { localeDetector } from './localeDetector';\n\nconst { internationalization, middleware } = intlayerConfiguration;\nconst { locales, defaultLocale } = internationalization;\nconst {\n headerName,\n cookieName,\n prefixDefault,\n basePath,\n serverSetCookie,\n noPrefix,\n} = middleware;\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":"AAAA,SAAuB,6BAA6B;AACpD,SAA2B,oBAAoB;AAC/C,SAAS,sBAAsB;AAE/B,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAC7C,MAAM,EAAE,SAAS,cAAc,IAAI;AACnC,MAAM;AAAA,EACJ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,IAAI;
|
|
1
|
+
{"version":3,"sources":["../../../src/middleware/intlayerMiddleware.ts"],"sourcesContent":["import { type Locales, intlayerConfiguration } from '@intlayer/config/client';\nimport { type NextRequest, NextResponse } from 'next/server';\nimport { localeDetector } from './localeDetector';\n\nconst { internationalization, middleware } = intlayerConfiguration;\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 * 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 */\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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","names":["basePath"]}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Locales } from '@intlayer/config';
|
|
2
2
|
import { NextRequest } from 'next/server.js';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Detects the locale from the request headers
|
|
6
|
+
*
|
|
7
|
+
* Headers are provided by the browser and can be used to determine the user's preferred language
|
|
8
|
+
*/
|
|
4
9
|
declare const localeDetector: (request: NextRequest) => Locales;
|
|
5
10
|
|
|
6
11
|
export { localeDetector };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/middleware/localeDetector.ts"],"sourcesContent":["import { match } from '@formatjs/intl-localematcher';\nimport type { Locales } from '@intlayer/config';\nimport { getConfiguration } from '@intlayer/config/client';\nimport Negotiator from 'negotiator';\nimport type { NextRequest } from 'next/server.js';\n\nconst { locales, defaultLocale } = getConfiguration().internationalization;\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 languages = new Negotiator({ headers: negotiatorHeaders }).languages();\n\n // match can only use specifically formatted locales\n // https://stackoverflow.com/questions/76447732/nextjs-13-i18n-incorrect-locale-information-provided\n try {\n return match(languages, locales, defaultLocale) as Locales;\n } catch (e) {\n console.warn(\n `No valid locales in accept-language header: ${languages.join(', ')}`\n );\n console.warn(`Reverting to using defaultLocale: ${defaultLocale}`);\n\n return defaultLocale;\n }\n};\n"],"mappings":"AAAA,SAAS,aAAa;AAEtB,SAAS,wBAAwB;AACjC,OAAO,gBAAgB;AAGvB,MAAM,EAAE,SAAS,cAAc,IAAI,iBAAiB,EAAE;
|
|
1
|
+
{"version":3,"sources":["../../../src/middleware/localeDetector.ts"],"sourcesContent":["import { match } from '@formatjs/intl-localematcher';\nimport type { Locales } from '@intlayer/config';\nimport { getConfiguration } from '@intlayer/config/client';\nimport Negotiator from 'negotiator';\nimport type { NextRequest } from 'next/server.js';\n\nconst { locales, defaultLocale } = getConfiguration().internationalization;\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 languages = new Negotiator({ headers: negotiatorHeaders }).languages();\n\n // match can only use specifically formatted locales\n // https://stackoverflow.com/questions/76447732/nextjs-13-i18n-incorrect-locale-information-provided\n try {\n return match(languages, locales, defaultLocale) as Locales;\n } catch (e) {\n console.warn(\n `No valid locales in accept-language header: ${languages.join(', ')}`\n );\n console.warn(`Reverting to using defaultLocale: ${defaultLocale}`);\n\n return defaultLocale;\n }\n};\n"],"mappings":"AAAA,SAAS,aAAa;AAEtB,SAAS,wBAAwB;AACjC,OAAO,gBAAgB;AAGvB,MAAM,EAAE,SAAS,cAAc,IAAI,iBAAiB,EAAE;AAO/C,MAAM,iBAAiB,CAAC,YAAkC;AAC/D,QAAM,oBAA4C,CAAC;AAEnD,UAAQ,QAAQ,QAAQ,CAAC,OAAO,QAAS,kBAAkB,GAAG,IAAI,KAAM;AAExE,QAAM,YAAY,IAAI,WAAW,EAAE,SAAS,kBAAkB,CAAC,EAAE,UAAU;AAI3E,MAAI;AACF,WAAO,MAAM,WAAW,SAAS,aAAa;AAAA,EAChD,SAAS,GAAG;AACV,YAAQ;AAAA,MACN,+CAA+C,UAAU,KAAK,IAAI,CAAC;AAAA,IACrE;AACA,YAAQ,KAAK,qCAAqC,aAAa,EAAE;AAEjE,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { IntlayerServer, IntlayerServerProvider, getLocaleContent, locale, useIntlayer, useTraduction } from 'react-intlayer/server';
|
|
2
2
|
export { withIntlayer } from './withIntlayer.mjs';
|
|
3
3
|
import 'next';
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getLocaleContent,
|
|
3
3
|
useTraduction,
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
IntlayerServer,
|
|
5
|
+
IntlayerServerProvider,
|
|
6
6
|
locale,
|
|
7
7
|
useIntlayer
|
|
8
8
|
} from "react-intlayer/server";
|
|
9
9
|
import { withIntlayer } from './withIntlayer.mjs';
|
|
10
10
|
export {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
IntlayerServer,
|
|
12
|
+
IntlayerServerProvider,
|
|
13
13
|
getLocaleContent,
|
|
14
14
|
locale,
|
|
15
15
|
useIntlayer,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/index.ts"],"sourcesContent":["export {\n getLocaleContent,\n useTraduction,\n
|
|
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,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,oBAAoB;","names":[]}
|
|
@@ -2,6 +2,16 @@ import 'react-intlayer/server';
|
|
|
2
2
|
import { NextConfig } from 'next';
|
|
3
3
|
|
|
4
4
|
type PluginOptions = {};
|
|
5
|
+
/**
|
|
6
|
+
* A Next.js plugin that adds the intlayer configuration to the webpack configuration
|
|
7
|
+
* and sets the environment variables
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
*
|
|
11
|
+
* // next.config.js
|
|
12
|
+
* export default withIntlayer(nextConfig)
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
5
15
|
declare const withIntlayer: (_pluginOptions?: PluginOptions) => (nextConfig?: Partial<NextConfig>) => Partial<NextConfig>;
|
|
6
16
|
|
|
7
17
|
export { withIntlayer };
|
|
@@ -1,35 +1,32 @@
|
|
|
1
1
|
import { resolve, relative, join } from "path";
|
|
2
2
|
import { getConfiguration, formatEnvVariable } from "@intlayer/config";
|
|
3
|
-
|
|
4
|
-
verbose: true
|
|
5
|
-
});
|
|
6
|
-
const env = formatEnvVariable("NEXT_PUBLIC_INTLAYER_");
|
|
7
|
-
const { mainDir, baseDir } = intlayerConfig.content;
|
|
8
|
-
const mergeEnvVariable = (nextEnv = {}) => Object.assign({}, nextEnv, env);
|
|
9
|
-
const mergeStats = (nextStats = {}) => Object.assign({}, nextStats, {
|
|
10
|
-
warnings: false
|
|
11
|
-
});
|
|
3
|
+
import { IntLayerPlugin } from "@intlayer/webpack";
|
|
12
4
|
const withIntlayer = (_pluginOptions = {}) => (nextConfig = {}) => {
|
|
13
5
|
if (typeof nextConfig !== "object")
|
|
14
6
|
nextConfig = {};
|
|
7
|
+
const intlayerConfig = getConfiguration();
|
|
8
|
+
const env = formatEnvVariable("next");
|
|
9
|
+
const { mainDir, baseDir } = intlayerConfig.content;
|
|
15
10
|
return Object.assign({}, nextConfig, {
|
|
16
|
-
env:
|
|
17
|
-
|
|
18
|
-
webpack: (config) => {
|
|
11
|
+
env: { ...nextConfig.env, ...env },
|
|
12
|
+
webpack: (config, { isServer, nextRuntime }) => {
|
|
19
13
|
const dictionariesPath = join(mainDir, "dictionaries.cjs");
|
|
20
14
|
const relativeDictionariesPath = relative(baseDir, dictionariesPath);
|
|
15
|
+
config.resolve.alias["@intlayer/dictionaries-entry"] = resolve(
|
|
16
|
+
relativeDictionariesPath
|
|
17
|
+
);
|
|
21
18
|
config.externals.push({
|
|
22
19
|
esbuild: "esbuild",
|
|
23
20
|
module: "module",
|
|
24
21
|
fs: "fs"
|
|
25
22
|
});
|
|
26
|
-
config.resolve.alias["@intlayer/dictionaries-entry"] = resolve(
|
|
27
|
-
relativeDictionariesPath
|
|
28
|
-
);
|
|
29
23
|
config.module.rules.push({
|
|
30
24
|
test: /\.node$/,
|
|
31
25
|
loader: "node-loader"
|
|
32
26
|
});
|
|
27
|
+
if (isServer && nextRuntime === "nodejs") {
|
|
28
|
+
config.plugins.push(new IntLayerPlugin());
|
|
29
|
+
}
|
|
33
30
|
return config;
|
|
34
31
|
}
|
|
35
32
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/withIntlayer.ts"],"sourcesContent":["import { resolve, relative, join } from 'path';\nimport { getConfiguration, formatEnvVariable } from '@intlayer/config';\nimport type { NextConfig } from 'next';\nimport type { NextJsWebpackConfig } from 'next/dist/server/config-shared';\n\ntype PluginOptions = {\n // TODO: add options\n};\n\
|
|
1
|
+
{"version":3,"sources":["../../../src/server/withIntlayer.ts"],"sourcesContent":["import { resolve, relative, join } from 'path';\nimport { getConfiguration, formatEnvVariable } from '@intlayer/config';\nimport { IntLayerPlugin } from '@intlayer/webpack';\nimport type { NextConfig } from 'next';\nimport type { NextJsWebpackConfig } from 'next/dist/server/config-shared';\n\ntype PluginOptions = {\n // TODO: add options\n};\n\ntype WebpackParams = Parameters<NextJsWebpackConfig>;\n\n/**\n * A Next.js plugin that adds the intlayer configuration to the webpack configuration\n * and sets the environment variables\n *\n * Usage:\n *\n * // next.config.js\n * export default withIntlayer(nextConfig)\n *\n */\nexport const withIntlayer =\n (_pluginOptions: PluginOptions = {}) =>\n (nextConfig: Partial<NextConfig> = {}): Partial<NextConfig> => {\n if (typeof nextConfig !== 'object') nextConfig = {};\n\n const intlayerConfig = getConfiguration();\n\n // Set all configuration values as environment variables\n const env = formatEnvVariable('next');\n\n const { mainDir, baseDir } = intlayerConfig.content;\n\n return Object.assign({}, nextConfig, {\n env: { ...nextConfig.env, ...env },\n\n webpack: (\n config: WebpackParams['0'],\n { isServer, nextRuntime }: WebpackParams[1]\n ) => {\n const dictionariesPath = join(mainDir, 'dictionaries.cjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n config.resolve.alias['@intlayer/dictionaries-entry'] = resolve(\n relativeDictionariesPath\n );\n\n config.externals.push({\n esbuild: 'esbuild',\n module: 'module',\n fs: 'fs',\n });\n config.module.rules.push({\n test: /\\.node$/,\n loader: 'node-loader',\n });\n\n // Apply IntLayerPlugin only on the server-side\n if (isServer && nextRuntime === 'nodejs') {\n config.plugins.push(new IntLayerPlugin());\n }\n\n return config;\n },\n });\n };\n"],"mappings":"AAAA,SAAS,SAAS,UAAU,YAAY;AACxC,SAAS,kBAAkB,yBAAyB;AACpD,SAAS,sBAAsB;AAoBxB,MAAM,eACX,CAAC,iBAAgC,CAAC,MAClC,CAAC,aAAkC,CAAC,MAA2B;AAC7D,MAAI,OAAO,eAAe;AAAU,iBAAa,CAAC;AAElD,QAAM,iBAAiB,iBAAiB;AAGxC,QAAM,MAAM,kBAAkB,MAAM;AAEpC,QAAM,EAAE,SAAS,QAAQ,IAAI,eAAe;AAE5C,SAAO,OAAO,OAAO,CAAC,GAAG,YAAY;AAAA,IACnC,KAAK,EAAE,GAAG,WAAW,KAAK,GAAG,IAAI;AAAA,IAEjC,SAAS,CACP,QACA,EAAE,UAAU,YAAY,MACrB;AACH,YAAM,mBAAmB,KAAK,SAAS,kBAAkB;AACzD,YAAM,2BAA2B,SAAS,SAAS,gBAAgB;AAEnE,aAAO,QAAQ,MAAM,8BAA8B,IAAI;AAAA,QACrD;AAAA,MACF;AAEA,aAAO,UAAU,KAAK;AAAA,QACpB,SAAS;AAAA,QACT,QAAQ;AAAA,QACR,IAAI;AAAA,MACN,CAAC;AACD,aAAO,OAAO,MAAM,KAAK;AAAA,QACvB,MAAM;AAAA,QACN,QAAQ;AAAA,MACV,CAAC;AAGD,UAAI,YAAY,gBAAgB,UAAU;AACxC,eAAO,QAAQ,KAAK,IAAI,eAAe,CAAC;AAAA,MAC1C;AAEA,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-intlayer",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Webpack configuration for IntLayer using NextJS",
|
|
6
6
|
"keywords": [
|
|
@@ -66,20 +66,22 @@
|
|
|
66
66
|
"negotiator": "^0.6.3",
|
|
67
67
|
"next": "14.1.4",
|
|
68
68
|
"webpack": "^5.91.0",
|
|
69
|
-
"@intlayer/
|
|
70
|
-
"@intlayer/
|
|
71
|
-
"@intlayer/
|
|
72
|
-
"intlayer": "^1.0
|
|
73
|
-
"
|
|
69
|
+
"@intlayer/chokidar": "^1.2.0",
|
|
70
|
+
"@intlayer/config": "^1.2.0",
|
|
71
|
+
"@intlayer/core": "^1.2.0",
|
|
72
|
+
"@intlayer/dictionaries-entry": "^1.2.0",
|
|
73
|
+
"@intlayer/webpack": "^1.2.0",
|
|
74
|
+
"intlayer": "^1.2.0",
|
|
75
|
+
"react-intlayer": "^1.2.0"
|
|
74
76
|
},
|
|
75
77
|
"devDependencies": {
|
|
76
78
|
"@types/negotiator": "^0.6.3",
|
|
77
|
-
"@types/node": "^20.
|
|
78
|
-
"@types/react": "^18.2.
|
|
79
|
+
"@types/node": "^20.12.7",
|
|
80
|
+
"@types/react": "^18.2.79",
|
|
79
81
|
"react": "^18.2.0",
|
|
80
82
|
"rimraf": "5.0.5",
|
|
81
83
|
"tsup": "^8.0.2",
|
|
82
|
-
"typescript": "^5.4.
|
|
84
|
+
"typescript": "^5.4.5",
|
|
83
85
|
"@utils/eslint-config": "^1.0.1",
|
|
84
86
|
"@utils/ts-config": "^1.0.1"
|
|
85
87
|
},
|
package/src/client/index.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -13,6 +13,21 @@ const {
|
|
|
13
13
|
noPrefix,
|
|
14
14
|
} = middleware;
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Middleware that handles the internationalization layer
|
|
18
|
+
*
|
|
19
|
+
* Usage:
|
|
20
|
+
*
|
|
21
|
+
* // ./src/middleware.ts
|
|
22
|
+
*
|
|
23
|
+
* export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';
|
|
24
|
+
*
|
|
25
|
+
* // applies this middleware only to files in the app directory
|
|
26
|
+
* export const config = {
|
|
27
|
+
* matcher: '/((?!api|static|.*\\..*|_next).*)',
|
|
28
|
+
* };
|
|
29
|
+
*
|
|
30
|
+
*/
|
|
16
31
|
export const intlayerMiddleware = (request: NextRequest): NextResponse => {
|
|
17
32
|
const pathname = request.nextUrl.pathname;
|
|
18
33
|
const cookieLocale = getCookieLocale(request);
|
|
@@ -6,6 +6,11 @@ import type { NextRequest } from 'next/server.js';
|
|
|
6
6
|
|
|
7
7
|
const { locales, defaultLocale } = getConfiguration().internationalization;
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Detects the locale from the request headers
|
|
11
|
+
*
|
|
12
|
+
* Headers are provided by the browser and can be used to determine the user's preferred language
|
|
13
|
+
*/
|
|
9
14
|
export const localeDetector = (request: NextRequest): Locales => {
|
|
10
15
|
const negotiatorHeaders: Record<string, string> = {};
|
|
11
16
|
|
package/src/server/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { resolve, relative, join } from 'path';
|
|
2
2
|
import { getConfiguration, formatEnvVariable } from '@intlayer/config';
|
|
3
|
+
import { IntLayerPlugin } from '@intlayer/webpack';
|
|
3
4
|
import type { NextConfig } from 'next';
|
|
4
5
|
import type { NextJsWebpackConfig } from 'next/dist/server/config-shared';
|
|
5
6
|
|
|
@@ -7,54 +8,59 @@ type PluginOptions = {
|
|
|
7
8
|
// TODO: add options
|
|
8
9
|
};
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
verbose: true,
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
// Set all configuration values as environment variables
|
|
15
|
-
const env = formatEnvVariable('NEXT_PUBLIC_INTLAYER_');
|
|
16
|
-
const { mainDir, baseDir } = intlayerConfig.content;
|
|
17
|
-
|
|
18
|
-
const mergeEnvVariable = (
|
|
19
|
-
nextEnv: Record<string, unknown> | undefined = {}
|
|
20
|
-
): Record<string, string> => Object.assign({}, nextEnv, env);
|
|
21
|
-
|
|
22
|
-
const mergeStats = (
|
|
23
|
-
nextStats: Record<string, unknown> | undefined = {}
|
|
24
|
-
): Record<string, unknown> =>
|
|
25
|
-
Object.assign({}, nextStats, {
|
|
26
|
-
warnings: false,
|
|
27
|
-
});
|
|
11
|
+
type WebpackParams = Parameters<NextJsWebpackConfig>;
|
|
28
12
|
|
|
13
|
+
/**
|
|
14
|
+
* A Next.js plugin that adds the intlayer configuration to the webpack configuration
|
|
15
|
+
* and sets the environment variables
|
|
16
|
+
*
|
|
17
|
+
* Usage:
|
|
18
|
+
*
|
|
19
|
+
* // next.config.js
|
|
20
|
+
* export default withIntlayer(nextConfig)
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
29
23
|
export const withIntlayer =
|
|
30
24
|
(_pluginOptions: PluginOptions = {}) =>
|
|
31
25
|
(nextConfig: Partial<NextConfig> = {}): Partial<NextConfig> => {
|
|
32
26
|
if (typeof nextConfig !== 'object') nextConfig = {};
|
|
33
27
|
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
const intlayerConfig = getConfiguration();
|
|
29
|
+
|
|
30
|
+
// Set all configuration values as environment variables
|
|
31
|
+
const env = formatEnvVariable('next');
|
|
36
32
|
|
|
37
|
-
|
|
33
|
+
const { mainDir, baseDir } = intlayerConfig.content;
|
|
38
34
|
|
|
39
|
-
|
|
35
|
+
return Object.assign({}, nextConfig, {
|
|
36
|
+
env: { ...nextConfig.env, ...env },
|
|
37
|
+
|
|
38
|
+
webpack: (
|
|
39
|
+
config: WebpackParams['0'],
|
|
40
|
+
{ isServer, nextRuntime }: WebpackParams[1]
|
|
41
|
+
) => {
|
|
40
42
|
const dictionariesPath = join(mainDir, 'dictionaries.cjs');
|
|
41
43
|
const relativeDictionariesPath = relative(baseDir, dictionariesPath);
|
|
42
44
|
|
|
45
|
+
config.resolve.alias['@intlayer/dictionaries-entry'] = resolve(
|
|
46
|
+
relativeDictionariesPath
|
|
47
|
+
);
|
|
48
|
+
|
|
43
49
|
config.externals.push({
|
|
44
50
|
esbuild: 'esbuild',
|
|
45
51
|
module: 'module',
|
|
46
52
|
fs: 'fs',
|
|
47
53
|
});
|
|
48
|
-
|
|
49
|
-
config.resolve.alias['@intlayer/dictionaries-entry'] = resolve(
|
|
50
|
-
relativeDictionariesPath
|
|
51
|
-
);
|
|
52
|
-
|
|
53
54
|
config.module.rules.push({
|
|
54
55
|
test: /\.node$/,
|
|
55
56
|
loader: 'node-loader',
|
|
56
57
|
});
|
|
57
58
|
|
|
59
|
+
// Apply IntLayerPlugin only on the server-side
|
|
60
|
+
if (isServer && nextRuntime === 'nodejs') {
|
|
61
|
+
config.plugins.push(new IntLayerPlugin());
|
|
62
|
+
}
|
|
63
|
+
|
|
58
64
|
return config;
|
|
59
65
|
},
|
|
60
66
|
});
|