nuxt-i18n-micro
Version:
Nuxt I18n Micro is a lightweight, high-performance internationalization module for Nuxt, designed to handle multi-language support with minimal overhead, fast build times, and efficient runtime performance.
65 lines (62 loc) • 3.01 kB
JavaScript
import path from 'node:path';
const isInternalPath = (p) => /(?:^|\/)__[^/]+/.test(p);
function extractLocaleRoutes(content, filePath) {
const defineMatch = content.match(/\$?\bdefineI18nRoute\s*\(\s*\{[\s\S]*?\}\s*\)/);
if (defineMatch) {
const localeRoutesMatch = defineMatch[0].match(/localeRoutes:\s*(\{[\s\S]*?\})/);
if (localeRoutesMatch && localeRoutesMatch[1]) {
try {
const parsedLocaleRoutes = Function('"use strict";return (' + localeRoutesMatch[1] + ")")();
if (typeof parsedLocaleRoutes === "object" && parsedLocaleRoutes !== null) {
if (validateDefineI18nRouteConfig(parsedLocaleRoutes)) {
return parsedLocaleRoutes;
}
} else {
console.error("localeRoutes found but it is not a valid object in file:", filePath);
}
} catch (error) {
console.error("Failed to parse localeRoutes:", error, "in file:", filePath);
}
}
}
return null;
}
function validateDefineI18nRouteConfig(obj) {
if (typeof obj !== "object")
return false;
for (const routeKey in obj.localeRoutes) {
if (typeof obj.localeRoutes[routeKey] !== "string")
return false;
}
return true;
}
const normalizePath = (routePath) => {
if (!routePath) {
return "";
}
const normalized = path.posix.normalize(routePath).replace(/\/+$/, "");
return normalized === "." ? "" : normalized;
};
const cloneArray = (array) => array.map((item) => ({ ...item }));
const isPageRedirectOnly = (page) => !!(page.redirect && !page.file);
const removeLeadingSlash = (routePath) => routePath.startsWith("/") ? routePath.slice(1) : routePath;
const buildRouteName = (baseName, localeCode, isCustom) => isCustom ? `localized-${baseName}-${localeCode}` : `localized-${baseName}`;
const shouldAddLocalePrefix = (locale, defaultLocale, addLocalePrefix, includeDefaultLocaleRoute) => addLocalePrefix && !(locale === defaultLocale.code && !includeDefaultLocaleRoute);
const isLocaleDefault = (locale, defaultLocale, includeDefaultLocaleRoute) => {
const localeCode = typeof locale === "string" ? locale : locale.code;
return localeCode === defaultLocale.code && !includeDefaultLocaleRoute;
};
const buildFullPath = (locale, basePath, customRegex) => {
const regexString = normalizeRegex(customRegex?.toString());
const localeParam = regexString ? regexString : Array.isArray(locale) ? locale.join("|") : locale;
return normalizePath(path.posix.join("/", `:locale(${localeParam})`, basePath));
};
const buildFullPathNoPrefix = (basePath) => {
return normalizePath(basePath);
};
const normalizeRegex = (toNorm) => {
if (typeof toNorm === "undefined")
return void 0;
return toNorm.startsWith("/") && toNorm.endsWith("/") ? toNorm?.slice(1, -1) : toNorm;
};
export { buildFullPath, buildFullPathNoPrefix, buildRouteName, cloneArray, extractLocaleRoutes, isInternalPath, isLocaleDefault, isPageRedirectOnly, normalizePath, removeLeadingSlash, shouldAddLocalePrefix, validateDefineI18nRouteConfig };