UNPKG

@astrolicious/i18n

Version:

Yet another i18n integration for Astro with server and client utilities, type safety and translations built-in.

2 lines 3.1 kB
import{mkdirSync as M,readFileSync as z,rmSync as C,writeFileSync as F}from"node:fs";import{dirname as D,join as G,relative as L,resolve as U}from"node:path";import{fileURLToPath as A}from"node:url";import{defineUtility as B}from"astro-integration-kit";import{addPageDir as N}from"astro-pages";import{AstroError as q}from"astro/errors";import{withLeadingSlash as J}from"ufo";import{normalizePath as $}from"vite";import{defineUtility as W}from"astro-integration-kit";import{join as k,relative as T}from"node:path";import{fileURLToPath as O}from"node:url";import{defineUtility as _,watchDirectory as H}from"astro-integration-kit";import{normalizePath as S}from"vite";var v=_("astro:config:setup")((t,r)=>{let{config:o}=t,c=S(k(O(o.srcDir),h));H(t,c),r.info(`Registered watcher for "${S(T(O(t.config.root),c))}" directory`)});var h="routes",b="astro-i18n/routing",ct=W("astro:config:setup")((t,r)=>{let o=t.logger.fork(b);v(t,o);let{routes:c}=x(t,r,o);return{routes:c}});var K=t=>{let r=t.match(/export const prerender = (\w+)/);if(r)return r[1]==="true"},Q=t=>Object.entries(N({...t,dir:h}).pages).map(([r,o])=>({pattern:r,entrypoint:o})),V=B("astro:config:setup")(({config:t})=>{let r=A(new URL(h,t.srcDir)),o=U(A(t.root),"./.astro/astro-i18n/entrypoints");return{routesDir:r,entrypointsDir:o}}),X=({strategy:t,defaultLocale:r,locales:o,pages:c},a,e,u,g)=>{let y=()=>{let s=a===r,m=s&&t==="prefixExceptDefault"?"":`/${a}`,n=J(s?e.pattern:c?.[e.pattern]?.[a]??e.pattern);return m+n},d=s=>{let m=(i,j,l)=>{let f=U(D(j),i),p=L(D(l),f);return $(p)};M(D(s),{recursive:!0});let n=z(e.entrypoint,"utf-8");if(e.entrypoint.endsWith(".astro"))try{n=n.replaceAll("getLocalePlaceholder()",`"${a}"`).replaceAll("getLocalesPlaceholder()",`[${o.map(l=>`"${l}"`).join(", ")}]`).replaceAll("getDefaultLocalePlaceholder()",`"${r}"`);let[,i,...j]=n.split("---");if(!i)throw new Error("No frontmatter found");i=i.replace(/import\s+([\s\S]*?)\s+from\s+['"](.+?)['"]/g,(l,f,p)=>{let E=p.startsWith("./")||p.startsWith("../")?m(p,e.entrypoint,s):p;return`import ${f} from '${E}'`}),i=i.replace(/import\s*\(\s*['"](.+?)['"]\s*\)/g,(l,f)=>`import('${f.startsWith("./")||f.startsWith("../")?m(f,e.entrypoint,s):f}')`),n=`---${i}---${j.join("---")}`}catch{throw new q(`An error occured while transforming "${e.entrypoint}".`,"Make sure it has a valid frontmatter, even empty")}return F(s,n,"utf-8"),{prerender:K(n)}},R=s=>{let m=[],n=s.match(/\[([^\]]+)]/g);if(n)for(let i of n)m.push(i.slice(1,-1));return m},P=y(),I=G(u.entrypointsDir,a,$(L(u.routesDir,e.entrypoint))),{prerender:w}=d(I);return g.info(`Injecting "${P}" route`),{locale:a,params:R(P),pattern:e.pattern,injectedRoute:{pattern:P,entrypoint:I,...w?{prerender:w}:{}}}},x=(t,r,o)=>{let{config:c,injectRoute:a}=t,{locales:e}=r;o.info("Starting routes injection...");let u=V(t);C(u.entrypointsDir,{recursive:!0,force:!0}),o.info(`Cleaned "${$(L(A(c.root),u.entrypointsDir))}" directory`);let g=[],y=Q(t);for(let d of e)for(let R of y)g.push(X(r,d,R,u,o));for(let{injectedRoute:d}of g)a(d);return{routes:g}};export{x as registerRoutes}; //# sourceMappingURL=register.js.map