UNPKG

next-intlayer

Version:

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

209 lines 7.35 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var intlayerMiddleware_exports = {}; __export(intlayerMiddleware_exports, { intlayerMiddleware: () => intlayerMiddleware }); module.exports = __toCommonJS(intlayerMiddleware_exports); var import_built = __toESM(require("@intlayer/config/built")); var import_server = require("next/server"); var import_localeDetector = require('./localeDetector.cjs'); const { internationalization, middleware } = import_built.default; const { locales, defaultLocale } = internationalization; const { headerName, cookieName, prefixDefault, basePath, serverSetCookie, noPrefix, detectLocaleOnPrefetchNoPrefix } = middleware; const isPrefetchRequest = (request) => { const purpose = request.headers.get("purpose"); const nextRouterPrefetch = request.headers.get("next-router-prefetch"); const nextUrl = request.headers.get("next-url"); const xNextjsData = request.headers.get("x-nextjs-data"); return purpose === "prefetch" || nextRouterPrefetch === "1" || !!nextUrl || !!xNextjsData; }; 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) { const isPrefetch = isPrefetchRequest(request); if (isPrefetch && !detectLocaleOnPrefetchNoPrefix) { return handleMissingPathLocale( request, defaultLocale, pathname, basePathTrailingSlash ); } return handleMissingPathLocale( request, cookieLocale, pathname, basePathTrailingSlash ); } return handleExistingPathLocale( request, cookieLocale, pathLocale, pathname, basePathTrailingSlash ); }; const handleMissingPathLocale = (request, cookieLocale, pathname, basePathTrailingSlash) => { let locale = cookieLocale ?? (0, import_localeDetector.localeDetector)?.(request) ?? defaultLocale; if (!locales.includes(locale)) { 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 search = request.nextUrl.search; const pathWithSearch = search && !newPath.includes("?") ? `${newPath}${search}` : newPath; const response = import_server.NextResponse.rewrite(new URL(pathWithSearch, request.url)); response.headers.set(headerName, locale); return response; }; const redirectUrl = (request, newPath) => { const search = request.nextUrl.search; const pathWithSearch = search && !newPath.includes("?") ? `${newPath}${search}` : newPath; return import_server.NextResponse.redirect(new URL(pathWithSearch, request.url)); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { intlayerMiddleware }); //# sourceMappingURL=intlayerMiddleware.cjs.map