UNPKG

intlayer

Version:

Manage internationalization i18n in a simple way, through TypeScript, declaration file, declare your multilingual content every where in your code.

119 lines (117 loc) 4.27 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); //#region src/routing/index.ts const buildRules = (rules, processor) => ({ rules: Object.entries(rules).map(([canonical, localized]) => ({ canonical: processor(canonical), localized: Object.fromEntries(Object.entries(localized).map(([locale, pattern]) => [locale, pattern ? processor(pattern) : pattern])) })) }); const cleanPath = (pattern) => pattern.startsWith("/") ? pattern : `/${pattern}`; /** * Standardizes pattern to :param syntax. * Supports: * - Next.js/SvelteKit/Nuxt: [slug], [...slug], [[slug]] * - TanStack Router: $slug * - Solid Router: *slug * - React/Vue Router: :slug, * */ const normalizePattern = (pattern, framework) => { let normalized = pattern; if (framework === "nextjs") { normalized = normalized.replace(/\[\[\.\.\.([^\]]+)\]\]/g, ":$1*").replace(/\[\.\.\.([^\]]+)\]/g, ":$1+").replace(/\[([^\]]+)\]/g, ":$1"); } else if (framework === "sveltekit") { normalized = normalized.replace(/\[\.\.\.([^\]]+)\]/g, ":$1*").replace(/\[\[([^\]]+)\]\]/g, ":$1?").replace(/\[([^\]]+)\]/g, ":$1"); } else if (framework === "nuxt") { normalized = normalized.replace(/\[\.\.\.([^\]]+)\]/g, ":$1*").replace(/\[([^\]]+)\]/g, ":$1"); } else { normalized = normalized.replace(/\$([^/]+)/g, ":$1").replace(/\*([^/]+)/g, ":$1*").replace(/:([^/]+)\?/g, ":$1?").replace(/\*/g, ":path*"); } return normalized; }; /** * Removes locale markers from the pattern. */ const stripLocale = (pattern) => pattern.replace(/\/?(:locale|\[locale\]|\$locale)\/?/g, "/").replace(/\/+/g, "/").replace(/\/$/, "") || "/"; /** * Factory to create formatters that populate 'url' and a specific proxy key. */ const createFormatter = (proxyKey, framework) => (rules) => { const normalize = (pattern) => normalizePattern(pattern, framework); const strip = (pattern) => stripLocale(normalize(pattern)); return { url: buildRules(rules, (pattern) => cleanPath(strip(pattern))), [proxyKey]: buildRules(rules, (pattern) => cleanPath(normalize(pattern))) }; }; /** * Formatter for Next.js style rewrites. * Patterns use Next.js dynamic routing syntax: * - Slug: `[slug]` * - Catch-all: `[...slug]` (1+) * - Optional catch-all: `[[...slug]]` (0+) * - Locale: `[locale]` */ const nextjsRewrite = createFormatter("nextjs", "nextjs"); /** * Formatter for SvelteKit style rewrites. * Patterns use SvelteKit dynamic routing syntax: * - Slug: `[slug]` * - Catch-all: `[...slug]` (0+) * - Locale: `[locale]` */ const svelteKitRewrite = createFormatter("vite", "sveltekit"); /** * Formatter for React Router style rewrites. * Patterns use React Router dynamic routing syntax: * - Slug: `:slug` * - Optional slug: `:slug?` * - Catch-all: `*` * - Locale: `:locale` */ const reactRouterRewrite = createFormatter("vite"); /** * Formatter for Vue style rewrites. * Patterns use Vue Router 4 dynamic routing syntax: * - Slug: `:slug` * - Optional slug: `:slug?` * - Catch-all: `:slug*` or `:slug+` * - Locale: `:locale` */ const vueRouterRewrite = createFormatter("vite"); /** * Formatter for Solid Router style rewrites. * Patterns use Solid Router dynamic routing syntax: * - Slug: `:slug` * - Catch-all: `*slug` * - Locale: `:locale` */ const solidRouterRewrite = createFormatter("vite"); /** * Formatter for Nuxt style rewrites. * Patterns use Nuxt 3 dynamic routing syntax: * - Slug: `[slug]` * - Catch-all: `[...slug]` (0+) * - Locale: `[locale]` */ const nuxtRewrite = createFormatter("vite", "nuxt"); /** * Formatter for TanStack Router style rewrites. * Patterns use TanStack Router dynamic routing syntax: * - Slug: `$slug` * - Catch-all: `*` * - Locale: `$locale` */ const tanstackRouterRewrite = createFormatter("vite"); /** * Generic formatter for Vite-based projects. * Supports most dynamic routing syntaxes and normalizes them for the Vite proxy. */ const viteRewrite = createFormatter("vite"); //#endregion exports.nextjsRewrite = nextjsRewrite; exports.nuxtRewrite = nuxtRewrite; exports.reactRouterRewrite = reactRouterRewrite; exports.solidRouterRewrite = solidRouterRewrite; exports.svelteKitRewrite = svelteKitRewrite; exports.tanstackRouterRewrite = tanstackRouterRewrite; exports.viteRewrite = viteRewrite; exports.vueRouterRewrite = vueRouterRewrite; //# sourceMappingURL=index.cjs.map