vite-intlayer
Version:
A Vite plugin for seamless internationalization (i18n), providing locale detection, redirection, and environment-based configuration
224 lines • 6.04 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var intlayerMiddlewarePlugin_exports = {};
__export(intlayerMiddlewarePlugin_exports, {
intLayerMiddlewarePlugin: () => intLayerMiddlewarePlugin
});
module.exports = __toCommonJS(intlayerMiddlewarePlugin_exports);
var import_url = require("url");
var import_config = require("@intlayer/config");
var import_core = require("@intlayer/core");
const intlayerConfig = (0, import_config.getConfiguration)();
const { internationalization, middleware } = intlayerConfig;
const { locales: supportedLocales, defaultLocale } = internationalization;
const {
cookieName,
headerName,
prefixDefault,
noPrefix,
serverSetCookie,
basePath = ""
} = middleware;
const intLayerMiddlewarePlugin = () => {
return {
name: "vite-intlayer-middleware-plugin",
configureServer: (server) => {
server.middlewares.use((req, res, next) => {
if (req.url?.startsWith("/node_modules") || req.url?.startsWith("/@") || req.url?.split("?")[0].match(/\.[a-z]+$/i)) {
return next();
}
const parsedUrl = (0, import_url.parse)(req.url ?? "/", true);
const originalPath = parsedUrl.pathname ?? "/";
const cookies = parseCookies(req.headers.cookie ?? "");
const cookieLocale = getValidLocaleFromCookie(cookies[cookieName]);
const pathLocale = getPathLocale(originalPath);
if (noPrefix) {
handleNoPrefix({
req,
res,
next,
originalPath,
cookieLocale
});
return;
}
handlePrefix({
req,
res,
next,
originalPath,
pathLocale,
cookieLocale
});
});
}
};
};
const parseCookies = (cookieHeader) => {
return cookieHeader.split(";").reduce(
(acc, cookie) => {
const [key, val] = cookie.trim().split("=");
acc[key] = val;
return acc;
},
{}
);
};
const getValidLocaleFromCookie = (locale) => {
if (locale && supportedLocales.includes(locale)) {
return locale;
}
return void 0;
};
const getPathLocale = (pathname) => {
const segments = pathname.split("/").filter(Boolean);
const firstSegment = segments[0];
if (firstSegment && supportedLocales.includes(firstSegment)) {
return firstSegment;
}
return void 0;
};
const redirectUrl = (res, newUrl) => {
res.writeHead(301, { Location: newUrl });
return res.end();
};
const rewriteUrl = (req, res, newUrl, locale) => {
req.url = newUrl;
if (locale && headerName) {
res.setHeader(headerName, locale);
}
};
const constructPath = (locale, currentPath) => {
const cleanBasePath = basePath.startsWith("/") ? basePath : `/${basePath}`;
const normalizedBasePath = cleanBasePath === "/" ? "" : cleanBasePath;
let newPath = `${normalizedBasePath}/${locale}${currentPath}`;
if (!prefixDefault && locale === defaultLocale) {
newPath = `${normalizedBasePath}${currentPath}`;
}
return newPath;
};
const handleNoPrefix = ({
req,
res,
next,
originalPath,
cookieLocale
}) => {
let locale = cookieLocale ?? defaultLocale;
if (!cookieLocale) {
const detectedLocale = (0, import_core.localeDetector)(
req.headers,
supportedLocales,
defaultLocale
);
locale = detectedLocale;
}
rewriteUrl(req, res, originalPath, locale);
return next();
};
const handlePrefix = ({
req,
res,
next,
originalPath,
pathLocale,
cookieLocale
}) => {
if (!pathLocale) {
handleMissingPathLocale({
req,
res,
next,
originalPath,
cookieLocale
});
return;
}
handleExistingPathLocale({
req,
res,
next,
originalPath,
pathLocale,
cookieLocale
});
};
const handleMissingPathLocale = ({
req,
res,
next,
originalPath,
cookieLocale
}) => {
let locale = cookieLocale ?? (0, import_core.localeDetector)(
req.headers,
supportedLocales,
defaultLocale
);
if (!supportedLocales.includes(locale)) {
locale = defaultLocale;
}
const newPath = constructPath(locale, originalPath);
if (prefixDefault || locale !== defaultLocale) {
return redirectUrl(res, newPath);
}
rewriteUrl(req, res, newPath, locale);
return next();
};
const handleExistingPathLocale = ({
req,
res,
next,
originalPath,
pathLocale,
cookieLocale
}) => {
if (cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always") {
const newPath = originalPath.replace(`/${pathLocale}`, `/${cookieLocale}`);
const finalPath = constructPath(cookieLocale, newPath.replace(/^\/+/, "/"));
return redirectUrl(res, finalPath);
}
handleDefaultLocaleRedirect({
req,
res,
next,
originalPath,
pathLocale
});
};
const handleDefaultLocaleRedirect = ({
req,
res,
next,
originalPath,
pathLocale
}) => {
if (!prefixDefault && pathLocale === defaultLocale) {
const newPath = originalPath.replace(`/${defaultLocale}`, "") ?? "/";
rewriteUrl(req, res, newPath, pathLocale);
return next();
}
rewriteUrl(req, res, originalPath, pathLocale);
return next();
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
intLayerMiddlewarePlugin
});
//# sourceMappingURL=intlayerMiddlewarePlugin.cjs.map