next-intlayer 1.0.0 → 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 +17 -23
- package/dist/cjs/client/index.cjs.map +1 -1
- package/dist/cjs/client/index.d.ts +3 -10
- package/dist/cjs/client/useLocale.cjs +13 -20
- package/dist/cjs/client/useLocale.cjs.map +1 -1
- package/dist/cjs/client/useLocale.d.ts +7 -7
- package/dist/cjs/generateStaticParams.cjs +8 -13
- package/dist/cjs/generateStaticParams.d.ts +2 -2
- package/dist/cjs/index.cjs +19 -25
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +8 -15
- package/dist/cjs/middleware/index.cjs +9 -21
- package/dist/cjs/middleware/index.d.ts +2 -2
- package/dist/cjs/middleware/intlayerMiddleware.cjs +28 -86
- package/dist/cjs/middleware/intlayerMiddleware.cjs.map +1 -1
- package/dist/cjs/middleware/intlayerMiddleware.d.ts +16 -1
- package/dist/cjs/middleware/localeDetector.cjs +20 -37
- package/dist/cjs/middleware/localeDetector.cjs.map +1 -1
- package/dist/cjs/middleware/localeDetector.d.ts +7 -2
- package/dist/cjs/server/index.cjs +17 -22
- package/dist/cjs/server/index.cjs.map +1 -1
- package/dist/cjs/server/index.d.ts +3 -10
- package/dist/cjs/server/withIntlayer.cjs +38 -53
- package/dist/cjs/server/withIntlayer.cjs.map +1 -1
- package/dist/cjs/server/withIntlayer.d.ts +13 -5
- package/dist/cjs/types/NextPage.cjs +4 -8
- package/dist/cjs/types/NextPage.d.ts +6 -6
- package/dist/cjs/types/index.cjs +4 -8
- package/dist/cjs/types/index.d.ts +4 -4
- package/dist/esm/client/index.d.mts +3 -10
- package/dist/esm/client/index.mjs +8 -8
- package/dist/esm/client/index.mjs.map +1 -1
- package/dist/esm/client/useLocale.d.mts +7 -7
- package/dist/esm/client/useLocale.mjs +12 -10
- package/dist/esm/client/useLocale.mjs.map +1 -1
- package/dist/esm/generateStaticParams.d.mts +2 -2
- package/dist/esm/generateStaticParams.mjs +4 -2
- package/dist/esm/index.d.mts +8 -15
- package/dist/esm/index.mjs +9 -9
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/middleware/index.d.mts +2 -2
- package/dist/esm/middleware/index.mjs +2 -2
- package/dist/esm/middleware/intlayerMiddleware.d.mts +16 -1
- package/dist/esm/middleware/intlayerMiddleware.mjs +21 -66
- package/dist/esm/middleware/intlayerMiddleware.mjs.map +1 -1
- package/dist/esm/middleware/localeDetector.d.mts +7 -2
- package/dist/esm/middleware/localeDetector.mjs +5 -3
- package/dist/esm/middleware/localeDetector.mjs.map +1 -1
- package/dist/esm/server/index.d.mts +3 -10
- package/dist/esm/server/index.mjs +8 -8
- package/dist/esm/server/index.mjs.map +1 -1
- package/dist/esm/server/withIntlayer.d.mts +13 -5
- package/dist/esm/server/withIntlayer.mjs +34 -37
- package/dist/esm/server/withIntlayer.mjs.map +1 -1
- package/dist/esm/types/NextPage.d.mts +6 -6
- package/dist/esm/types/NextPage.mjs +1 -1
- package/dist/esm/types/index.d.mts +4 -4
- package/dist/esm/types/index.mjs +1 -1
- package/package.json +14 -12
- package/src/client/index.ts +2 -2
- package/src/client/useLocale.ts +6 -4
- 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/dist/cjs/types/index.cjs
CHANGED
|
@@ -4,18 +4,14 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
6
|
var __copyProps = (to, from, except, desc) => {
|
|
7
|
-
if (
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
8
|
for (let key of __getOwnPropNames(from))
|
|
9
9
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
-
__defProp(to, key, {
|
|
11
|
-
get: () => from[key],
|
|
12
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
|
|
13
|
-
});
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
11
|
}
|
|
15
12
|
return to;
|
|
16
13
|
};
|
|
17
|
-
var __toCommonJS = (mod) =>
|
|
18
|
-
__copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
15
|
var types_exports = {};
|
|
20
16
|
module.exports = __toCommonJS(types_exports);
|
|
21
|
-
//# sourceMappingURL=index.cjs.map
|
|
17
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { LocalParams, NextPageIntlayer } from
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
export { LocalParams, NextPageIntlayer } from './NextPage.js';
|
|
2
|
+
import 'intlayer';
|
|
3
|
+
import 'next';
|
|
4
|
+
import 'react';
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
getTranslation,
|
|
5
|
-
useIntlayer,
|
|
6
|
-
useLocaleCookie,
|
|
7
|
-
useTraduction,
|
|
8
|
-
} from "react-intlayer";
|
|
9
|
-
export { useLocale } from "./useLocale.mjs";
|
|
10
|
-
import "intlayer";
|
|
1
|
+
export { IntlayerClient, IntlayerClientProvider, getTranslation, useIntlayer, useLocaleCookie, useTraduction } from 'react-intlayer';
|
|
2
|
+
export { useLocale } from './useLocale.mjs';
|
|
3
|
+
import '@intlayer/config/client';
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getTranslation,
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
IntlayerClientProvider,
|
|
4
|
+
IntlayerClient,
|
|
5
5
|
useIntlayer,
|
|
6
6
|
useTraduction,
|
|
7
|
-
useLocaleCookie
|
|
7
|
+
useLocaleCookie
|
|
8
8
|
} from "react-intlayer";
|
|
9
|
-
import { useLocale } from
|
|
9
|
+
import { useLocale } from './useLocale.mjs';
|
|
10
10
|
export {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
IntlayerClient,
|
|
12
|
+
IntlayerClientProvider,
|
|
13
13
|
getTranslation,
|
|
14
14
|
useIntlayer,
|
|
15
15
|
useLocale,
|
|
16
16
|
useLocaleCookie,
|
|
17
|
-
useTraduction
|
|
17
|
+
useTraduction
|
|
18
18
|
};
|
|
19
|
-
//# sourceMappingURL=index.mjs.map
|
|
19
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -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":[]}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Locales } from
|
|
1
|
+
import 'react-intlayer';
|
|
2
|
+
import { Locales } from '@intlayer/config/client';
|
|
3
3
|
|
|
4
4
|
declare const useLocale: () => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
setLocale: (locale: Locales) => void;
|
|
6
|
+
locale: Locales;
|
|
7
|
+
defaultLocale: Locales;
|
|
8
|
+
availableLocales: Locales[];
|
|
9
|
+
localeList: Locales[];
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
export { useLocale };
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
intlayerMiddlewareConfiguration
|
|
3
|
+
} from "@intlayer/config/client";
|
|
2
4
|
import { usePathname, useRouter } from "next/navigation.js";
|
|
3
5
|
import { useLocale as useReactLocale, useLocaleCookie } from "react-intlayer";
|
|
4
6
|
const { prefixDefault } = intlayerMiddlewareConfiguration;
|
|
@@ -10,19 +12,17 @@ const useLocale = () => {
|
|
|
10
12
|
const {
|
|
11
13
|
defaultLocale,
|
|
12
14
|
availableLocales,
|
|
13
|
-
locale: currentLocale
|
|
15
|
+
locale: currentLocale
|
|
14
16
|
} = reactLocaleHook;
|
|
15
17
|
const setLocale = (locale) => {
|
|
16
|
-
if (currentLocale === locale)
|
|
18
|
+
if (currentLocale.toString() === locale.toString())
|
|
19
|
+
return;
|
|
17
20
|
if (!availableLocales.includes(locale)) {
|
|
18
21
|
console.error(`Locale ${locale} is not available`);
|
|
19
22
|
return;
|
|
20
23
|
}
|
|
21
24
|
setLocaleCookie(locale);
|
|
22
|
-
const pathWithoutLocale =
|
|
23
|
-
!prefixDefault && currentLocale === defaultLocale
|
|
24
|
-
? pathname
|
|
25
|
-
: pathname.slice(`/${currentLocale}`.length) || "/";
|
|
25
|
+
const pathWithoutLocale = !prefixDefault && currentLocale.toString() === defaultLocale.toString() ? pathname : pathname.slice(`/${currentLocale}`.length) || "/";
|
|
26
26
|
if (!prefixDefault && locale === defaultLocale) {
|
|
27
27
|
return router.push(pathWithoutLocale);
|
|
28
28
|
}
|
|
@@ -30,8 +30,10 @@ const useLocale = () => {
|
|
|
30
30
|
};
|
|
31
31
|
return {
|
|
32
32
|
...reactLocaleHook,
|
|
33
|
-
setLocale
|
|
33
|
+
setLocale
|
|
34
34
|
};
|
|
35
35
|
};
|
|
36
|
-
export {
|
|
37
|
-
|
|
36
|
+
export {
|
|
37
|
+
useLocale
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=useLocale.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/client/useLocale.ts"],"sourcesContent":["import { intlayerMiddlewareConfiguration
|
|
1
|
+
{"version":3,"sources":["../../../src/client/useLocale.ts"],"sourcesContent":["import {\n type Locales,\n intlayerMiddlewareConfiguration,\n} from '@intlayer/config/client';\nimport { usePathname, useRouter } from 'next/navigation.js';\nimport { useLocale as useReactLocale, useLocaleCookie } from 'react-intlayer';\n\nconst { prefixDefault } = intlayerMiddlewareConfiguration;\n\nexport const useLocale = () => {\n const { setLocaleCookie } = useLocaleCookie();\n const reactLocaleHook = useReactLocale();\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,EAEE;AAAA,OACK;AACP,SAAS,aAAa,iBAAiB;AACvC,SAAS,aAAa,gBAAgB,uBAAuB;AAE7D,MAAM,EAAE,cAAc,IAAI;AAEnB,MAAM,YAAY,MAAM;AAC7B,QAAM,EAAE,gBAAgB,IAAI,gBAAgB;AAC5C,QAAM,kBAAkB,eAAe;AACvC,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,7 +1,7 @@
|
|
|
1
|
-
import * as _intlayer_config_client from
|
|
1
|
+
import * as _intlayer_config_client from '@intlayer/config/client';
|
|
2
2
|
|
|
3
3
|
declare const generateStaticParams: () => {
|
|
4
|
-
|
|
4
|
+
locale: _intlayer_config_client.Locales;
|
|
5
5
|
}[];
|
|
6
6
|
|
|
7
7
|
export { generateStaticParams };
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { intlayerIntlConfiguration } from "@intlayer/config/client";
|
|
2
2
|
const { locales } = intlayerIntlConfiguration;
|
|
3
3
|
const generateStaticParams = () => locales.map((locale) => ({ locale }));
|
|
4
|
-
export {
|
|
5
|
-
|
|
4
|
+
export {
|
|
5
|
+
generateStaticParams
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=generateStaticParams.mjs.map
|
package/dist/esm/index.d.mts
CHANGED
|
@@ -1,15 +1,8 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export { generateStaticParams } from "./generateStaticParams.mjs";
|
|
10
|
-
export { LocalParams, NextPageIntlayer } from "./types/NextPage.mjs";
|
|
11
|
-
export { useLocale } from "./client/useLocale.mjs";
|
|
12
|
-
import "@intlayer/config/client";
|
|
13
|
-
import "intlayer";
|
|
14
|
-
import "next";
|
|
15
|
-
import "react";
|
|
1
|
+
export { IntlayerClient, IntlayerClientProvider, getTranslation, useIntlayer, useLocaleCookie, useTraduction } from 'react-intlayer';
|
|
2
|
+
export { generateStaticParams } from './generateStaticParams.mjs';
|
|
3
|
+
export { LocalParams, NextPageIntlayer } from './types/NextPage.mjs';
|
|
4
|
+
export { useLocale } from './client/useLocale.mjs';
|
|
5
|
+
import '@intlayer/config/client';
|
|
6
|
+
import 'intlayer';
|
|
7
|
+
import 'next';
|
|
8
|
+
import 'react';
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getTranslation,
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
IntlayerClientProvider,
|
|
4
|
+
IntlayerClient,
|
|
5
5
|
useIntlayer,
|
|
6
6
|
useTraduction,
|
|
7
|
-
useLocaleCookie
|
|
7
|
+
useLocaleCookie
|
|
8
8
|
} from "react-intlayer";
|
|
9
|
-
import { generateStaticParams } from
|
|
10
|
-
import { useLocale } from
|
|
9
|
+
import { generateStaticParams } from './generateStaticParams.mjs';
|
|
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,
|
|
17
17
|
useLocale,
|
|
18
18
|
useLocaleCookie,
|
|
19
|
-
useTraduction
|
|
19
|
+
useTraduction
|
|
20
20
|
};
|
|
21
|
-
//# sourceMappingURL=index.mjs.map
|
|
21
|
+
//# sourceMappingURL=index.mjs.map
|
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,2 +1,2 @@
|
|
|
1
|
-
export { intlayerMiddleware } from
|
|
2
|
-
import
|
|
1
|
+
export { intlayerMiddleware } from './intlayerMiddleware.mjs';
|
|
2
|
+
import 'next/server';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
//# sourceMappingURL=index.mjs.map
|
|
1
|
+
export * from './intlayerMiddleware.mjs';
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,5 +1,20 @@
|
|
|
1
|
-
import { NextRequest, NextResponse } from
|
|
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,6 +1,6 @@
|
|
|
1
1
|
import { intlayerConfiguration } from "@intlayer/config/client";
|
|
2
2
|
import { NextResponse } from "next/server";
|
|
3
|
-
import { localeDetector } from
|
|
3
|
+
import { localeDetector } from './localeDetector.mjs';
|
|
4
4
|
const { internationalization, middleware } = intlayerConfiguration;
|
|
5
5
|
const { locales, defaultLocale } = internationalization;
|
|
6
6
|
const {
|
|
@@ -9,7 +9,7 @@ const {
|
|
|
9
9
|
prefixDefault,
|
|
10
10
|
basePath,
|
|
11
11
|
serverSetCookie,
|
|
12
|
-
noPrefix
|
|
12
|
+
noPrefix
|
|
13
13
|
} = middleware;
|
|
14
14
|
const intlayerMiddleware = (request) => {
|
|
15
15
|
const pathname = request.nextUrl.pathname;
|
|
@@ -33,18 +33,14 @@ const intlayerMiddleware = (request) => {
|
|
|
33
33
|
);
|
|
34
34
|
};
|
|
35
35
|
const getCookieLocale = (request) => {
|
|
36
|
-
if (!cookieName)
|
|
36
|
+
if (!cookieName)
|
|
37
|
+
return void 0;
|
|
37
38
|
const cookieValue = request.cookies.get(cookieName)?.value;
|
|
38
39
|
if (cookieValue && locales.includes(cookieValue)) {
|
|
39
40
|
return cookieValue;
|
|
40
41
|
}
|
|
41
42
|
};
|
|
42
|
-
const handleNoPrefix = (
|
|
43
|
-
request,
|
|
44
|
-
cookieLocale,
|
|
45
|
-
pathname,
|
|
46
|
-
basePathTrailingSlash
|
|
47
|
-
) => {
|
|
43
|
+
const handleNoPrefix = (request, cookieLocale, pathname, basePathTrailingSlash) => {
|
|
48
44
|
const locale = cookieLocale ?? defaultLocale;
|
|
49
45
|
const newPath = constructPath(
|
|
50
46
|
locale,
|
|
@@ -54,17 +50,10 @@ const handleNoPrefix = (
|
|
|
54
50
|
);
|
|
55
51
|
return rewriteUrl(request, newPath, locale);
|
|
56
52
|
};
|
|
57
|
-
const getPathLocale = (pathname) =>
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const handlePrefix = (
|
|
62
|
-
request,
|
|
63
|
-
cookieLocale,
|
|
64
|
-
pathLocale,
|
|
65
|
-
pathname,
|
|
66
|
-
basePathTrailingSlash
|
|
67
|
-
) => {
|
|
53
|
+
const getPathLocale = (pathname) => locales.find(
|
|
54
|
+
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
|
|
55
|
+
);
|
|
56
|
+
const handlePrefix = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => {
|
|
68
57
|
if (!pathLocale) {
|
|
69
58
|
return handleMissingPathLocale(
|
|
70
59
|
request,
|
|
@@ -81,12 +70,7 @@ const handlePrefix = (
|
|
|
81
70
|
basePathTrailingSlash
|
|
82
71
|
);
|
|
83
72
|
};
|
|
84
|
-
const handleMissingPathLocale = (
|
|
85
|
-
request,
|
|
86
|
-
cookieLocale,
|
|
87
|
-
pathname,
|
|
88
|
-
basePathTrailingSlash
|
|
89
|
-
) => {
|
|
73
|
+
const handleMissingPathLocale = (request, cookieLocale, pathname, basePathTrailingSlash) => {
|
|
90
74
|
let locale = cookieLocale ?? localeDetector?.(request) ?? defaultLocale;
|
|
91
75
|
if (!locales.includes(locale)) {
|
|
92
76
|
console.warn(
|
|
@@ -100,22 +84,10 @@ const handleMissingPathLocale = (
|
|
|
100
84
|
basePath,
|
|
101
85
|
basePathTrailingSlash
|
|
102
86
|
);
|
|
103
|
-
return prefixDefault || locale !== defaultLocale
|
|
104
|
-
? redirectUrl(request, newPath)
|
|
105
|
-
: rewriteUrl(request, newPath, locale);
|
|
87
|
+
return prefixDefault || locale !== defaultLocale ? redirectUrl(request, newPath) : rewriteUrl(request, newPath, locale);
|
|
106
88
|
};
|
|
107
|
-
const handleExistingPathLocale = (
|
|
108
|
-
|
|
109
|
-
cookieLocale,
|
|
110
|
-
pathLocale,
|
|
111
|
-
pathname,
|
|
112
|
-
basePathTrailingSlash
|
|
113
|
-
) => {
|
|
114
|
-
if (
|
|
115
|
-
cookieLocale &&
|
|
116
|
-
cookieLocale !== pathLocale &&
|
|
117
|
-
serverSetCookie !== "always"
|
|
118
|
-
) {
|
|
89
|
+
const handleExistingPathLocale = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => {
|
|
90
|
+
if (cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always") {
|
|
119
91
|
const newPath = handleCookieLocaleMismatch(
|
|
120
92
|
request,
|
|
121
93
|
pathname,
|
|
@@ -133,14 +105,7 @@ const handleExistingPathLocale = (
|
|
|
133
105
|
basePathTrailingSlash
|
|
134
106
|
);
|
|
135
107
|
};
|
|
136
|
-
const handleCookieLocaleMismatch = (
|
|
137
|
-
request,
|
|
138
|
-
pathname,
|
|
139
|
-
pathLocale,
|
|
140
|
-
cookieLocale,
|
|
141
|
-
basePath2,
|
|
142
|
-
basePathTrailingSlash
|
|
143
|
-
) => {
|
|
108
|
+
const handleCookieLocaleMismatch = (request, pathname, pathLocale, cookieLocale, basePath2, basePathTrailingSlash) => {
|
|
144
109
|
const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);
|
|
145
110
|
return constructPath(
|
|
146
111
|
cookieLocale,
|
|
@@ -150,12 +115,7 @@ const handleCookieLocaleMismatch = (
|
|
|
150
115
|
request.nextUrl.search
|
|
151
116
|
);
|
|
152
117
|
};
|
|
153
|
-
const handleDefaultLocaleRedirect = (
|
|
154
|
-
request,
|
|
155
|
-
pathLocale,
|
|
156
|
-
pathname,
|
|
157
|
-
basePathTrailingSlash
|
|
158
|
-
) => {
|
|
118
|
+
const handleDefaultLocaleRedirect = (request, pathLocale, pathname, basePathTrailingSlash) => {
|
|
159
119
|
if (!prefixDefault && pathLocale === defaultLocale) {
|
|
160
120
|
let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || "/";
|
|
161
121
|
if (basePathTrailingSlash) {
|
|
@@ -168,13 +128,7 @@ const handleDefaultLocaleRedirect = (
|
|
|
168
128
|
}
|
|
169
129
|
return rewriteUrl(request, pathname, pathLocale);
|
|
170
130
|
};
|
|
171
|
-
const constructPath = (
|
|
172
|
-
locale,
|
|
173
|
-
path,
|
|
174
|
-
basePath2,
|
|
175
|
-
basePathTrailingSlash,
|
|
176
|
-
search
|
|
177
|
-
) => {
|
|
131
|
+
const constructPath = (locale, path, basePath2, basePathTrailingSlash, search) => {
|
|
178
132
|
let newPath = `${locale}${path}`;
|
|
179
133
|
newPath = `${basePath2}${basePathTrailingSlash ? "" : "/"}${newPath}`;
|
|
180
134
|
if (search) {
|
|
@@ -187,7 +141,8 @@ const rewriteUrl = (request, newPath, locale) => {
|
|
|
187
141
|
response.headers.set(headerName, locale);
|
|
188
142
|
return response;
|
|
189
143
|
};
|
|
190
|
-
const redirectUrl = (request, newPath) =>
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
144
|
+
const redirectUrl = (request, newPath) => NextResponse.redirect(new URL(newPath, request.url));
|
|
145
|
+
export {
|
|
146
|
+
intlayerMiddleware
|
|
147
|
+
};
|
|
148
|
+
//# sourceMappingURL=intlayerMiddleware.mjs.map
|
|
@@ -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
|
-
import { Locales } from
|
|
2
|
-
import { NextRequest } from
|
|
1
|
+
import { Locales } from '@intlayer/config';
|
|
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 };
|
|
@@ -4,7 +4,7 @@ import Negotiator from "negotiator";
|
|
|
4
4
|
const { locales, defaultLocale } = getConfiguration().internationalization;
|
|
5
5
|
const localeDetector = (request) => {
|
|
6
6
|
const negotiatorHeaders = {};
|
|
7
|
-
request.headers.forEach((value, key) =>
|
|
7
|
+
request.headers.forEach((value, key) => negotiatorHeaders[key] = value);
|
|
8
8
|
const languages = new Negotiator({ headers: negotiatorHeaders }).languages();
|
|
9
9
|
try {
|
|
10
10
|
return match(languages, locales, defaultLocale);
|
|
@@ -16,5 +16,7 @@ const localeDetector = (request) => {
|
|
|
16
16
|
return defaultLocale;
|
|
17
17
|
}
|
|
18
18
|
};
|
|
19
|
-
export {
|
|
20
|
-
|
|
19
|
+
export {
|
|
20
|
+
localeDetector
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=localeDetector.mjs.map
|
|
@@ -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,10 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
getLocaleContent,
|
|
5
|
-
locale,
|
|
6
|
-
useIntlayer,
|
|
7
|
-
useTraduction,
|
|
8
|
-
} from "react-intlayer/server";
|
|
9
|
-
export { withIntlayer } from "./withIntlayer.mjs";
|
|
10
|
-
import "next";
|
|
1
|
+
export { IntlayerServer, IntlayerServerProvider, getLocaleContent, locale, useIntlayer, useTraduction } from 'react-intlayer/server';
|
|
2
|
+
export { withIntlayer } from './withIntlayer.mjs';
|
|
3
|
+
import 'next';
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getLocaleContent,
|
|
3
3
|
useTraduction,
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
IntlayerServer,
|
|
5
|
+
IntlayerServerProvider,
|
|
6
6
|
locale,
|
|
7
|
-
useIntlayer
|
|
7
|
+
useIntlayer
|
|
8
8
|
} from "react-intlayer/server";
|
|
9
|
-
import { withIntlayer } from
|
|
9
|
+
import { withIntlayer } from './withIntlayer.mjs';
|
|
10
10
|
export {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
IntlayerServer,
|
|
12
|
+
IntlayerServerProvider,
|
|
13
13
|
getLocaleContent,
|
|
14
14
|
locale,
|
|
15
15
|
useIntlayer,
|
|
16
16
|
useTraduction,
|
|
17
|
-
withIntlayer
|
|
17
|
+
withIntlayer
|
|
18
18
|
};
|
|
19
|
-
//# sourceMappingURL=index.mjs.map
|
|
19
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -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":[]}
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { NextConfig } from
|
|
1
|
+
import 'react-intlayer/server';
|
|
2
|
+
import { NextConfig } from 'next';
|
|
3
3
|
|
|
4
4
|
type PluginOptions = {};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
+
*/
|
|
15
|
+
declare const withIntlayer: (_pluginOptions?: PluginOptions) => (nextConfig?: Partial<NextConfig>) => Partial<NextConfig>;
|
|
8
16
|
|
|
9
17
|
export { withIntlayer };
|