next-intlayer 1.2.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +38 -6
  2. package/dist/cjs/client/IntlayerClientProvider.cjs +36 -0
  3. package/dist/cjs/client/IntlayerClientProvider.cjs.map +1 -0
  4. package/dist/cjs/client/IntlayerClientProvider.d.ts +7 -0
  5. package/dist/cjs/client/index.cjs +4 -3
  6. package/dist/cjs/client/index.cjs.map +1 -1
  7. package/dist/cjs/client/index.d.ts +3 -1
  8. package/dist/cjs/client/useLocale.cjs +0 -2
  9. package/dist/cjs/client/useLocale.cjs.map +1 -1
  10. package/dist/cjs/client/useLocale.d.ts +0 -1
  11. package/dist/cjs/index.cjs +5 -5
  12. package/dist/cjs/index.cjs.map +1 -1
  13. package/dist/cjs/index.d.ts +2 -1
  14. package/dist/cjs/server/withIntlayer.cjs +25 -4
  15. package/dist/cjs/server/withIntlayer.cjs.map +1 -1
  16. package/dist/cjs/server/withIntlayer.d.ts +1 -2
  17. package/dist/esm/client/IntlayerClientProvider.d.mts +7 -0
  18. package/dist/esm/client/IntlayerClientProvider.mjs +12 -0
  19. package/dist/esm/client/IntlayerClientProvider.mjs.map +1 -0
  20. package/dist/esm/client/index.d.mts +3 -1
  21. package/dist/esm/client/index.mjs +5 -3
  22. package/dist/esm/client/index.mjs.map +1 -1
  23. package/dist/esm/client/useLocale.d.mts +0 -1
  24. package/dist/esm/client/useLocale.mjs +0 -2
  25. package/dist/esm/client/useLocale.mjs.map +1 -1
  26. package/dist/esm/index.d.mts +2 -1
  27. package/dist/esm/index.mjs +6 -4
  28. package/dist/esm/index.mjs.map +1 -1
  29. package/dist/esm/server/withIntlayer.d.mts +1 -2
  30. package/dist/esm/server/withIntlayer.mjs +25 -4
  31. package/dist/esm/server/withIntlayer.mjs.map +1 -1
  32. package/package.json +8 -10
  33. package/src/index.ts +6 -3
  34. package/src/client/index.ts +0 -9
  35. package/src/client/useLocale.ts +0 -44
  36. package/src/generateStaticParams.ts +0 -5
  37. package/src/middleware/index.ts +0 -1
  38. package/src/middleware/intlayerMiddleware.ts +0 -232
  39. package/src/middleware/localeDetector.ts +0 -33
  40. package/src/server/index.ts +0 -9
  41. package/src/server/withIntlayer.ts +0 -69
  42. package/src/types/NextPage.ts +0 -10
  43. package/src/types/index.ts +0 -1
package/README.md CHANGED
@@ -63,15 +63,47 @@ Implement dynamic routing for localized content:
63
63
 
64
64
  Change `src/app/page.ts` to `src/app/[locale]/page.ts`
65
65
 
66
+ Then, implement the generateStaticParams function in your application Layout.
67
+
68
+ ```tsx
69
+ // src/app/layout.tsx
70
+
71
+ import type { Metadata } from "next";
72
+ import { Inter } from "next/font/google";
73
+ import type { ReactNode } from "react";
74
+ import "./globals.css";
75
+
76
+ const inter = Inter({ subsets: ["latin"] });
77
+
78
+ export { generateStaticParams } from "next-intlayer"; // Line to insert
79
+
80
+ export const metadata: Metadata = {
81
+ title: "Create Next App",
82
+ description: "Generated by create next app",
83
+ };
84
+
85
+ const RootLayout = ({
86
+ children,
87
+ }: Readonly<{
88
+ children: ReactNode;
89
+ }>) => (
90
+ <html lang="en">
91
+ <body className={inter.className}>{children}</body>
92
+ </html>
93
+ );
94
+
95
+ export default RootLayout;
96
+ ```
97
+
66
98
  ### Step 5: Manage Your Content
67
99
 
68
100
  Create and manage your content dictionaries:
69
101
 
70
102
  ```tsx
71
103
  // src/app/[locale]/page.content.ts
72
- import { t, type ContentModule } from "intlayer";
104
+ import { t, type DeclarationContent } from "intlayer";
73
105
 
74
- const pageContent: ContentModule = {
106
+ const pageContent: DeclarationContent = {
75
107
  id: "page",
76
108
  getStarted: {
77
109
  main: t({
@@ -99,7 +131,7 @@ import { ClientComponentExample } from "@component/components/ClientComponentExa
99
131
  import { LocaleSwitcher } from "@component/components/LangSwitcherDropDown";
100
132
  import { NestedServerComponentExample } from "@component/components/NestedServerComponentExample";
101
133
  import { ServerComponentExample } from "@component/components/ServerComponentExample";
102
- import { type NextPageIntlayer, IntlayerClientProvider } from "next-intlayer";
134
+ import { type NextPageIntlayer, IntlayerProvider } from "next-intlayer";
103
135
  import { IntlayerServerProvider, useIntlayer } from "next-intlayer/server";
104
136
 
105
137
  const Page: NextPageIntlayer = ({ params: { locale } }) => {
@@ -119,12 +151,12 @@ const Page: NextPageIntlayer = ({ params: { locale } }) => {
119
151
  <ServerComponentExample />
120
152
  </IntlayerServerProvider>
121
153
  {/**
122
- * IntlayerClientProvider is used to provide the locale to the client children
154
+ * IntlayerProvider is used to provide the locale to the client children
123
155
  * Can be set in any parent component, including the layout
124
156
  */}
125
- <IntlayerClientProvider locale={locale}>
157
+ <IntlayerProvider locale={locale}>
126
158
  <ClientComponentExample />
127
- </IntlayerClientProvider>
159
+ </IntlayerProvider>
128
160
  </>
129
161
  );
130
162
  };
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ "use client";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var IntlayerClientProvider_exports = {};
21
+ __export(IntlayerClientProvider_exports, {
22
+ IntlayerClientProvider: () => IntlayerClientProvider
23
+ });
24
+ module.exports = __toCommonJS(IntlayerClientProvider_exports);
25
+ var import_jsx_runtime = require("react/jsx-runtime");
26
+ var import_react_intlayer = require("react-intlayer");
27
+ var import_useLocale = require('./useLocale.cjs');
28
+ const IntlayerClientProvider = (props) => {
29
+ const { setLocale } = (0, import_useLocale.useLocale)();
30
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_intlayer.IntlayerProvider, { setLocale, ...props });
31
+ };
32
+ // Annotate the CommonJS export names for ESM import in node:
33
+ 0 && (module.exports = {
34
+ IntlayerClientProvider
35
+ });
36
+ //# sourceMappingURL=IntlayerClientProvider.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/client/IntlayerClientProvider.tsx"],"sourcesContent":["'use client';\n\nimport type { FC } from 'react';\nimport { IntlayerProvider, type IntlayerProviderProps } from 'react-intlayer';\nimport { useLocale } from './useLocale';\n\nexport type IntlayerClientProviderProps = IntlayerProviderProps;\n\nexport const IntlayerClientProvider: FC<IntlayerProviderProps> = (props) => {\n const { setLocale } = useLocale();\n return <IntlayerProvider setLocale={setLocale} {...props} />;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUS;AAPT,4BAA6D;AAC7D,uBAA0B;AAInB,MAAM,yBAAoD,CAAC,UAAU;AAC1E,QAAM,EAAE,UAAU,QAAI,4BAAU;AAChC,SAAO,4CAAC,0CAAiB,WAAuB,GAAG,OAAO;AAC5D;","names":[]}
@@ -0,0 +1,7 @@
1
+ import { FC } from 'react';
2
+ import { IntlayerProviderProps } from 'react-intlayer';
3
+
4
+ type IntlayerClientProviderProps = IntlayerProviderProps;
5
+ declare const IntlayerClientProvider: FC<IntlayerProviderProps>;
6
+
7
+ export { IntlayerClientProvider, type IntlayerClientProviderProps };
@@ -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
- IntlayerClient: () => import_react_intlayer.IntlayerClient,
22
- IntlayerClientProvider: () => import_react_intlayer.IntlayerClientProvider,
21
+ IntlayerClientContext: () => import_react_intlayer.IntlayerClientContext,
22
+ IntlayerClientProvider: () => import_IntlayerClientProvider.IntlayerClientProvider,
23
23
  getTranslation: () => import_react_intlayer.getTranslation,
24
24
  useIntlayer: () => import_react_intlayer.useIntlayer,
25
25
  useLocale: () => import_useLocale.useLocale,
@@ -29,9 +29,10 @@ __export(client_exports, {
29
29
  module.exports = __toCommonJS(client_exports);
30
30
  var import_react_intlayer = require("react-intlayer");
31
31
  var import_useLocale = require('./useLocale.cjs');
32
+ var import_IntlayerClientProvider = require('./IntlayerClientProvider.cjs');
32
33
  // Annotate the CommonJS export names for ESM import in node:
33
34
  0 && (module.exports = {
34
- IntlayerClient,
35
+ IntlayerClientContext,
35
36
  IntlayerClientProvider,
36
37
  getTranslation,
37
38
  useIntlayer,
@@ -1 +1 @@
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
+ {"version":3,"sources":["../../../src/client/index.ts"],"sourcesContent":["export {\n getTranslation,\n IntlayerClientContext,\n useIntlayer,\n useTraduction,\n useLocaleCookie,\n} from 'react-intlayer';\nexport { useLocale } from './useLocale';\nexport {\n IntlayerClientProvider,\n type IntlayerClientProviderProps,\n} from './IntlayerClientProvider';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAMO;AACP,uBAA0B;AAC1B,oCAGO;","names":[]}
@@ -1,3 +1,5 @@
1
- export { IntlayerClient, IntlayerClientProvider, getTranslation, useIntlayer, useLocaleCookie, useTraduction } from 'react-intlayer';
1
+ export { IntlayerClientContext, getTranslation, useIntlayer, useLocaleCookie, useTraduction } from 'react-intlayer';
2
2
  export { useLocale } from './useLocale.js';
3
+ export { IntlayerClientProvider, IntlayerClientProviderProps } from './IntlayerClientProvider.js';
3
4
  import '@intlayer/config/client';
5
+ import 'react';
@@ -36,8 +36,6 @@ const useLocale = () => {
36
36
  locale: currentLocale
37
37
  } = reactLocaleHook;
38
38
  const setLocale = (locale) => {
39
- if (currentLocale.toString() === locale.toString())
40
- return;
41
39
  if (!availableLocales.includes(locale)) {
42
40
  console.error(`Locale ${locale} is not available`);
43
41
  return;
@@ -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 router = useRouter();\n const pathname = usePathname();\n\n const {\n defaultLocale,\n availableLocales,\n locale: currentLocale,\n } = reactLocaleHook;\n\n const setLocale = (locale: Locales) => {\n if (currentLocale.toString() === locale.toString()) return;\n\n if (!availableLocales.includes(locale)) {\n console.error(`Locale ${locale} is not available`);\n return;\n }\n\n setLocaleCookie(locale);\n\n const pathWithoutLocale =\n !prefixDefault && currentLocale.toString() === defaultLocale.toString()\n ? pathname\n : pathname.slice(`/${currentLocale}`.length) || '/';\n\n if (!prefixDefault && locale === defaultLocale) {\n return router.push(pathWithoutLocale);\n }\n\n return router.push(`/${locale}${pathWithoutLocale}`);\n };\n\n return {\n ...reactLocaleHook,\n setLocale,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAC/C,wBAAuC;AACvC,4BAA+C;AAExC,MAAM,YAAY,MAAM;AAC7B,QAAM,EAAE,cAAc,QAAI,gCAAiB,EAAE;AAC7C,QAAM,EAAE,gBAAgB,QAAI,uCAAgB;AAC5C,QAAM,sBAAkB,qCAAc;AACtC,QAAM,aAAS,6BAAU;AACzB,QAAM,eAAW,+BAAY;AAE7B,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,EACV,IAAI;AAEJ,QAAM,YAAY,CAAC,WAAoB;AACrC,QAAI,cAAc,SAAS,MAAM,OAAO,SAAS;AAAG;AAEpD,QAAI,CAAC,iBAAiB,SAAS,MAAM,GAAG;AACtC,cAAQ,MAAM,UAAU,MAAM,mBAAmB;AACjD;AAAA,IACF;AAEA,oBAAgB,MAAM;AAEtB,UAAM,oBACJ,CAAC,iBAAiB,cAAc,SAAS,MAAM,cAAc,SAAS,IAClE,WACA,SAAS,MAAM,IAAI,aAAa,GAAG,MAAM,KAAK;AAEpD,QAAI,CAAC,iBAAiB,WAAW,eAAe;AAC9C,aAAO,OAAO,KAAK,iBAAiB;AAAA,IACtC;AAEA,WAAO,OAAO,KAAK,IAAI,MAAM,GAAG,iBAAiB,EAAE;AAAA,EACrD;AAEA,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EACF;AACF;","names":[]}
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 router = useRouter();\n const pathname = usePathname();\n\n const {\n defaultLocale,\n availableLocales,\n locale: currentLocale,\n } = reactLocaleHook;\n\n const setLocale = (locale: Locales) => {\n if (!availableLocales.includes(locale)) {\n console.error(`Locale ${locale} is not available`);\n return;\n }\n\n setLocaleCookie(locale);\n\n const pathWithoutLocale =\n !prefixDefault && currentLocale.toString() === defaultLocale.toString()\n ? pathname\n : pathname.slice(`/${currentLocale}`.length) || '/';\n\n if (!prefixDefault && locale === defaultLocale) {\n return router.push(pathWithoutLocale);\n }\n\n return router.push(`/${locale}${pathWithoutLocale}`);\n };\n\n return {\n ...reactLocaleHook,\n setLocale,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAC/C,wBAAuC;AACvC,4BAA+C;AAExC,MAAM,YAAY,MAAM;AAC7B,QAAM,EAAE,cAAc,QAAI,gCAAiB,EAAE;AAC7C,QAAM,EAAE,gBAAgB,QAAI,uCAAgB;AAC5C,QAAM,sBAAkB,qCAAc;AACtC,QAAM,aAAS,6BAAU;AACzB,QAAM,eAAW,+BAAY;AAE7B,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,EACV,IAAI;AAEJ,QAAM,YAAY,CAAC,WAAoB;AACrC,QAAI,CAAC,iBAAiB,SAAS,MAAM,GAAG;AACtC,cAAQ,MAAM,UAAU,MAAM,mBAAmB;AACjD;AAAA,IACF;AAEA,oBAAgB,MAAM;AAEtB,UAAM,oBACJ,CAAC,iBAAiB,cAAc,SAAS,MAAM,cAAc,SAAS,IAClE,WACA,SAAS,MAAM,IAAI,aAAa,GAAG,MAAM,KAAK;AAEpD,QAAI,CAAC,iBAAiB,WAAW,eAAe;AAC9C,aAAO,OAAO,KAAK,iBAAiB;AAAA,IACtC;AAEA,WAAO,OAAO,KAAK,IAAI,MAAM,GAAG,iBAAiB,EAAE;AAAA,EACrD;AAEA,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EACF;AACF;","names":[]}
@@ -1,4 +1,3 @@
1
- import 'react-intlayer';
2
1
  import { Locales } from '@intlayer/config/client';
3
2
 
4
3
  declare const useLocale: () => {
@@ -18,22 +18,22 @@ 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
- IntlayerClient: () => import_react_intlayer.IntlayerClient,
22
- IntlayerClientProvider: () => import_react_intlayer.IntlayerClientProvider,
21
+ IntlayerClientContext: () => import_react_intlayer.IntlayerClientContext,
22
+ IntlayerClientProvider: () => import_client.IntlayerClientProvider,
23
23
  generateStaticParams: () => import_generateStaticParams.generateStaticParams,
24
24
  getTranslation: () => import_react_intlayer.getTranslation,
25
25
  useIntlayer: () => import_react_intlayer.useIntlayer,
26
- useLocale: () => import_useLocale.useLocale,
26
+ useLocale: () => import_client.useLocale,
27
27
  useLocaleCookie: () => import_react_intlayer.useLocaleCookie,
28
28
  useTraduction: () => import_react_intlayer.useTraduction
29
29
  });
30
30
  module.exports = __toCommonJS(src_exports);
31
31
  var import_react_intlayer = require("react-intlayer");
32
32
  var import_generateStaticParams = require('./generateStaticParams.cjs');
33
- var import_useLocale = require('./client/useLocale.cjs');
33
+ var import_client = require('./client/index.cjs');
34
34
  // Annotate the CommonJS export names for ESM import in node:
35
35
  0 && (module.exports = {
36
- IntlayerClient,
36
+ IntlayerClientContext,
37
37
  IntlayerClientProvider,
38
38
  generateStaticParams,
39
39
  getTranslation,
@@ -1 +1 @@
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":[]}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export {\n getTranslation,\n IntlayerClientContext,\n useIntlayer,\n useTraduction,\n useLocaleCookie,\n} from 'react-intlayer';\nexport { generateStaticParams } from './generateStaticParams';\nexport type { LocalParams, NextPageIntlayer } 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,4BAMO;AACP,kCAAqC;AAErC,oBAIO;","names":[]}
@@ -1,7 +1,8 @@
1
- export { IntlayerClient, IntlayerClientProvider, getTranslation, useIntlayer, useLocaleCookie, useTraduction } from 'react-intlayer';
1
+ export { IntlayerClientContext, 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';
5
+ export { IntlayerClientProvider, IntlayerClientProviderProps } from './client/IntlayerClientProvider.js';
5
6
  import '@intlayer/config/client';
6
7
  import 'intlayer';
7
8
  import 'next';
@@ -24,17 +24,37 @@ module.exports = __toCommonJS(withIntlayer_exports);
24
24
  var import_path = require("path");
25
25
  var import_config = require("@intlayer/config");
26
26
  var import_webpack = require("@intlayer/webpack");
27
- const withIntlayer = (_pluginOptions = {}) => (nextConfig = {}) => {
27
+ const withIntlayer = (nextConfig = {}) => {
28
28
  if (typeof nextConfig !== "object")
29
29
  nextConfig = {};
30
30
  const intlayerConfig = (0, import_config.getConfiguration)();
31
31
  const env = (0, import_config.formatEnvVariable)("next");
32
32
  const { mainDir, baseDir } = intlayerConfig.content;
33
+ const dictionariesPath = (0, import_path.join)(mainDir, "dictionaries.mjs");
34
+ const relativeDictionariesPath = (0, import_path.relative)(baseDir, dictionariesPath);
33
35
  return Object.assign({}, nextConfig, {
34
36
  env: { ...nextConfig.env, ...env },
35
- webpack: (config, { isServer, nextRuntime }) => {
36
- const dictionariesPath = (0, import_path.join)(mainDir, "dictionaries.cjs");
37
- const relativeDictionariesPath = (0, import_path.relative)(baseDir, dictionariesPath);
37
+ experimental: {
38
+ ...nextConfig.experimental ?? {},
39
+ // Using Intlayer with Turbopack is not supported as long external modules can't be resolved (such as esbuild or fs)
40
+ turbo: {
41
+ ...nextConfig.experimental?.turbo ?? {},
42
+ resolveAlias: {
43
+ ...nextConfig.experimental?.turbo?.resolveAlias,
44
+ "@intlayer/dictionaries-entry": (0, import_path.resolve)(relativeDictionariesPath)
45
+ },
46
+ rules: {
47
+ "*.node": {
48
+ as: "*.node",
49
+ loaders: ["node-loader"]
50
+ }
51
+ }
52
+ }
53
+ },
54
+ webpack: (config, options) => {
55
+ if (nextConfig.webpack) {
56
+ config = nextConfig.webpack(config, options);
57
+ }
38
58
  config.resolve.alias["@intlayer/dictionaries-entry"] = (0, import_path.resolve)(
39
59
  relativeDictionariesPath
40
60
  );
@@ -47,6 +67,7 @@ const withIntlayer = (_pluginOptions = {}) => (nextConfig = {}) => {
47
67
  test: /\.node$/,
48
68
  loader: "node-loader"
49
69
  });
70
+ const { isServer, nextRuntime } = options;
50
71
  if (isServer && nextRuntime === "nodejs") {
51
72
  config.plugins.push(new import_webpack.IntLayerPlugin());
52
73
  }
@@ -1 +1 @@
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 * ```ts\n * // next.config.js\n * export default withIntlayer(nextConfig)\n * ```\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;AAsBxB,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":[]}
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 * ```ts\n * // next.config.js\n * export default withIntlayer(nextConfig)\n * ```\n *\n */\nexport const withIntlayer = (\n nextConfig: Partial<NextConfig> = {}\n): 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 const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n return Object.assign({}, nextConfig, {\n env: { ...nextConfig.env, ...env },\n\n experimental: {\n ...(nextConfig.experimental ?? {}),\n // Using Intlayer with Turbopack is not supported as long external modules can't be resolved (such as esbuild or fs)\n turbo: {\n ...(nextConfig.experimental?.turbo ?? {}),\n resolveAlias: {\n ...nextConfig.experimental?.turbo?.resolveAlias,\n '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),\n },\n\n rules: {\n '*.node': {\n as: '*.node',\n loaders: ['node-loader'],\n },\n },\n },\n },\n\n webpack: (config: WebpackParams['0'], options: WebpackParams[1]) => {\n if (nextConfig.webpack) {\n // Invoke the existing webpack config if it exists\n config = nextConfig.webpack(config, options);\n }\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 const { isServer, nextRuntime } = options;\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 } satisfies Partial<NextConfig>);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAwC;AACxC,oBAAoD;AACpD,qBAA+B;AAsBxB,MAAM,eAAe,CAC1B,aAAkC,CAAC,MACX;AACxB,MAAI,OAAO,eAAe;AAAU,iBAAa,CAAC;AAElD,QAAM,qBAAiB,gCAAiB;AAGxC,QAAM,UAAM,iCAAkB,MAAM;AAEpC,QAAM,EAAE,SAAS,QAAQ,IAAI,eAAe;AAC5C,QAAM,uBAAmB,kBAAK,SAAS,kBAAkB;AACzD,QAAM,+BAA2B,sBAAS,SAAS,gBAAgB;AAEnE,SAAO,OAAO,OAAO,CAAC,GAAG,YAAY;AAAA,IACnC,KAAK,EAAE,GAAG,WAAW,KAAK,GAAG,IAAI;AAAA,IAEjC,cAAc;AAAA,MACZ,GAAI,WAAW,gBAAgB,CAAC;AAAA;AAAA,MAEhC,OAAO;AAAA,QACL,GAAI,WAAW,cAAc,SAAS,CAAC;AAAA,QACvC,cAAc;AAAA,UACZ,GAAG,WAAW,cAAc,OAAO;AAAA,UACnC,oCAAgC,qBAAQ,wBAAwB;AAAA,QAClE;AAAA,QAEA,OAAO;AAAA,UACL,UAAU;AAAA,YACR,IAAI;AAAA,YACJ,SAAS,CAAC,aAAa;AAAA,UACzB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IAEA,SAAS,CAAC,QAA4B,YAA8B;AAClE,UAAI,WAAW,SAAS;AAEtB,iBAAS,WAAW,QAAQ,QAAQ,OAAO;AAAA,MAC7C;AAEA,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;AAED,YAAM,EAAE,UAAU,YAAY,IAAI;AAGlC,UAAI,YAAY,gBAAgB,UAAU;AACxC,eAAO,QAAQ,KAAK,IAAI,8BAAe,CAAC;AAAA,MAC1C;AAEA,aAAO;AAAA,IACT;AAAA,EACF,CAA+B;AACjC;","names":[]}
@@ -1,7 +1,6 @@
1
1
  import 'react-intlayer/server';
2
2
  import { NextConfig } from 'next';
3
3
 
4
- type PluginOptions = {};
5
4
  /**
6
5
  * A Next.js plugin that adds the intlayer configuration to the webpack configuration
7
6
  * and sets the environment variables
@@ -14,6 +13,6 @@ type PluginOptions = {};
14
13
  * ```
15
14
  *
16
15
  */
17
- declare const withIntlayer: (_pluginOptions?: PluginOptions) => (nextConfig?: Partial<NextConfig>) => Partial<NextConfig>;
16
+ declare const withIntlayer: (nextConfig?: Partial<NextConfig>) => Partial<NextConfig>;
18
17
 
19
18
  export { withIntlayer };
@@ -0,0 +1,7 @@
1
+ import { FC } from 'react';
2
+ import { IntlayerProviderProps } from 'react-intlayer';
3
+
4
+ type IntlayerClientProviderProps = IntlayerProviderProps;
5
+ declare const IntlayerClientProvider: FC<IntlayerProviderProps>;
6
+
7
+ export { IntlayerClientProvider, type IntlayerClientProviderProps };
@@ -0,0 +1,12 @@
1
+ "use client";
2
+ import { jsx } from "react/jsx-runtime";
3
+ import { IntlayerProvider } from "react-intlayer";
4
+ import { useLocale } from './useLocale.mjs';
5
+ const IntlayerClientProvider = (props) => {
6
+ const { setLocale } = useLocale();
7
+ return /* @__PURE__ */ jsx(IntlayerProvider, { setLocale, ...props });
8
+ };
9
+ export {
10
+ IntlayerClientProvider
11
+ };
12
+ //# sourceMappingURL=IntlayerClientProvider.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/client/IntlayerClientProvider.tsx"],"sourcesContent":["'use client';\n\nimport type { FC } from 'react';\nimport { IntlayerProvider, type IntlayerProviderProps } from 'react-intlayer';\nimport { useLocale } from './useLocale';\n\nexport type IntlayerClientProviderProps = IntlayerProviderProps;\n\nexport const IntlayerClientProvider: FC<IntlayerProviderProps> = (props) => {\n const { setLocale } = useLocale();\n return <IntlayerProvider setLocale={setLocale} {...props} />;\n};\n"],"mappings":";AAUS;AAPT,SAAS,wBAAoD;AAC7D,SAAS,iBAAiB;AAInB,MAAM,yBAAoD,CAAC,UAAU;AAC1E,QAAM,EAAE,UAAU,IAAI,UAAU;AAChC,SAAO,oBAAC,oBAAiB,WAAuB,GAAG,OAAO;AAC5D;","names":[]}
@@ -1,3 +1,5 @@
1
- export { IntlayerClient, IntlayerClientProvider, getTranslation, useIntlayer, useLocaleCookie, useTraduction } from 'react-intlayer';
1
+ export { IntlayerClientContext, getTranslation, useIntlayer, useLocaleCookie, useTraduction } from 'react-intlayer';
2
2
  export { useLocale } from './useLocale.mjs';
3
+ export { IntlayerClientProvider, IntlayerClientProviderProps } from './IntlayerClientProvider.mjs';
3
4
  import '@intlayer/config/client';
5
+ import 'react';
@@ -1,14 +1,16 @@
1
1
  import {
2
2
  getTranslation,
3
- IntlayerClientProvider,
4
- IntlayerClient,
3
+ IntlayerClientContext,
5
4
  useIntlayer,
6
5
  useTraduction,
7
6
  useLocaleCookie
8
7
  } from "react-intlayer";
9
8
  import { useLocale } from './useLocale.mjs';
9
+ import {
10
+ IntlayerClientProvider
11
+ } from './IntlayerClientProvider.mjs';
10
12
  export {
11
- IntlayerClient,
13
+ IntlayerClientContext,
12
14
  IntlayerClientProvider,
13
15
  getTranslation,
14
16
  useIntlayer,
@@ -1 +1 @@
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":[]}
1
+ {"version":3,"sources":["../../../src/client/index.ts"],"sourcesContent":["export {\n getTranslation,\n IntlayerClientContext,\n useIntlayer,\n useTraduction,\n useLocaleCookie,\n} from 'react-intlayer';\nexport { useLocale } from './useLocale';\nexport {\n IntlayerClientProvider,\n type IntlayerClientProviderProps,\n} from './IntlayerClientProvider';\n"],"mappings":"AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,OAEK;","names":[]}
@@ -1,4 +1,3 @@
1
- import 'react-intlayer';
2
1
  import { Locales } from '@intlayer/config/client';
3
2
 
4
3
  declare const useLocale: () => {
@@ -13,8 +13,6 @@ const useLocale = () => {
13
13
  locale: currentLocale
14
14
  } = reactLocaleHook;
15
15
  const setLocale = (locale) => {
16
- if (currentLocale.toString() === locale.toString())
17
- return;
18
16
  if (!availableLocales.includes(locale)) {
19
17
  console.error(`Locale ${locale} is not available`);
20
18
  return;
@@ -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 router = useRouter();\n const pathname = usePathname();\n\n const {\n defaultLocale,\n availableLocales,\n locale: currentLocale,\n } = reactLocaleHook;\n\n const setLocale = (locale: Locales) => {\n if (currentLocale.toString() === locale.toString()) return;\n\n if (!availableLocales.includes(locale)) {\n console.error(`Locale ${locale} is not available`);\n return;\n }\n\n setLocaleCookie(locale);\n\n const pathWithoutLocale =\n !prefixDefault && currentLocale.toString() === defaultLocale.toString()\n ? pathname\n : pathname.slice(`/${currentLocale}`.length) || '/';\n\n if (!prefixDefault && locale === defaultLocale) {\n return router.push(pathWithoutLocale);\n }\n\n return router.push(`/${locale}${pathWithoutLocale}`);\n };\n\n return {\n ...reactLocaleHook,\n setLocale,\n };\n};\n"],"mappings":"AAAA,SAAuB,wBAAwB;AAC/C,SAAS,aAAa,iBAAiB;AACvC,SAAS,iBAAiB,qBAAqB;AAExC,MAAM,YAAY,MAAM;AAC7B,QAAM,EAAE,cAAc,IAAI,iBAAiB,EAAE;AAC7C,QAAM,EAAE,gBAAgB,IAAI,gBAAgB;AAC5C,QAAM,kBAAkB,cAAc;AACtC,QAAM,SAAS,UAAU;AACzB,QAAM,WAAW,YAAY;AAE7B,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,EACV,IAAI;AAEJ,QAAM,YAAY,CAAC,WAAoB;AACrC,QAAI,cAAc,SAAS,MAAM,OAAO,SAAS;AAAG;AAEpD,QAAI,CAAC,iBAAiB,SAAS,MAAM,GAAG;AACtC,cAAQ,MAAM,UAAU,MAAM,mBAAmB;AACjD;AAAA,IACF;AAEA,oBAAgB,MAAM;AAEtB,UAAM,oBACJ,CAAC,iBAAiB,cAAc,SAAS,MAAM,cAAc,SAAS,IAClE,WACA,SAAS,MAAM,IAAI,aAAa,GAAG,MAAM,KAAK;AAEpD,QAAI,CAAC,iBAAiB,WAAW,eAAe;AAC9C,aAAO,OAAO,KAAK,iBAAiB;AAAA,IACtC;AAEA,WAAO,OAAO,KAAK,IAAI,MAAM,GAAG,iBAAiB,EAAE;AAAA,EACrD;AAEA,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EACF;AACF;","names":[]}
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 router = useRouter();\n const pathname = usePathname();\n\n const {\n defaultLocale,\n availableLocales,\n locale: currentLocale,\n } = reactLocaleHook;\n\n const setLocale = (locale: Locales) => {\n if (!availableLocales.includes(locale)) {\n console.error(`Locale ${locale} is not available`);\n return;\n }\n\n setLocaleCookie(locale);\n\n const pathWithoutLocale =\n !prefixDefault && currentLocale.toString() === defaultLocale.toString()\n ? pathname\n : pathname.slice(`/${currentLocale}`.length) || '/';\n\n if (!prefixDefault && locale === defaultLocale) {\n return router.push(pathWithoutLocale);\n }\n\n return router.push(`/${locale}${pathWithoutLocale}`);\n };\n\n return {\n ...reactLocaleHook,\n setLocale,\n };\n};\n"],"mappings":"AAAA,SAAuB,wBAAwB;AAC/C,SAAS,aAAa,iBAAiB;AACvC,SAAS,iBAAiB,qBAAqB;AAExC,MAAM,YAAY,MAAM;AAC7B,QAAM,EAAE,cAAc,IAAI,iBAAiB,EAAE;AAC7C,QAAM,EAAE,gBAAgB,IAAI,gBAAgB;AAC5C,QAAM,kBAAkB,cAAc;AACtC,QAAM,SAAS,UAAU;AACzB,QAAM,WAAW,YAAY;AAE7B,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,EACV,IAAI;AAEJ,QAAM,YAAY,CAAC,WAAoB;AACrC,QAAI,CAAC,iBAAiB,SAAS,MAAM,GAAG;AACtC,cAAQ,MAAM,UAAU,MAAM,mBAAmB;AACjD;AAAA,IACF;AAEA,oBAAgB,MAAM;AAEtB,UAAM,oBACJ,CAAC,iBAAiB,cAAc,SAAS,MAAM,cAAc,SAAS,IAClE,WACA,SAAS,MAAM,IAAI,aAAa,GAAG,MAAM,KAAK;AAEpD,QAAI,CAAC,iBAAiB,WAAW,eAAe;AAC9C,aAAO,OAAO,KAAK,iBAAiB;AAAA,IACtC;AAEA,WAAO,OAAO,KAAK,IAAI,MAAM,GAAG,iBAAiB,EAAE;AAAA,EACrD;AAEA,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EACF;AACF;","names":[]}
@@ -1,7 +1,8 @@
1
- export { IntlayerClient, IntlayerClientProvider, getTranslation, useIntlayer, useLocaleCookie, useTraduction } from 'react-intlayer';
1
+ export { IntlayerClientContext, 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';
5
+ export { IntlayerClientProvider, IntlayerClientProviderProps } from './client/IntlayerClientProvider.mjs';
5
6
  import '@intlayer/config/client';
6
7
  import 'intlayer';
7
8
  import 'next';
@@ -1,15 +1,17 @@
1
1
  import {
2
2
  getTranslation,
3
- IntlayerClientProvider,
4
- IntlayerClient,
3
+ IntlayerClientContext,
5
4
  useIntlayer,
6
5
  useTraduction,
7
6
  useLocaleCookie
8
7
  } from "react-intlayer";
9
8
  import { generateStaticParams } from './generateStaticParams.mjs';
10
- import { useLocale } from './client/useLocale.mjs';
9
+ import {
10
+ useLocale,
11
+ IntlayerClientProvider
12
+ } from './client/index.mjs';
11
13
  export {
12
- IntlayerClient,
14
+ IntlayerClientContext,
13
15
  IntlayerClientProvider,
14
16
  generateStaticParams,
15
17
  getTranslation,
@@ -1 +1 @@
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
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export {\n getTranslation,\n IntlayerClientContext,\n useIntlayer,\n useTraduction,\n useLocaleCookie,\n} from 'react-intlayer';\nexport { generateStaticParams } from './generateStaticParams';\nexport type { LocalParams, NextPageIntlayer } 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,OACK;AACP,SAAS,4BAA4B;AAErC;AAAA,EACE;AAAA,EACA;AAAA,OAEK;","names":[]}
@@ -1,7 +1,6 @@
1
1
  import 'react-intlayer/server';
2
2
  import { NextConfig } from 'next';
3
3
 
4
- type PluginOptions = {};
5
4
  /**
6
5
  * A Next.js plugin that adds the intlayer configuration to the webpack configuration
7
6
  * and sets the environment variables
@@ -14,6 +13,6 @@ type PluginOptions = {};
14
13
  * ```
15
14
  *
16
15
  */
17
- declare const withIntlayer: (_pluginOptions?: PluginOptions) => (nextConfig?: Partial<NextConfig>) => Partial<NextConfig>;
16
+ declare const withIntlayer: (nextConfig?: Partial<NextConfig>) => Partial<NextConfig>;
18
17
 
19
18
  export { withIntlayer };
@@ -1,17 +1,37 @@
1
1
  import { resolve, relative, join } from "path";
2
2
  import { getConfiguration, formatEnvVariable } from "@intlayer/config";
3
3
  import { IntLayerPlugin } from "@intlayer/webpack";
4
- const withIntlayer = (_pluginOptions = {}) => (nextConfig = {}) => {
4
+ const withIntlayer = (nextConfig = {}) => {
5
5
  if (typeof nextConfig !== "object")
6
6
  nextConfig = {};
7
7
  const intlayerConfig = getConfiguration();
8
8
  const env = formatEnvVariable("next");
9
9
  const { mainDir, baseDir } = intlayerConfig.content;
10
+ const dictionariesPath = join(mainDir, "dictionaries.mjs");
11
+ const relativeDictionariesPath = relative(baseDir, dictionariesPath);
10
12
  return Object.assign({}, nextConfig, {
11
13
  env: { ...nextConfig.env, ...env },
12
- webpack: (config, { isServer, nextRuntime }) => {
13
- const dictionariesPath = join(mainDir, "dictionaries.cjs");
14
- const relativeDictionariesPath = relative(baseDir, dictionariesPath);
14
+ experimental: {
15
+ ...nextConfig.experimental ?? {},
16
+ // Using Intlayer with Turbopack is not supported as long external modules can't be resolved (such as esbuild or fs)
17
+ turbo: {
18
+ ...nextConfig.experimental?.turbo ?? {},
19
+ resolveAlias: {
20
+ ...nextConfig.experimental?.turbo?.resolveAlias,
21
+ "@intlayer/dictionaries-entry": resolve(relativeDictionariesPath)
22
+ },
23
+ rules: {
24
+ "*.node": {
25
+ as: "*.node",
26
+ loaders: ["node-loader"]
27
+ }
28
+ }
29
+ }
30
+ },
31
+ webpack: (config, options) => {
32
+ if (nextConfig.webpack) {
33
+ config = nextConfig.webpack(config, options);
34
+ }
15
35
  config.resolve.alias["@intlayer/dictionaries-entry"] = resolve(
16
36
  relativeDictionariesPath
17
37
  );
@@ -24,6 +44,7 @@ const withIntlayer = (_pluginOptions = {}) => (nextConfig = {}) => {
24
44
  test: /\.node$/,
25
45
  loader: "node-loader"
26
46
  });
47
+ const { isServer, nextRuntime } = options;
27
48
  if (isServer && nextRuntime === "nodejs") {
28
49
  config.plugins.push(new IntLayerPlugin());
29
50
  }
@@ -1 +1 @@
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 * ```ts\n * // next.config.js\n * export default withIntlayer(nextConfig)\n * ```\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;AAsBxB,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":[]}
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 * ```ts\n * // next.config.js\n * export default withIntlayer(nextConfig)\n * ```\n *\n */\nexport const withIntlayer = (\n nextConfig: Partial<NextConfig> = {}\n): 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 const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n return Object.assign({}, nextConfig, {\n env: { ...nextConfig.env, ...env },\n\n experimental: {\n ...(nextConfig.experimental ?? {}),\n // Using Intlayer with Turbopack is not supported as long external modules can't be resolved (such as esbuild or fs)\n turbo: {\n ...(nextConfig.experimental?.turbo ?? {}),\n resolveAlias: {\n ...nextConfig.experimental?.turbo?.resolveAlias,\n '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),\n },\n\n rules: {\n '*.node': {\n as: '*.node',\n loaders: ['node-loader'],\n },\n },\n },\n },\n\n webpack: (config: WebpackParams['0'], options: WebpackParams[1]) => {\n if (nextConfig.webpack) {\n // Invoke the existing webpack config if it exists\n config = nextConfig.webpack(config, options);\n }\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 const { isServer, nextRuntime } = options;\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 } satisfies Partial<NextConfig>);\n};\n"],"mappings":"AAAA,SAAS,SAAS,UAAU,YAAY;AACxC,SAAS,kBAAkB,yBAAyB;AACpD,SAAS,sBAAsB;AAsBxB,MAAM,eAAe,CAC1B,aAAkC,CAAC,MACX;AACxB,MAAI,OAAO,eAAe;AAAU,iBAAa,CAAC;AAElD,QAAM,iBAAiB,iBAAiB;AAGxC,QAAM,MAAM,kBAAkB,MAAM;AAEpC,QAAM,EAAE,SAAS,QAAQ,IAAI,eAAe;AAC5C,QAAM,mBAAmB,KAAK,SAAS,kBAAkB;AACzD,QAAM,2BAA2B,SAAS,SAAS,gBAAgB;AAEnE,SAAO,OAAO,OAAO,CAAC,GAAG,YAAY;AAAA,IACnC,KAAK,EAAE,GAAG,WAAW,KAAK,GAAG,IAAI;AAAA,IAEjC,cAAc;AAAA,MACZ,GAAI,WAAW,gBAAgB,CAAC;AAAA;AAAA,MAEhC,OAAO;AAAA,QACL,GAAI,WAAW,cAAc,SAAS,CAAC;AAAA,QACvC,cAAc;AAAA,UACZ,GAAG,WAAW,cAAc,OAAO;AAAA,UACnC,gCAAgC,QAAQ,wBAAwB;AAAA,QAClE;AAAA,QAEA,OAAO;AAAA,UACL,UAAU;AAAA,YACR,IAAI;AAAA,YACJ,SAAS,CAAC,aAAa;AAAA,UACzB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IAEA,SAAS,CAAC,QAA4B,YAA8B;AAClE,UAAI,WAAW,SAAS;AAEtB,iBAAS,WAAW,QAAQ,QAAQ,OAAO;AAAA,MAC7C;AAEA,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;AAED,YAAM,EAAE,UAAU,YAAY,IAAI;AAGlC,UAAI,YAAY,gBAAgB,UAAU;AACxC,eAAO,QAAQ,KAAK,IAAI,eAAe,CAAC;AAAA,MAC1C;AAEA,aAAO;AAAA,IACT;AAAA,EACF,CAA+B;AACjC;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-intlayer",
3
- "version": "1.2.1",
3
+ "version": "2.0.1",
4
4
  "private": false,
5
5
  "description": "Webpack configuration for IntLayer using NextJS",
6
6
  "keywords": [
@@ -57,8 +57,6 @@
57
57
  },
58
58
  "files": [
59
59
  "./dist",
60
- "./src",
61
- "./bin",
62
60
  "./package.json"
63
61
  ],
64
62
  "dependencies": {
@@ -66,13 +64,13 @@
66
64
  "negotiator": "^0.6.3",
67
65
  "next": "14.1.4",
68
66
  "webpack": "^5.91.0",
69
- "@intlayer/chokidar": "^1.2.1",
70
- "@intlayer/config": "^1.2.1",
71
- "@intlayer/core": "^1.2.1",
72
- "@intlayer/dictionaries-entry": "^1.2.1",
73
- "@intlayer/webpack": "^1.2.1",
74
- "intlayer": "^1.2.1",
75
- "react-intlayer": "^1.2.1"
67
+ "@intlayer/chokidar": "^2.0.1",
68
+ "@intlayer/config": "^2.0.1",
69
+ "@intlayer/core": "^2.0.1",
70
+ "@intlayer/dictionaries-entry": "^2.0.1",
71
+ "@intlayer/webpack": "^2.0.1",
72
+ "intlayer": "^2.0.1",
73
+ "react-intlayer": "^2.0.1"
76
74
  },
77
75
  "devDependencies": {
78
76
  "@types/negotiator": "^0.6.3",
package/src/index.ts CHANGED
@@ -1,11 +1,14 @@
1
1
  export {
2
2
  getTranslation,
3
- IntlayerClientProvider,
4
- IntlayerClient,
3
+ IntlayerClientContext,
5
4
  useIntlayer,
6
5
  useTraduction,
7
6
  useLocaleCookie,
8
7
  } from 'react-intlayer';
9
8
  export { generateStaticParams } from './generateStaticParams';
10
9
  export type { LocalParams, NextPageIntlayer } from './types/index';
11
- export { useLocale } from './client/useLocale';
10
+ export {
11
+ useLocale,
12
+ IntlayerClientProvider,
13
+ type IntlayerClientProviderProps,
14
+ } from './client/index';
@@ -1,9 +0,0 @@
1
- export {
2
- getTranslation,
3
- IntlayerClientProvider,
4
- IntlayerClient,
5
- useIntlayer,
6
- useTraduction,
7
- useLocaleCookie,
8
- } from 'react-intlayer';
9
- export { useLocale } from './useLocale';
@@ -1,44 +0,0 @@
1
- import { type Locales, getConfiguration } from '@intlayer/config/client';
2
- import { usePathname, useRouter } from 'next/navigation.js';
3
- import { useLocaleCookie, useLocaleBase } from 'react-intlayer';
4
-
5
- export const useLocale = () => {
6
- const { prefixDefault } = getConfiguration().middleware;
7
- const { setLocaleCookie } = useLocaleCookie();
8
- const reactLocaleHook = useLocaleBase();
9
- const router = useRouter();
10
- const pathname = usePathname();
11
-
12
- const {
13
- defaultLocale,
14
- availableLocales,
15
- locale: currentLocale,
16
- } = reactLocaleHook;
17
-
18
- const setLocale = (locale: Locales) => {
19
- if (currentLocale.toString() === locale.toString()) return;
20
-
21
- if (!availableLocales.includes(locale)) {
22
- console.error(`Locale ${locale} is not available`);
23
- return;
24
- }
25
-
26
- setLocaleCookie(locale);
27
-
28
- const pathWithoutLocale =
29
- !prefixDefault && currentLocale.toString() === defaultLocale.toString()
30
- ? pathname
31
- : pathname.slice(`/${currentLocale}`.length) || '/';
32
-
33
- if (!prefixDefault && locale === defaultLocale) {
34
- return router.push(pathWithoutLocale);
35
- }
36
-
37
- return router.push(`/${locale}${pathWithoutLocale}`);
38
- };
39
-
40
- return {
41
- ...reactLocaleHook,
42
- setLocale,
43
- };
44
- };
@@ -1,5 +0,0 @@
1
- import { getConfiguration } from '@intlayer/config/client';
2
-
3
- const { locales } = getConfiguration().internationalization;
4
-
5
- export const generateStaticParams = () => locales.map((locale) => ({ locale }));
@@ -1 +0,0 @@
1
- export * from './intlayerMiddleware';
@@ -1,232 +0,0 @@
1
- import { type Locales, getConfiguration } from '@intlayer/config/client';
2
- import { type NextRequest, NextResponse } from 'next/server';
3
- import { localeDetector } from './localeDetector';
4
-
5
- const { internationalization, middleware } = getConfiguration();
6
- const { locales, defaultLocale } = internationalization;
7
- const {
8
- headerName,
9
- cookieName,
10
- prefixDefault,
11
- basePath,
12
- serverSetCookie,
13
- noPrefix,
14
- } = middleware;
15
-
16
- /**
17
- * Middleware that handles the internationalization layer
18
- *
19
- * Usage:
20
- *
21
- * // ./src/middleware.ts
22
- *
23
- * ```ts
24
- * export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';
25
- *
26
- * // applies this middleware only to files in the app directory
27
- * export const config = {
28
- * matcher: '/((?!api|static|.*\\..*|_next).*)',
29
- * };
30
- * ```
31
- *
32
- */
33
- export const intlayerMiddleware = (request: NextRequest): NextResponse => {
34
- const pathname = request.nextUrl.pathname;
35
- const cookieLocale = getCookieLocale(request);
36
- const basePathTrailingSlash = basePath.endsWith('/');
37
-
38
- if (noPrefix) {
39
- return handleNoPrefix(
40
- request,
41
- cookieLocale,
42
- pathname,
43
- basePathTrailingSlash
44
- );
45
- }
46
-
47
- const pathLocale = getPathLocale(pathname);
48
- return handlePrefix(
49
- request,
50
- cookieLocale,
51
- pathLocale,
52
- pathname,
53
- basePathTrailingSlash
54
- );
55
- };
56
-
57
- const getCookieLocale = (request: NextRequest): Locales | undefined => {
58
- if (!cookieName) return undefined;
59
- const cookieValue = request.cookies.get(cookieName)?.value as Locales;
60
- if (cookieValue && locales.includes(cookieValue)) {
61
- return cookieValue;
62
- }
63
- };
64
-
65
- const handleNoPrefix = (
66
- request: NextRequest,
67
- cookieLocale: Locales | undefined,
68
- pathname: string,
69
- basePathTrailingSlash: boolean
70
- ): NextResponse => {
71
- const locale = cookieLocale ?? defaultLocale;
72
- const newPath = constructPath(
73
- locale,
74
- pathname,
75
- basePath,
76
- basePathTrailingSlash
77
- );
78
- return rewriteUrl(request, newPath, locale);
79
- };
80
-
81
- const getPathLocale = (pathname: string): Locales | undefined =>
82
- locales.find(
83
- (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
84
- );
85
-
86
- const handlePrefix = (
87
- request: NextRequest,
88
- cookieLocale: Locales | undefined,
89
- pathLocale: Locales | undefined,
90
- pathname: string,
91
- basePathTrailingSlash: boolean
92
- ): NextResponse => {
93
- if (!pathLocale) {
94
- return handleMissingPathLocale(
95
- request,
96
- cookieLocale,
97
- pathname,
98
- basePathTrailingSlash
99
- );
100
- }
101
- return handleExistingPathLocale(
102
- request,
103
- cookieLocale,
104
- pathLocale,
105
- pathname,
106
- basePathTrailingSlash
107
- );
108
- };
109
-
110
- const handleMissingPathLocale = (
111
- request: NextRequest,
112
- cookieLocale: Locales | undefined,
113
- pathname: string,
114
- basePathTrailingSlash: boolean
115
- ): NextResponse => {
116
- let locale = cookieLocale ?? localeDetector?.(request) ?? defaultLocale;
117
- if (!locales.includes(locale)) {
118
- console.warn(
119
- 'The localeDetector callback must return a locale included in your locales array. Reverting to using defaultLocale.'
120
- );
121
- locale = defaultLocale;
122
- }
123
- const newPath = constructPath(
124
- locale,
125
- pathname,
126
- basePath,
127
- basePathTrailingSlash
128
- );
129
- return prefixDefault || locale !== defaultLocale
130
- ? redirectUrl(request, newPath)
131
- : rewriteUrl(request, newPath, locale);
132
- };
133
-
134
- const handleExistingPathLocale = (
135
- request: NextRequest,
136
- cookieLocale: Locales | undefined,
137
- pathLocale: Locales,
138
- pathname: string,
139
- basePathTrailingSlash: boolean
140
- ): NextResponse => {
141
- if (
142
- cookieLocale &&
143
- cookieLocale !== pathLocale &&
144
- serverSetCookie !== 'always'
145
- ) {
146
- const newPath = handleCookieLocaleMismatch(
147
- request,
148
- pathname,
149
- pathLocale,
150
- cookieLocale,
151
- basePath,
152
- basePathTrailingSlash
153
- );
154
- return redirectUrl(request, newPath);
155
- }
156
-
157
- return handleDefaultLocaleRedirect(
158
- request,
159
- pathLocale,
160
- pathname,
161
- basePathTrailingSlash
162
- );
163
- };
164
-
165
- const handleCookieLocaleMismatch = (
166
- request: NextRequest,
167
-
168
- pathname: string,
169
- pathLocale: Locales,
170
- cookieLocale: Locales,
171
- basePath: string,
172
- basePathTrailingSlash: boolean
173
- ): string => {
174
- const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);
175
- return constructPath(
176
- cookieLocale,
177
- newPath,
178
- basePath,
179
- basePathTrailingSlash,
180
- request.nextUrl.search
181
- );
182
- };
183
-
184
- const handleDefaultLocaleRedirect = (
185
- request: NextRequest,
186
- pathLocale: Locales,
187
- pathname: string,
188
- basePathTrailingSlash: boolean
189
- ): NextResponse => {
190
- if (!prefixDefault && pathLocale === defaultLocale) {
191
- let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';
192
-
193
- if (basePathTrailingSlash) {
194
- pathWithoutLocale = pathWithoutLocale.slice(1);
195
- }
196
-
197
- if (request.nextUrl.search) {
198
- pathWithoutLocale += request.nextUrl.search;
199
- }
200
-
201
- return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);
202
- }
203
- return rewriteUrl(request, pathname, pathLocale);
204
- };
205
-
206
- const constructPath = (
207
- locale: Locales,
208
- path: string,
209
- basePath: string,
210
- basePathTrailingSlash: boolean,
211
- search?: string
212
- ): string => {
213
- let newPath = `${locale}${path}`;
214
- newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${newPath}`;
215
- if (search) {
216
- newPath += search;
217
- }
218
- return newPath;
219
- };
220
-
221
- const rewriteUrl = (
222
- request: NextRequest,
223
- newPath: string,
224
- locale: Locales
225
- ): NextResponse => {
226
- const response = NextResponse.rewrite(new URL(newPath, request.url));
227
- response.headers.set(headerName, locale);
228
- return response;
229
- };
230
-
231
- const redirectUrl = (request: NextRequest, newPath: string): NextResponse =>
232
- NextResponse.redirect(new URL(newPath, request.url));
@@ -1,33 +0,0 @@
1
- import { match } from '@formatjs/intl-localematcher';
2
- import type { Locales } from '@intlayer/config';
3
- import { getConfiguration } from '@intlayer/config/client';
4
- import Negotiator from 'negotiator';
5
- import type { NextRequest } from 'next/server.js';
6
-
7
- const { locales, defaultLocale } = getConfiguration().internationalization;
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
- */
14
- export const localeDetector = (request: NextRequest): Locales => {
15
- const negotiatorHeaders: Record<string, string> = {};
16
-
17
- request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));
18
-
19
- const languages = new Negotiator({ headers: negotiatorHeaders }).languages();
20
-
21
- // match can only use specifically formatted locales
22
- // https://stackoverflow.com/questions/76447732/nextjs-13-i18n-incorrect-locale-information-provided
23
- try {
24
- return match(languages, locales, defaultLocale) as Locales;
25
- } catch (e) {
26
- console.warn(
27
- `No valid locales in accept-language header: ${languages.join(', ')}`
28
- );
29
- console.warn(`Reverting to using defaultLocale: ${defaultLocale}`);
30
-
31
- return defaultLocale;
32
- }
33
- };
@@ -1,9 +0,0 @@
1
- export {
2
- getLocaleContent,
3
- useTraduction,
4
- IntlayerServer,
5
- IntlayerServerProvider,
6
- locale,
7
- useIntlayer,
8
- } from 'react-intlayer/server';
9
- export { withIntlayer } from './withIntlayer';
@@ -1,69 +0,0 @@
1
- import { resolve, relative, join } from 'path';
2
- import { getConfiguration, formatEnvVariable } from '@intlayer/config';
3
- import { IntLayerPlugin } from '@intlayer/webpack';
4
- import type { NextConfig } from 'next';
5
- import type { NextJsWebpackConfig } from 'next/dist/server/config-shared';
6
-
7
- type PluginOptions = {
8
- // TODO: add options
9
- };
10
-
11
- type WebpackParams = Parameters<NextJsWebpackConfig>;
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
- * ```ts
20
- * // next.config.js
21
- * export default withIntlayer(nextConfig)
22
- * ```
23
- *
24
- */
25
- export const withIntlayer =
26
- (_pluginOptions: PluginOptions = {}) =>
27
- (nextConfig: Partial<NextConfig> = {}): Partial<NextConfig> => {
28
- if (typeof nextConfig !== 'object') nextConfig = {};
29
-
30
- const intlayerConfig = getConfiguration();
31
-
32
- // Set all configuration values as environment variables
33
- const env = formatEnvVariable('next');
34
-
35
- const { mainDir, baseDir } = intlayerConfig.content;
36
-
37
- return Object.assign({}, nextConfig, {
38
- env: { ...nextConfig.env, ...env },
39
-
40
- webpack: (
41
- config: WebpackParams['0'],
42
- { isServer, nextRuntime }: WebpackParams[1]
43
- ) => {
44
- const dictionariesPath = join(mainDir, 'dictionaries.cjs');
45
- const relativeDictionariesPath = relative(baseDir, dictionariesPath);
46
-
47
- config.resolve.alias['@intlayer/dictionaries-entry'] = resolve(
48
- relativeDictionariesPath
49
- );
50
-
51
- config.externals.push({
52
- esbuild: 'esbuild',
53
- module: 'module',
54
- fs: 'fs',
55
- });
56
- config.module.rules.push({
57
- test: /\.node$/,
58
- loader: 'node-loader',
59
- });
60
-
61
- // Apply IntLayerPlugin only on the server-side
62
- if (isServer && nextRuntime === 'nodejs') {
63
- config.plugins.push(new IntLayerPlugin());
64
- }
65
-
66
- return config;
67
- },
68
- });
69
- };
@@ -1,10 +0,0 @@
1
- import type { Locales } from 'intlayer';
2
- import type { NextPage } from 'next';
3
- import type { PropsWithChildren } from 'react';
4
-
5
- export type LocalParams = {
6
- params: { locale: Locales };
7
- };
8
-
9
- export type NextPageIntlayer = NextPage<LocalParams>;
10
- export type NextLayoutIntlayer = NextPage<PropsWithChildren<LocalParams>>;
@@ -1 +0,0 @@
1
- export type { LocalParams, NextPageIntlayer } from './NextPage';