next-intl 2.22.0 → 2.22.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/config.d.ts +3 -0
- package/dist/config.js +7 -0
- package/dist/development/_virtual/_rollupPluginBabelHelpers.js +20 -0
- package/dist/development/config.js +9 -0
- package/dist/development/index.js +21 -0
- package/dist/development/index.react-server.js +28 -0
- package/dist/development/middleware/getAlternateLinksHeaderValue.js +81 -0
- package/dist/development/middleware/middleware.js +146 -0
- package/dist/development/middleware/resolveLocale.js +110 -0
- package/dist/development/middleware/utils.js +150 -0
- package/dist/development/middleware.js +9 -0
- package/dist/development/navigation/BaseLink.js +52 -0
- package/dist/development/navigation/baseRedirect.js +22 -0
- package/dist/development/navigation/createLocalizedPathnamesNavigation.js +135 -0
- package/dist/development/navigation/createSharedPathnamesNavigation.js +19 -0
- package/dist/development/navigation/react-server/BaseLink.js +25 -0
- package/dist/development/navigation/react-server/baseRedirect.js +16 -0
- package/dist/development/navigation/react-server/createLocalizedPathnamesNavigation.js +77 -0
- package/dist/development/navigation/react-server/createSharedPathnamesNavigation.js +22 -0
- package/dist/development/navigation/useBasePathname.js +37 -0
- package/dist/development/navigation/useBaseRouter.js +76 -0
- package/dist/development/navigation/utils.js +108 -0
- package/dist/development/navigation.js +11 -0
- package/dist/development/navigation.react-server.js +11 -0
- package/dist/development/plugin.js +48 -0
- package/dist/development/react-client/index.js +38 -0
- package/dist/development/react-client/useLocale.js +24 -0
- package/dist/development/react-server/NextIntlClientProvider.js +33 -0
- package/dist/development/react-server/getBaseTranslator.js +21 -0
- package/dist/development/react-server/useFormatter.js +19 -0
- package/dist/development/react-server/useHook.js +21 -0
- package/dist/development/react-server/useLocale.js +14 -0
- package/dist/development/react-server/useMessages.js +19 -0
- package/dist/development/react-server/useNow.js +23 -0
- package/dist/development/react-server/useTimeZone.js +19 -0
- package/dist/development/react-server/useTranslations.js +19 -0
- package/dist/development/server/RequestLocale.js +43 -0
- package/dist/development/server/createRequestConfig.js +17 -0
- package/dist/development/server/getConfig.js +43 -0
- package/dist/development/server/getFormatter.js +26 -0
- package/dist/development/server/getLocale.js +11 -0
- package/dist/development/server/getMessages.js +21 -0
- package/dist/development/server/getNow.js +18 -0
- package/dist/development/server/getRequestConfig.js +12 -0
- package/dist/development/server/getTimeZone.js +18 -0
- package/dist/development/server/getTranslations.js +31 -0
- package/dist/development/server/resolveLocaleArg.js +15 -0
- package/dist/development/server.js +23 -0
- package/dist/development/shared/BaseLinkWithLocale.js +58 -0
- package/dist/development/shared/NextIntlClientProvider.js +30 -0
- package/dist/development/shared/constants.js +17 -0
- package/dist/development/shared/redirectWithLocale.js +16 -0
- package/dist/development/shared/utils.js +87 -0
- package/dist/esm/_virtual/_rollupPluginBabelHelpers.js +1 -0
- package/dist/esm/config.js +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.react-server.js +1 -0
- package/dist/esm/middleware/getAlternateLinksHeaderValue.js +1 -0
- package/dist/esm/middleware/middleware.js +1 -0
- package/dist/esm/middleware/resolveLocale.js +1 -0
- package/dist/esm/middleware/utils.js +1 -0
- package/dist/esm/middleware.js +1 -0
- package/dist/esm/navigation/BaseLink.js +1 -0
- package/dist/esm/navigation/baseRedirect.js +1 -0
- package/dist/esm/navigation/createLocalizedPathnamesNavigation.js +1 -0
- package/dist/esm/navigation/createSharedPathnamesNavigation.js +1 -0
- package/dist/esm/navigation/react-server/BaseLink.js +1 -0
- package/dist/esm/navigation/react-server/baseRedirect.js +1 -0
- package/dist/esm/navigation/react-server/createLocalizedPathnamesNavigation.js +1 -0
- package/dist/esm/navigation/react-server/createSharedPathnamesNavigation.js +1 -0
- package/dist/esm/navigation/useBasePathname.js +2 -0
- package/dist/esm/navigation/useBaseRouter.js +1 -0
- package/dist/esm/navigation/utils.js +1 -0
- package/dist/esm/navigation.js +1 -0
- package/dist/esm/navigation.react-server.js +1 -0
- package/dist/esm/plugin.js +1 -0
- package/dist/esm/react-client/index.js +1 -0
- package/dist/esm/react-client/useLocale.js +1 -0
- package/dist/esm/react-server/NextIntlClientProvider.js +1 -0
- package/dist/esm/react-server/getBaseTranslator.js +1 -0
- package/dist/esm/react-server/useFormatter.js +1 -0
- package/dist/esm/react-server/useHook.js +1 -0
- package/dist/esm/react-server/useLocale.js +1 -0
- package/dist/esm/react-server/useMessages.js +1 -0
- package/dist/esm/react-server/useNow.js +1 -0
- package/dist/esm/react-server/useTimeZone.js +1 -0
- package/dist/esm/react-server/useTranslations.js +1 -0
- package/dist/esm/server/RequestLocale.js +1 -0
- package/dist/esm/server/createRequestConfig.js +1 -0
- package/dist/esm/server/getConfig.js +1 -0
- package/dist/esm/server/getFormatter.js +1 -0
- package/dist/esm/server/getLocale.js +1 -0
- package/dist/esm/server/getMessages.js +1 -0
- package/dist/esm/server/getNow.js +1 -0
- package/dist/esm/server/getRequestConfig.js +1 -0
- package/dist/esm/server/getTimeZone.js +1 -0
- package/dist/esm/server/getTranslations.js +1 -0
- package/dist/esm/server/resolveLocaleArg.js +1 -0
- package/dist/esm/server.js +1 -0
- package/dist/esm/shared/BaseLinkWithLocale.js +2 -0
- package/dist/esm/shared/NextIntlClientProvider.js +2 -0
- package/dist/esm/shared/constants.js +1 -0
- package/dist/esm/shared/redirectWithLocale.js +1 -0
- package/dist/esm/shared/utils.js +1 -0
- package/dist/index.js +3 -4
- package/dist/index.react-server.js +7 -0
- package/dist/middleware.js +7 -0
- package/dist/navigation.js +7 -0
- package/dist/navigation.react-server.js +7 -0
- package/dist/plugin.js +7 -0
- package/dist/production/_virtual/_rollupPluginBabelHelpers.js +1 -0
- package/dist/production/config.js +1 -0
- package/dist/production/index.js +1 -0
- package/dist/production/index.react-server.js +1 -0
- package/dist/production/middleware/getAlternateLinksHeaderValue.js +1 -0
- package/dist/production/middleware/middleware.js +1 -0
- package/dist/production/middleware/resolveLocale.js +1 -0
- package/dist/production/middleware/utils.js +1 -0
- package/dist/production/middleware.js +1 -0
- package/dist/production/navigation/BaseLink.js +1 -0
- package/dist/production/navigation/baseRedirect.js +1 -0
- package/dist/production/navigation/createLocalizedPathnamesNavigation.js +1 -0
- package/dist/production/navigation/createSharedPathnamesNavigation.js +1 -0
- package/dist/production/navigation/react-server/BaseLink.js +1 -0
- package/dist/production/navigation/react-server/baseRedirect.js +1 -0
- package/dist/production/navigation/react-server/createLocalizedPathnamesNavigation.js +1 -0
- package/dist/production/navigation/react-server/createSharedPathnamesNavigation.js +1 -0
- package/dist/production/navigation/useBasePathname.js +2 -0
- package/dist/production/navigation/useBaseRouter.js +1 -0
- package/dist/production/navigation/utils.js +1 -0
- package/dist/production/navigation.js +1 -0
- package/dist/production/navigation.react-server.js +1 -0
- package/dist/production/plugin.js +1 -0
- package/dist/production/react-client/index.js +1 -0
- package/dist/production/react-client/useLocale.js +1 -0
- package/dist/production/react-server/NextIntlClientProvider.js +1 -0
- package/dist/production/react-server/getBaseTranslator.js +1 -0
- package/dist/production/react-server/useFormatter.js +1 -0
- package/dist/production/react-server/useHook.js +1 -0
- package/dist/production/react-server/useLocale.js +1 -0
- package/dist/production/react-server/useMessages.js +1 -0
- package/dist/production/react-server/useNow.js +1 -0
- package/dist/production/react-server/useTimeZone.js +1 -0
- package/dist/production/react-server/useTranslations.js +1 -0
- package/dist/production/server/RequestLocale.js +1 -0
- package/dist/production/server/createRequestConfig.js +1 -0
- package/dist/production/server/getConfig.js +1 -0
- package/dist/production/server/getFormatter.js +1 -0
- package/dist/production/server/getLocale.js +1 -0
- package/dist/production/server/getMessages.js +1 -0
- package/dist/production/server/getNow.js +1 -0
- package/dist/production/server/getRequestConfig.js +1 -0
- package/dist/production/server/getTimeZone.js +1 -0
- package/dist/production/server/getTranslations.js +1 -0
- package/dist/production/server/resolveLocaleArg.js +1 -0
- package/dist/production/server.js +1 -0
- package/dist/production/shared/BaseLinkWithLocale.js +2 -0
- package/dist/production/shared/NextIntlClientProvider.js +2 -0
- package/dist/production/shared/constants.js +1 -0
- package/dist/production/shared/redirectWithLocale.js +1 -0
- package/dist/production/shared/utils.js +1 -0
- package/dist/server.js +7 -0
- package/dist/types/src/config.d.ts +1 -0
- package/dist/types/src/index.react-server.d.ts +1 -0
- package/dist/{src → types/src}/middleware/NextIntlMiddlewareConfig.d.ts +14 -10
- package/dist/types/src/middleware/getAlternateLinksHeaderValue.d.ts +12 -0
- package/dist/types/src/middleware/middleware.d.ts +4 -0
- package/dist/types/src/middleware/resolveLocale.d.ts +7 -0
- package/dist/types/src/middleware/utils.d.ts +17 -0
- package/dist/types/src/middleware.d.ts +1 -0
- package/dist/types/src/navigation/BaseLink.d.ts +63 -0
- package/dist/types/src/navigation/StrictParams.d.ts +8 -0
- package/dist/types/src/navigation/baseRedirect.d.ts +3 -0
- package/dist/types/src/navigation/createLocalizedPathnamesNavigation.d.ts +78 -0
- package/dist/types/src/navigation/createSharedPathnamesNavigation.d.ts +13 -0
- package/dist/types/src/navigation/index.d.ts +3 -0
- package/dist/types/src/navigation/react-server/BaseLink.d.ts +8 -0
- package/dist/types/src/navigation/react-server/baseRedirect.d.ts +3 -0
- package/dist/types/src/navigation/react-server/createLocalizedPathnamesNavigation.d.ts +60 -0
- package/dist/types/src/navigation/react-server/createSharedPathnamesNavigation.d.ts +44 -0
- package/dist/types/src/navigation/react-server/index.d.ts +3 -0
- package/dist/{src/client/usePathname.d.ts → types/src/navigation/useBasePathname.d.ts} +1 -1
- package/dist/{client/useRouter.d.ts → types/src/navigation/useBaseRouter.d.ts} +32 -31
- package/dist/types/src/navigation/utils.d.ts +51 -0
- package/dist/types/src/navigation.d.ts +1 -0
- package/dist/types/src/navigation.react-server.d.ts +1 -0
- package/dist/types/src/plugin.d.ts +1 -0
- package/dist/{react-client → types/src/react-client}/index.d.ts +15 -12
- package/dist/types/src/react-client/useLocale.d.ts +1 -0
- package/dist/types/src/react-server/NextIntlClientProvider.d.ts +5 -0
- package/dist/types/src/react-server/getBaseTranslator.d.ts +42 -0
- package/dist/types/src/react-server/index.d.ts +15 -0
- package/dist/types/src/react-server/useFormatter.d.ts +2 -0
- package/dist/types/src/react-server/useHook.d.ts +1 -0
- package/dist/types/src/react-server/useLocale.d.ts +2 -0
- package/dist/types/src/react-server/useMessages.d.ts +2 -0
- package/dist/types/src/react-server/useNow.d.ts +2 -0
- package/dist/types/src/react-server/useTimeZone.d.ts +2 -0
- package/dist/types/src/react-server/useTranslations.d.ts +2 -0
- package/dist/types/src/server/RequestLocale.d.ts +2 -0
- package/dist/types/src/server/createRequestConfig.d.ts +4 -0
- package/dist/types/src/server/getConfig.d.ts +23 -0
- package/dist/types/src/server/getFormatter.d.ts +14 -0
- package/dist/types/src/server/getLocale.d.ts +1 -0
- package/dist/types/src/server/getMessages.d.ts +3 -0
- package/dist/types/src/server/getNow.d.ts +3 -0
- package/dist/types/src/server/getRequestConfig.d.ts +10 -0
- package/dist/types/src/server/getTimeZone.d.ts +3 -0
- package/dist/types/src/server/getTranslations.d.ts +83 -0
- package/dist/types/src/server/index.d.ts +11 -0
- package/dist/types/src/server/resolveLocaleArg.d.ts +3 -0
- package/dist/types/src/server.d.ts +1 -0
- package/dist/types/src/shared/NextIntlClientProvider.d.ts +8 -0
- package/dist/{shared → types/src/shared}/constants.d.ts +3 -2
- package/dist/types/src/shared/redirectWithLocale.d.ts +3 -0
- package/dist/types/src/shared/types.d.ts +6 -0
- package/dist/{src → types/src}/shared/utils.d.ts +6 -0
- package/dist/types/test/middleware/getAlternateLinksHeaderValue.test.d.ts +1 -0
- package/dist/types/test/middleware/middleware.test.d.ts +1 -0
- package/dist/types/test/middleware/utils.test.d.ts +1 -0
- package/dist/types/test/navigation/BaseLink.test.d.ts +1 -0
- package/dist/types/test/navigation/createLocalizedPathnamesNavigation.test.d.ts +1 -0
- package/dist/types/test/navigation/createSharedPathnamesNavigation.test.d.ts +1 -0
- package/dist/types/test/navigation/useBasePathname.test.d.ts +1 -0
- package/dist/types/test/navigation/useBaseRouter.test.d.ts +1 -0
- package/dist/types/test/navigation/utils.test.d.ts +1 -0
- package/dist/types/test/react-client/useFormatter.test.d.ts +1 -0
- package/dist/types/test/react-client/useLocale.test.d.ts +1 -0
- package/dist/types/test/react-client/useNow.test.d.ts +1 -0
- package/dist/types/test/react-client/useTimeZone.test.d.ts +1 -0
- package/dist/types/test/react-client/useTranslations.test.d.ts +1 -0
- package/dist/types/test/server/index.test.d.ts +1 -0
- package/dist/types/test/setup.d.ts +1 -0
- package/dist/types/test/shared/NextIntlClientProvider.test.d.ts +1 -0
- package/dist/types/test/shared/utils.test.d.ts +1 -0
- package/middleware.d.ts +1 -1
- package/navigation.d.ts +1 -0
- package/package.json +45 -34
- package/plugin.d.ts +6 -0
- package/server.d.ts +1 -1
- package/client.d.ts +0 -1
- package/dist/_virtual/next-intl.esm.js +0 -29
- package/dist/_virtual/next-intl.esm.js.map +0 -1
- package/dist/client/NextIntlClientProvider.d.ts +0 -4
- package/dist/client/index.d.ts +0 -6
- package/dist/client/useClientLocale.d.ts +0 -1
- package/dist/client/usePathname.d.ts +0 -14
- package/dist/index.d.ts +0 -8
- package/dist/link/Link.d.ts +0 -27
- package/dist/link/index.d.ts +0 -1
- package/dist/link/react-server/Link.d.ts +0 -1
- package/dist/link/react-server/index.d.ts +0 -1
- package/dist/middleware/NextIntlMiddlewareConfig.d.ts +0 -28
- package/dist/middleware/getAlternateLinksHeaderValue.d.ts +0 -6
- package/dist/middleware/index.d.ts +0 -4
- package/dist/middleware/middleware.d.ts +0 -3
- package/dist/middleware/resolveLocale.d.ts +0 -6
- package/dist/middleware/utils.d.ts +0 -5
- package/dist/next-intl.cjs.development.js +0 -82
- package/dist/next-intl.cjs.development.js.map +0 -1
- package/dist/next-intl.cjs.production.min.js +0 -2
- package/dist/next-intl.cjs.production.min.js.map +0 -1
- package/dist/next-intl.esm.js +0 -3
- package/dist/next-intl.esm.js.map +0 -1
- package/dist/react-server/index.d.ts +0 -17
- package/dist/server/index.d.ts +0 -6
- package/dist/shared/BaseLink.d.ts +0 -7
- package/dist/shared/NextIntlClientProvider.d.ts +0 -9
- package/dist/shared/next-intl.esm.js +0 -41
- package/dist/shared/next-intl.esm.js.map +0 -1
- package/dist/shared/utils.d.ts +0 -15
- package/dist/src/client/NextIntlClientProvider.d.ts +0 -4
- package/dist/src/client/NextIntlClientProvider.js +0 -16
- package/dist/src/client/NextIntlClientProvider.js.map +0 -1
- package/dist/src/client/index.d.ts +0 -6
- package/dist/src/client/index.js +0 -8
- package/dist/src/client/index.js.map +0 -1
- package/dist/src/client/useClientLocale.d.ts +0 -1
- package/dist/src/client/useClientLocale.js +0 -18
- package/dist/src/client/useClientLocale.js.map +0 -1
- package/dist/src/client/usePathname.js +0 -34
- package/dist/src/client/usePathname.js.map +0 -1
- package/dist/src/client/useRouter.d.ts +0 -31
- package/dist/src/client/useRouter.js +0 -61
- package/dist/src/client/useRouter.js.map +0 -1
- package/dist/src/index.js +0 -9
- package/dist/src/index.js.map +0 -1
- package/dist/src/link/Link.d.ts +0 -27
- package/dist/src/link/Link.js +0 -29
- package/dist/src/link/Link.js.map +0 -1
- package/dist/src/link/index.d.ts +0 -1
- package/dist/src/link/index.js +0 -2
- package/dist/src/link/index.js.map +0 -1
- package/dist/src/link/react-server/Link.d.ts +0 -1
- package/dist/src/link/react-server/Link.js +0 -6
- package/dist/src/link/react-server/Link.js.map +0 -1
- package/dist/src/link/react-server/index.d.ts +0 -1
- package/dist/src/link/react-server/index.js +0 -2
- package/dist/src/link/react-server/index.js.map +0 -1
- package/dist/src/middleware/NextIntlMiddlewareConfig.js +0 -2
- package/dist/src/middleware/NextIntlMiddlewareConfig.js.map +0 -1
- package/dist/src/middleware/getAlternateLinksHeaderValue.d.ts +0 -6
- package/dist/src/middleware/getAlternateLinksHeaderValue.js +0 -71
- package/dist/src/middleware/getAlternateLinksHeaderValue.js.map +0 -1
- package/dist/src/middleware/index.js +0 -5
- package/dist/src/middleware/index.js.map +0 -1
- package/dist/src/middleware/middleware.d.ts +0 -3
- package/dist/src/middleware/middleware.js +0 -149
- package/dist/src/middleware/middleware.js.map +0 -1
- package/dist/src/middleware/resolveLocale.d.ts +0 -6
- package/dist/src/middleware/resolveLocale.js +0 -88
- package/dist/src/middleware/resolveLocale.js.map +0 -1
- package/dist/src/middleware/utils.d.ts +0 -5
- package/dist/src/middleware/utils.js +0 -37
- package/dist/src/middleware/utils.js.map +0 -1
- package/dist/src/react-client/index.d.ts +0 -12
- package/dist/src/react-client/index.js +0 -14
- package/dist/src/react-client/index.js.map +0 -1
- package/dist/src/react-server/index.d.ts +0 -17
- package/dist/src/react-server/index.js +0 -25
- package/dist/src/react-server/index.js.map +0 -1
- package/dist/src/server/index.d.ts +0 -6
- package/dist/src/server/index.js +0 -30
- package/dist/src/server/index.js.map +0 -1
- package/dist/src/shared/BaseLink.js +0 -38
- package/dist/src/shared/BaseLink.js.map +0 -1
- package/dist/src/shared/NextIntlClientProvider.d.ts +0 -9
- package/dist/src/shared/NextIntlClientProvider.js +0 -33
- package/dist/src/shared/NextIntlClientProvider.js.map +0 -1
- package/dist/src/shared/constants.d.ts +0 -2
- package/dist/src/shared/constants.js +0 -6
- package/dist/src/shared/constants.js.map +0 -1
- package/dist/src/shared/utils.js +0 -53
- package/dist/src/shared/utils.js.map +0 -1
- package/link.d.ts +0 -3
- package/src/client/NextIntlClientProvider.tsx +0 -18
- package/src/client/index.tsx +0 -9
- package/src/client/useClientLocale.tsx +0 -20
- package/src/client/usePathname.tsx +0 -40
- package/src/client/useRouter.tsx +0 -92
- package/src/index.tsx +0 -9
- package/src/link/Link.tsx +0 -34
- package/src/link/index.tsx +0 -1
- package/src/link/react-server/Link.tsx +0 -6
- package/src/link/react-server/index.tsx +0 -1
- package/src/middleware/NextIntlMiddlewareConfig.tsx +0 -42
- package/src/middleware/getAlternateLinksHeaderValue.tsx +0 -97
- package/src/middleware/index.tsx +0 -5
- package/src/middleware/middleware.tsx +0 -206
- package/src/middleware/resolveLocale.tsx +0 -151
- package/src/middleware/utils.tsx +0 -61
- package/src/react-client/index.tsx +0 -15
- package/src/react-server/index.tsx +0 -29
- package/src/server/index.tsx +0 -34
- package/src/shared/BaseLink.tsx +0 -57
- package/src/shared/NextIntlClientProvider.tsx +0 -54
- package/src/shared/constants.tsx +0 -6
- package/src/shared/utils.tsx +0 -91
- /package/dist/{src → types/src}/index.d.ts +0 -0
- /package/dist/{src → types/src}/middleware/index.d.ts +0 -0
- /package/dist/{src/shared/BaseLink.d.ts → types/src/shared/BaseLinkWithLocale.d.ts} +0 -0
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import {NextRequest, NextResponse} from 'next/server';
|
|
2
|
-
import {COOKIE_LOCALE_NAME} from '../shared/constants';
|
|
3
|
-
import MiddlewareConfig, {
|
|
4
|
-
MiddlewareConfigWithDefaults
|
|
5
|
-
} from './NextIntlMiddlewareConfig';
|
|
6
|
-
import getAlternateLinksHeaderValue from './getAlternateLinksHeaderValue';
|
|
7
|
-
import resolveLocale from './resolveLocale';
|
|
8
|
-
import {
|
|
9
|
-
getBestMatchingDomain,
|
|
10
|
-
getLocaleFromPathname,
|
|
11
|
-
isLocaleSupportedOnDomain
|
|
12
|
-
} from './utils';
|
|
13
|
-
|
|
14
|
-
const ROOT_URL = '/';
|
|
15
|
-
|
|
16
|
-
function receiveConfig(config: MiddlewareConfig) {
|
|
17
|
-
const result: MiddlewareConfigWithDefaults = {
|
|
18
|
-
...config,
|
|
19
|
-
alternateLinks: config.alternateLinks ?? true,
|
|
20
|
-
localePrefix: config.localePrefix ?? 'as-needed',
|
|
21
|
-
localeDetection: config.localeDetection ?? true
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
return result;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export default function createMiddleware(config: MiddlewareConfig) {
|
|
28
|
-
const configWithDefaults = receiveConfig(config);
|
|
29
|
-
|
|
30
|
-
// Currently only in use to enable a seamless upgrade path from the
|
|
31
|
-
// `{createIntlMiddleware} from 'next-intl/server'` API.
|
|
32
|
-
const matcher: Array<string> | undefined = (config as any)._matcher;
|
|
33
|
-
|
|
34
|
-
return function middleware(request: NextRequest) {
|
|
35
|
-
const matches =
|
|
36
|
-
!matcher ||
|
|
37
|
-
matcher.some((pattern) => request.nextUrl.pathname.match(pattern));
|
|
38
|
-
if (!matches) return NextResponse.next();
|
|
39
|
-
|
|
40
|
-
const {domain, locale} = resolveLocale(
|
|
41
|
-
configWithDefaults,
|
|
42
|
-
request.headers,
|
|
43
|
-
request.cookies,
|
|
44
|
-
request.nextUrl.pathname
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
const isRoot = request.nextUrl.pathname === ROOT_URL;
|
|
48
|
-
const hasOutdatedCookie =
|
|
49
|
-
request.cookies.get(COOKIE_LOCALE_NAME)?.value !== locale;
|
|
50
|
-
const hasMatchedDefaultLocale = domain
|
|
51
|
-
? domain.defaultLocale === locale
|
|
52
|
-
: locale === configWithDefaults.defaultLocale;
|
|
53
|
-
|
|
54
|
-
const domainConfigs =
|
|
55
|
-
configWithDefaults.domains?.filter((curDomain) =>
|
|
56
|
-
isLocaleSupportedOnDomain(locale, curDomain)
|
|
57
|
-
) || [];
|
|
58
|
-
const hasUnknownHost = configWithDefaults.domains != null && !domain;
|
|
59
|
-
|
|
60
|
-
function getResponseInit() {
|
|
61
|
-
const responseInit = {
|
|
62
|
-
request: {
|
|
63
|
-
headers: request.headers
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
return responseInit;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function rewrite(url: string) {
|
|
71
|
-
return NextResponse.rewrite(new URL(url, request.url), getResponseInit());
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function next() {
|
|
75
|
-
return NextResponse.next(getResponseInit());
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function redirect(url: string, host?: string) {
|
|
79
|
-
const urlObj = new URL(url, request.url);
|
|
80
|
-
|
|
81
|
-
if (domainConfigs.length > 0) {
|
|
82
|
-
if (!host) {
|
|
83
|
-
const bestMatchingDomain = getBestMatchingDomain(
|
|
84
|
-
domain,
|
|
85
|
-
locale,
|
|
86
|
-
domainConfigs
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
if (bestMatchingDomain) {
|
|
90
|
-
host = bestMatchingDomain.domain;
|
|
91
|
-
|
|
92
|
-
if (
|
|
93
|
-
bestMatchingDomain.defaultLocale === locale &&
|
|
94
|
-
configWithDefaults.localePrefix === 'as-needed'
|
|
95
|
-
) {
|
|
96
|
-
urlObj.pathname = urlObj.pathname.replace(`/${locale}`, '');
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (host) {
|
|
103
|
-
urlObj.host = host;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return NextResponse.redirect(urlObj.toString());
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
let response;
|
|
110
|
-
if (isRoot) {
|
|
111
|
-
let pathWithSearch = `/${locale}`;
|
|
112
|
-
if (request.nextUrl.search) {
|
|
113
|
-
pathWithSearch += request.nextUrl.search;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (
|
|
117
|
-
configWithDefaults.localePrefix === 'never' ||
|
|
118
|
-
(hasMatchedDefaultLocale &&
|
|
119
|
-
configWithDefaults.localePrefix === 'as-needed')
|
|
120
|
-
) {
|
|
121
|
-
response = rewrite(pathWithSearch);
|
|
122
|
-
} else {
|
|
123
|
-
response = redirect(pathWithSearch);
|
|
124
|
-
}
|
|
125
|
-
} else {
|
|
126
|
-
const pathLocaleCandidate = getLocaleFromPathname(
|
|
127
|
-
request.nextUrl.pathname
|
|
128
|
-
);
|
|
129
|
-
const pathLocale = configWithDefaults.locales.includes(
|
|
130
|
-
pathLocaleCandidate
|
|
131
|
-
)
|
|
132
|
-
? pathLocaleCandidate
|
|
133
|
-
: undefined;
|
|
134
|
-
const hasLocalePrefix = pathLocale != null;
|
|
135
|
-
|
|
136
|
-
let pathWithSearch = request.nextUrl.pathname;
|
|
137
|
-
if (request.nextUrl.search) {
|
|
138
|
-
pathWithSearch += request.nextUrl.search;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (hasLocalePrefix) {
|
|
142
|
-
const basePath = pathWithSearch.replace(`/${pathLocale}`, '') || '/';
|
|
143
|
-
|
|
144
|
-
if (configWithDefaults.localePrefix === 'never') {
|
|
145
|
-
response = redirect(basePath);
|
|
146
|
-
} else if (pathLocale === locale) {
|
|
147
|
-
if (
|
|
148
|
-
hasMatchedDefaultLocale &&
|
|
149
|
-
configWithDefaults.localePrefix === 'as-needed'
|
|
150
|
-
) {
|
|
151
|
-
response = redirect(basePath);
|
|
152
|
-
} else {
|
|
153
|
-
if (configWithDefaults.domains) {
|
|
154
|
-
const pathDomain = getBestMatchingDomain(
|
|
155
|
-
domain,
|
|
156
|
-
pathLocale,
|
|
157
|
-
domainConfigs
|
|
158
|
-
);
|
|
159
|
-
|
|
160
|
-
if (domain?.domain !== pathDomain?.domain && !hasUnknownHost) {
|
|
161
|
-
response = redirect(basePath, pathDomain?.domain);
|
|
162
|
-
} else {
|
|
163
|
-
response = next();
|
|
164
|
-
}
|
|
165
|
-
} else {
|
|
166
|
-
response = next();
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
} else {
|
|
170
|
-
response = redirect(`/${locale}${basePath}`);
|
|
171
|
-
}
|
|
172
|
-
} else {
|
|
173
|
-
if (
|
|
174
|
-
configWithDefaults.localePrefix === 'never' ||
|
|
175
|
-
(hasMatchedDefaultLocale &&
|
|
176
|
-
(configWithDefaults.localePrefix === 'as-needed' ||
|
|
177
|
-
configWithDefaults.domains))
|
|
178
|
-
) {
|
|
179
|
-
response = rewrite(`/${locale}${pathWithSearch}`);
|
|
180
|
-
} else {
|
|
181
|
-
response = redirect(`/${locale}${pathWithSearch}`);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (hasOutdatedCookie) {
|
|
187
|
-
response.cookies.set(COOKIE_LOCALE_NAME, locale, {
|
|
188
|
-
sameSite: 'strict',
|
|
189
|
-
maxAge: 31536000 // 1 year
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (
|
|
194
|
-
configWithDefaults.localePrefix !== 'never' &&
|
|
195
|
-
configWithDefaults.alternateLinks &&
|
|
196
|
-
configWithDefaults.locales.length > 1
|
|
197
|
-
) {
|
|
198
|
-
response.headers.set(
|
|
199
|
-
'Link',
|
|
200
|
-
getAlternateLinksHeaderValue(configWithDefaults, request)
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return response;
|
|
205
|
-
};
|
|
206
|
-
}
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import {match} from '@formatjs/intl-localematcher';
|
|
2
|
-
import Negotiator from 'negotiator';
|
|
3
|
-
import {RequestCookies} from 'next/dist/server/web/spec-extension/cookies';
|
|
4
|
-
import {COOKIE_LOCALE_NAME} from '../shared/constants';
|
|
5
|
-
import {
|
|
6
|
-
DomainConfig,
|
|
7
|
-
MiddlewareConfigWithDefaults
|
|
8
|
-
} from './NextIntlMiddlewareConfig';
|
|
9
|
-
import {
|
|
10
|
-
getLocaleFromPathname,
|
|
11
|
-
getHost,
|
|
12
|
-
isLocaleSupportedOnDomain
|
|
13
|
-
} from './utils';
|
|
14
|
-
|
|
15
|
-
function findDomainFromHost(
|
|
16
|
-
requestHeaders: Headers,
|
|
17
|
-
domains: Array<DomainConfig>
|
|
18
|
-
) {
|
|
19
|
-
let host = getHost(requestHeaders);
|
|
20
|
-
|
|
21
|
-
// Remove port (easier for local development)
|
|
22
|
-
host = host?.replace(/:\d+$/, '');
|
|
23
|
-
|
|
24
|
-
if (host && domains) {
|
|
25
|
-
return domains.find((cur) => cur.domain === host);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return undefined;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function getAcceptLanguageLocale(
|
|
32
|
-
requestHeaders: Headers,
|
|
33
|
-
locales: Array<string>,
|
|
34
|
-
defaultLocale: string
|
|
35
|
-
) {
|
|
36
|
-
let locale;
|
|
37
|
-
|
|
38
|
-
const languages = new Negotiator({
|
|
39
|
-
headers: {
|
|
40
|
-
'accept-language': requestHeaders.get('accept-language') || undefined
|
|
41
|
-
}
|
|
42
|
-
}).languages();
|
|
43
|
-
try {
|
|
44
|
-
locale = match(languages, locales, defaultLocale);
|
|
45
|
-
} catch (e) {
|
|
46
|
-
// Invalid language
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return locale;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function resolveLocaleFromPrefix(
|
|
53
|
-
{defaultLocale, localeDetection, locales}: MiddlewareConfigWithDefaults,
|
|
54
|
-
requestHeaders: Headers,
|
|
55
|
-
requestCookies: RequestCookies,
|
|
56
|
-
pathname: string
|
|
57
|
-
) {
|
|
58
|
-
let locale;
|
|
59
|
-
|
|
60
|
-
// Prio 1: Use route prefix
|
|
61
|
-
if (pathname) {
|
|
62
|
-
const pathLocale = getLocaleFromPathname(pathname);
|
|
63
|
-
if (locales.includes(pathLocale)) {
|
|
64
|
-
locale = pathLocale;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Prio 2: Use existing cookie
|
|
69
|
-
if (!locale && localeDetection && requestCookies) {
|
|
70
|
-
if (requestCookies.has(COOKIE_LOCALE_NAME)) {
|
|
71
|
-
const value = requestCookies.get(COOKIE_LOCALE_NAME)?.value;
|
|
72
|
-
if (value && locales.includes(value)) {
|
|
73
|
-
locale = value;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Prio 3: Use the `accept-language` header
|
|
79
|
-
if (!locale && localeDetection && requestHeaders) {
|
|
80
|
-
locale = getAcceptLanguageLocale(requestHeaders, locales, defaultLocale);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Prio 4: Use default locale
|
|
84
|
-
if (!locale) {
|
|
85
|
-
locale = defaultLocale;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return locale;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function resolveLocaleFromDomain(
|
|
92
|
-
config: MiddlewareConfigWithDefaults,
|
|
93
|
-
requestHeaders: Headers,
|
|
94
|
-
requestCookies: RequestCookies,
|
|
95
|
-
pathname: string
|
|
96
|
-
) {
|
|
97
|
-
const {domains} = config;
|
|
98
|
-
|
|
99
|
-
const localeFromPrefixStrategy = resolveLocaleFromPrefix(
|
|
100
|
-
config,
|
|
101
|
-
requestHeaders,
|
|
102
|
-
requestCookies,
|
|
103
|
-
pathname
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
// Prio 1: Use a domain
|
|
107
|
-
if (domains) {
|
|
108
|
-
const domain = findDomainFromHost(requestHeaders, domains);
|
|
109
|
-
const hasLocalePrefix =
|
|
110
|
-
pathname && pathname.startsWith(`/${localeFromPrefixStrategy}`);
|
|
111
|
-
|
|
112
|
-
if (domain) {
|
|
113
|
-
return {
|
|
114
|
-
locale:
|
|
115
|
-
isLocaleSupportedOnDomain(localeFromPrefixStrategy, domain) ||
|
|
116
|
-
hasLocalePrefix
|
|
117
|
-
? localeFromPrefixStrategy
|
|
118
|
-
: domain.defaultLocale,
|
|
119
|
-
domain
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Prio 2: Use prefix strategy
|
|
125
|
-
return {locale: localeFromPrefixStrategy};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export default function resolveLocale(
|
|
129
|
-
config: MiddlewareConfigWithDefaults,
|
|
130
|
-
requestHeaders: Headers,
|
|
131
|
-
requestCookies: RequestCookies,
|
|
132
|
-
pathname: string
|
|
133
|
-
): {locale: string; domain?: DomainConfig} {
|
|
134
|
-
if (config.domains) {
|
|
135
|
-
return resolveLocaleFromDomain(
|
|
136
|
-
config,
|
|
137
|
-
requestHeaders,
|
|
138
|
-
requestCookies,
|
|
139
|
-
pathname
|
|
140
|
-
);
|
|
141
|
-
} else {
|
|
142
|
-
return {
|
|
143
|
-
locale: resolveLocaleFromPrefix(
|
|
144
|
-
config,
|
|
145
|
-
requestHeaders,
|
|
146
|
-
requestCookies,
|
|
147
|
-
pathname
|
|
148
|
-
)
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
}
|
package/src/middleware/utils.tsx
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import {DomainConfig} from './NextIntlMiddlewareConfig';
|
|
2
|
-
|
|
3
|
-
export function getLocaleFromPathname(pathname: string) {
|
|
4
|
-
return pathname.split('/')[1];
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function getHost(requestHeaders: Headers) {
|
|
8
|
-
return (
|
|
9
|
-
requestHeaders.get('x-forwarded-host') ??
|
|
10
|
-
requestHeaders.get('host') ??
|
|
11
|
-
undefined
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function isLocaleSupportedOnDomain(
|
|
16
|
-
locale: string,
|
|
17
|
-
domain: DomainConfig
|
|
18
|
-
) {
|
|
19
|
-
return (
|
|
20
|
-
domain.defaultLocale === locale ||
|
|
21
|
-
!domain.locales ||
|
|
22
|
-
domain.locales.includes(locale)
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function getBestMatchingDomain(
|
|
27
|
-
curHostDomain: DomainConfig | undefined,
|
|
28
|
-
locale: string,
|
|
29
|
-
domainConfigs: Array<DomainConfig>
|
|
30
|
-
) {
|
|
31
|
-
let domainConfig;
|
|
32
|
-
|
|
33
|
-
// Prio 1: Stay on current domain
|
|
34
|
-
if (curHostDomain && isLocaleSupportedOnDomain(locale, curHostDomain)) {
|
|
35
|
-
domainConfig = curHostDomain;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Prio 2: Use alternative domain with matching default locale
|
|
39
|
-
if (!domainConfig) {
|
|
40
|
-
domainConfig = domainConfigs.find((cur) => cur.defaultLocale === locale);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Prio 3: Use alternative domain with restricted matching locale
|
|
44
|
-
if (!domainConfig) {
|
|
45
|
-
domainConfig = domainConfigs.find(
|
|
46
|
-
(cur) => cur.locales != null && cur.locales.includes(locale)
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Prio 4: Stay on the current domain if it supports all locales
|
|
51
|
-
if (!domainConfig && curHostDomain?.locales == null) {
|
|
52
|
-
domainConfig = curHostDomain;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Prio 5: Use alternative domain that supports all locales
|
|
56
|
-
if (!domainConfig) {
|
|
57
|
-
domainConfig = domainConfigs.find((cur) => !cur.locales);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return domainConfig;
|
|
61
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This is the main entry file when non-'react-server'
|
|
3
|
-
* environments import from 'next-intl'.
|
|
4
|
-
*
|
|
5
|
-
* Maintainer notes:
|
|
6
|
-
* - Make sure this mirrors the API from 'react-server'.
|
|
7
|
-
* - Make sure everything exported from this module is
|
|
8
|
-
* supported in all Next.js versions that are supported.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
export * from 'use-intl';
|
|
12
|
-
export {default as NextIntlClientProvider} from '../shared/NextIntlClientProvider';
|
|
13
|
-
|
|
14
|
-
// Legacy export (TBD if we'll deprecate this in favour of `NextIntlClientProvider`)
|
|
15
|
-
export {default as NextIntlProvider} from '../shared/NextIntlClientProvider';
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This is the main entry file when 'react-server' environments
|
|
3
|
-
* (i.e. RSC) import from 'next-intl'. Currently we export everything
|
|
4
|
-
* from `use-intl` core, but React-APIs are stubbed out.
|
|
5
|
-
*
|
|
6
|
-
* Make sure this mirrors the API from '../react-client'.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
export * from 'use-intl/dist/src/core';
|
|
10
|
-
export {default as NextIntlClientProvider} from '../shared/NextIntlClientProvider';
|
|
11
|
-
|
|
12
|
-
function notSupported() {
|
|
13
|
-
throw new Error(
|
|
14
|
-
`The React APIs of next-intl are currently not available in Server Components.
|
|
15
|
-
|
|
16
|
-
You can try one of these options:
|
|
17
|
-
1. Try out the Server Components beta, see https://next-intl-docs.vercel.app/docs/getting-started
|
|
18
|
-
2. Use the core library as a stopgap solution, see https://next-intl-docs.vercel.app/docs/environments/core-library
|
|
19
|
-
`
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const IntlProvider = notSupported;
|
|
24
|
-
export const useTranslations = notSupported;
|
|
25
|
-
export const useIntl = notSupported;
|
|
26
|
-
export const useLocale = notSupported;
|
|
27
|
-
export const useNow = notSupported;
|
|
28
|
-
export const useTimeZone = notSupported;
|
|
29
|
-
export const Link = notSupported;
|
package/src/server/index.tsx
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Server-only APIs available via `next-intl/server`.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import createMiddleware_ from '../middleware';
|
|
6
|
-
import MiddlewareConfig from '../middleware/NextIntlMiddlewareConfig';
|
|
7
|
-
|
|
8
|
-
let hasWarned = false;
|
|
9
|
-
/** @deprecated Should be imported as `import createMiddleware from 'next-intl/middleware', not from `next-intl/server`. */
|
|
10
|
-
export function createIntlMiddleware(config: MiddlewareConfig) {
|
|
11
|
-
if (!hasWarned) {
|
|
12
|
-
hasWarned = true;
|
|
13
|
-
console.warn(
|
|
14
|
-
`
|
|
15
|
-
Importing \`createMiddleware\` from \`next-intl/server\` is deprecated. Please update the import and add a \`matcher\`:
|
|
16
|
-
|
|
17
|
-
// middleware.ts
|
|
18
|
-
import createMiddleware from 'next-intl/middleware';
|
|
19
|
-
|
|
20
|
-
// ...
|
|
21
|
-
|
|
22
|
-
export const config = {
|
|
23
|
-
// Skip all paths that should not be internationalized
|
|
24
|
-
matcher: ['/((?!api|_next|.*\\\\..*).*)']
|
|
25
|
-
};
|
|
26
|
-
`
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
return createMiddleware_({
|
|
30
|
-
...config,
|
|
31
|
-
// @ts-expect-error
|
|
32
|
-
_matcher: ['/((?!api|_next|.*\\..*).*)']
|
|
33
|
-
});
|
|
34
|
-
}
|
package/src/shared/BaseLink.tsx
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import NextLink from 'next/link';
|
|
4
|
-
import {usePathname} from 'next/navigation';
|
|
5
|
-
import React, {ComponentProps, forwardRef, useEffect, useState} from 'react';
|
|
6
|
-
import useClientLocale from '../client/useClientLocale';
|
|
7
|
-
import {isLocalHref, localizeHref, prefixHref} from './utils';
|
|
8
|
-
|
|
9
|
-
type Props = Omit<ComponentProps<typeof NextLink>, 'locale'> & {
|
|
10
|
-
locale: string;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
function BaseLink({href, locale, prefetch, ...rest}: Props, ref: Props['ref']) {
|
|
14
|
-
// The types aren't entirely correct here. Outside of Next.js
|
|
15
|
-
// `useParams` can be called, but the return type is `null`.
|
|
16
|
-
const pathname = usePathname() as ReturnType<typeof usePathname> | null;
|
|
17
|
-
|
|
18
|
-
const defaultLocale = useClientLocale();
|
|
19
|
-
const isChangingLocale = locale !== defaultLocale;
|
|
20
|
-
|
|
21
|
-
const [localizedHref, setLocalizedHref] = useState<typeof href>(() =>
|
|
22
|
-
isLocalHref(href) && locale
|
|
23
|
-
? // Potentially the href shouldn't be prefixed, but to determine this we
|
|
24
|
-
// need a) the default locale and b) the information if we use prefixed
|
|
25
|
-
// routing. During the server side render (both in RSC as well as SSR),
|
|
26
|
-
// we don't have this information. Therefore we always prefix the href
|
|
27
|
-
// since this will always result in a valid URL, even if it might cause
|
|
28
|
-
// a redirect. This is better than pointing to a non-localized href
|
|
29
|
-
// during the server render, which would potentially be wrong. The final
|
|
30
|
-
// href is determined in the effect below.
|
|
31
|
-
prefixHref(href, locale)
|
|
32
|
-
: href
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
useEffect(() => {
|
|
36
|
-
if (!pathname) return;
|
|
37
|
-
|
|
38
|
-
setLocalizedHref(
|
|
39
|
-
localizeHref(href, locale, defaultLocale, pathname ?? undefined)
|
|
40
|
-
);
|
|
41
|
-
}, [defaultLocale, href, locale, pathname]);
|
|
42
|
-
|
|
43
|
-
if (isChangingLocale) {
|
|
44
|
-
if (prefetch && process.env.NODE_ENV !== 'production') {
|
|
45
|
-
console.error(
|
|
46
|
-
'The `prefetch` prop is currently not supported when using the `locale` prop on `Link` to switch the locale.`'
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
prefetch = false;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return (
|
|
53
|
-
<NextLink ref={ref} href={localizedHref} prefetch={prefetch} {...rest} />
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export default forwardRef(BaseLink);
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import {useRouter} from 'next/router';
|
|
4
|
-
import React, {ComponentProps} from 'react';
|
|
5
|
-
import {IntlProvider} from 'use-intl';
|
|
6
|
-
|
|
7
|
-
type Props = Omit<ComponentProps<typeof IntlProvider>, 'locale' | 'now'> & {
|
|
8
|
-
locale?: string;
|
|
9
|
-
/** If a string is supplied, make sure this conforms to the ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ */
|
|
10
|
-
now?: Date | string;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export default function NextIntlClientProvider({
|
|
14
|
-
children,
|
|
15
|
-
locale,
|
|
16
|
-
now,
|
|
17
|
-
...rest
|
|
18
|
-
}: Props) {
|
|
19
|
-
let router;
|
|
20
|
-
try {
|
|
21
|
-
// Reading from context is practically ok to do conditionally
|
|
22
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
23
|
-
router = useRouter();
|
|
24
|
-
} catch (error) {
|
|
25
|
-
// Calling `useRouter` is not supported in the app folder
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// The router can be undefined if used in a context outside
|
|
29
|
-
// of Next.js (e.g. unit tests, Storybook, ...)
|
|
30
|
-
if (!locale && router) {
|
|
31
|
-
locale = router.locale;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Currently RSC serialize dates to strings, therefore make sure we have
|
|
35
|
-
// a date object. We might be able to remove this once more types have
|
|
36
|
-
// first-class serialization support (https://github.com/facebook/react/issues/25687)
|
|
37
|
-
if (typeof now === 'string') {
|
|
38
|
-
now = new Date(now);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (!locale) {
|
|
42
|
-
throw new Error(
|
|
43
|
-
process.env.NODE_ENV !== 'production'
|
|
44
|
-
? "Couldn't determine locale. Please pass an explicit `locale` prop the provider, or if you're using the `pages` folder, use internationalized routing (https://nextjs.org/docs/advanced-features/i18n-routing)."
|
|
45
|
-
: undefined
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return (
|
|
50
|
-
<IntlProvider locale={locale} now={now} {...rest}>
|
|
51
|
-
{children}
|
|
52
|
-
</IntlProvider>
|
|
53
|
-
);
|
|
54
|
-
}
|
package/src/shared/constants.tsx
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
// Reuse the legacy cookie name
|
|
2
|
-
// https://nextjs.org/docs/advanced-features/i18n-routing#leveraging-the-next_locale-cookie
|
|
3
|
-
export const COOKIE_LOCALE_NAME = 'NEXT_LOCALE';
|
|
4
|
-
|
|
5
|
-
// In a URL like "/en-US/about", the locale segment is "en-US"
|
|
6
|
-
export const LOCALE_SEGMENT_NAME = 'locale';
|