UNPKG

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.

53 lines (52 loc) 2.4 kB
export function extractBaseRoutePattern(matchedPath) { return matchedPath.replace(/\/:locale\([^)]+\)/g, "").replace(/\/:([^()]+)\(\)/g, "/[$1]").replace(/\/:([^()]+)/g, "/[$1]"); } export function findAllowedLocalesForRoute(route, routeLocales) { const routePath = route.path; const routeName = route.name?.toString(); const normalizedRouteName = routeName?.replace("localized-", ""); const normalizedRoutePath = normalizedRouteName ? `/${normalizedRouteName}` : void 0; let allowedLocales = routeName && routeLocales?.[routeName] || normalizedRouteName && routeLocales?.[normalizedRouteName] || normalizedRoutePath && routeLocales?.[normalizedRoutePath] || routeLocales?.[routePath]; if (!allowedLocales && route.matched && route.matched.length > 0) { const matchedRoute = route.matched[0]; const matchedPath = matchedRoute.path; const baseRoutePattern = extractBaseRoutePattern(matchedPath); if (routeLocales?.[baseRoutePattern]) { allowedLocales = routeLocales[baseRoutePattern]; } } return allowedLocales || null; } export function isMetaDisabledForRoute(route, routeDisableMeta, currentLocale) { if (!routeDisableMeta) { return false; } const routePath = route.path; const routeName = route.name?.toString(); const normalizedRouteName = routeName?.replace("localized-", ""); const normalizedRoutePath = normalizedRouteName ? `/${normalizedRouteName}` : void 0; const checkDisableMeta = (disableMetaValue) => { if (disableMetaValue === void 0) { return false; } if (typeof disableMetaValue === "boolean") { return disableMetaValue; } if (Array.isArray(disableMetaValue)) { return currentLocale ? disableMetaValue.includes(currentLocale) : false; } return false; }; if (checkDisableMeta(routeDisableMeta[routePath]) || routeName && checkDisableMeta(routeDisableMeta[routeName]) || normalizedRouteName && checkDisableMeta(routeDisableMeta[normalizedRouteName]) || normalizedRoutePath && checkDisableMeta(routeDisableMeta[normalizedRoutePath])) { return true; } if (route.matched && route.matched.length > 0) { const matchedRoute = route.matched[0]; const matchedPath = matchedRoute.path; const baseRoutePattern = extractBaseRoutePattern(matchedPath); if (checkDisableMeta(routeDisableMeta[baseRoutePattern])) { return true; } } return false; }