UNPKG

next-intlayer

Version:

Simplify internationalization i18n in Next.js with context providers, hooks, locale detection, and multilingual content integration.

157 lines 4.81 kB
import configuration from "@intlayer/config/built"; import { NextResponse } from "next/server"; import { localeDetector } from "./localeDetector.mjs"; const { internationalization, middleware } = configuration; const { locales, defaultLocale } = internationalization; const { headerName, cookieName, prefixDefault, basePath, serverSetCookie, noPrefix } = middleware; const intlayerMiddleware = (request, _event, _response) => { const pathname = request.nextUrl.pathname; const cookieLocale = getCookieLocale(request); const basePathTrailingSlash = basePath.endsWith("/"); if (noPrefix) { return handleNoPrefix( request, cookieLocale, pathname, basePathTrailingSlash ); } const pathLocale = getPathLocale(pathname); return handlePrefix( request, cookieLocale, pathLocale, pathname, basePathTrailingSlash ); }; const getCookieLocale = (request) => { if (!cookieName) return void 0; const cookieValue = request.cookies.get(cookieName)?.value; if (cookieValue && locales.includes(cookieValue)) { return cookieValue; } }; const handleNoPrefix = (request, cookieLocale, pathname, basePathTrailingSlash) => { const locale = cookieLocale ?? defaultLocale; const newPath = constructPath( locale, pathname, basePath, basePathTrailingSlash, request.nextUrl.search ); return rewriteUrl(request, newPath, locale); }; const getPathLocale = (pathname) => locales.find( (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}` ); const handlePrefix = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => { if (!pathLocale) { return handleMissingPathLocale( request, cookieLocale, pathname, basePathTrailingSlash ); } return handleExistingPathLocale( request, cookieLocale, pathLocale, pathname, basePathTrailingSlash ); }; const handleMissingPathLocale = (request, cookieLocale, pathname, basePathTrailingSlash) => { let locale = cookieLocale ?? localeDetector?.(request) ?? defaultLocale; if (!locales.includes(locale)) { console.warn( "The localeDetector callback must return a locale included in your locales array. Reverting to using defaultLocale." ); locale = defaultLocale; } const newPath = constructPath( locale, pathname, basePath, basePathTrailingSlash, request.nextUrl.search ); return prefixDefault || locale !== defaultLocale ? redirectUrl(request, newPath) : rewriteUrl(request, newPath, locale); }; const handleExistingPathLocale = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => { if ( // If the cookie locale is set and differs from the locale in the URL, and server should not always set cookie cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always" ) { const newPath = handleCookieLocaleMismatch( request, pathname, pathLocale, cookieLocale, basePath, basePathTrailingSlash ); return redirectUrl(request, newPath); } return handleDefaultLocaleRedirect( request, pathLocale, pathname, basePathTrailingSlash ); }; const handleCookieLocaleMismatch = (request, pathname, pathLocale, cookieLocale, basePath2, basePathTrailingSlash) => { const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`); return constructPath( cookieLocale, newPath, basePath2, basePathTrailingSlash, request.nextUrl.search ); }; const handleDefaultLocaleRedirect = (request, pathLocale, pathname, basePathTrailingSlash) => { if ( // If default locale should not be prefixed and the pathLocale is the defaultLocale !prefixDefault && pathLocale === defaultLocale ) { let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) ?? "/"; if (basePathTrailingSlash) { pathWithoutLocale = pathWithoutLocale.slice(1); } if (request.nextUrl.search) { pathWithoutLocale += request.nextUrl.search; } return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale); } return rewriteUrl(request, pathname, pathLocale); }; const constructPath = (locale, path, basePath2, basePathTrailingSlash, search) => { let newPath = `${locale}${path}`; newPath = `${basePath2}${basePathTrailingSlash ? "" : "/"}${newPath}`; if (search) { newPath += search; } return newPath; }; const rewriteUrl = (request, newPath, locale) => { const response = NextResponse.rewrite(new URL(newPath, request.url)); response.headers.set(headerName, locale); return response; }; const redirectUrl = (request, newPath) => NextResponse.redirect(new URL(newPath, request.url)); export { intlayerMiddleware }; //# sourceMappingURL=intlayerMiddleware.mjs.map