@astrolicious/i18n
Version:
Yet another i18n integration for Astro with server and client utilities, type safety and translations built-in.
12 lines (11 loc) • 1.93 kB
JavaScript
import{join as $,relative as I}from"node:path";import{fileURLToPath as d}from"node:url";import{defineUtility as A,watchDirectory as w}from"astro-integration-kit";import{normalizePath as u}from"vite";import{existsSync as h,readdirSync as x}from"node:fs";import{basename as L,extname as N}from"node:path";var f=(r,o,s)=>{let t=[];if(h(r)){let e=x(r).filter(n=>n.endsWith(".json"));for(let n of e)t.push({namespaceName:L(n,N(n)),fileName:n})}let i=t.map(e=>e.namespaceName);return s.info(`Detected namespaces: ${i.map(e=>`"${e}"`).join(",")}`),i.includes(o)||s.warn(`Default namespace "${o}" is not detected`),{namespaces:i}};import{existsSync as D,readFileSync as O,readdirSync as j}from"node:fs";import{basename as R,extname as S,join as p}from"node:path";import{normalizePath as l}from"vite";var g=(r,{locales:o},s)=>{let t={},i=o.map(e=>({locale:e,dir:l(p(s,e))})).filter(e=>D(e.dir));for(let{locale:e,dir:n}of i){let y=j(n).filter(a=>a.endsWith(".json"));for(let a of y){let c=l(p(n,a));try{let m=JSON.parse(O(c,"utf-8"));t[e]??={},t[e][R(a,S(a))]=m}catch{r.warn(`Can't parse "${c}", skipping.`)}}}return r.info(`${Object.keys(Object.values(t)).length} resources registered`),t};var C=(r,o)=>{let s=u(d(new URL(o.localesDir,r))),t=$(s,o.defaultLocale);return{localesDir:s,defaultLocalesDir:t}},b="astro-i18n/i18next",q=A("astro:config:setup")((r,o)=>{let s=r.logger.fork(b),t=C(r.config.root,o);w(r,t.localesDir),s.info(`Registered watcher for "${u(I(d(r.config.root),t.localesDir))}" directory`);let{namespaces:i}=f(t.defaultLocalesDir,o.defaultNamespace,s),e=g(s,o,t.localesDir),n=`
type Resources = ${JSON.stringify(e[o.defaultLocale]??{})}
declare module "i18next" {
interface CustomTypeOptions {
defaultNS: "${o.defaultNamespace}";
resources: Resources;
}
}
export {}
`;return{namespaces:i,resources:e,dtsContent:n}});export{q as handleI18next};
//# sourceMappingURL=index.js.map