@i18n-micro/astro 1.3.4 → 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const R=require("@i18n-micro/core"),j=require("node:fs"),D=require("node:path");class T extends R.BaseI18n{constructor(e){const l=e._storage||{translations:new Map};if(super({storage:l,plural:e.plural,missingWarn:e.missingWarn,missingHandler:e.missingHandler}),this.initialMessages={},this.storage=l,this._locale=e.locale,this._fallbackLocale=e.fallbackLocale||e.locale,this._currentRoute="index",e.messages){this.initialMessages={...e.messages};for(const[a,i]of Object.entries(e.messages))this.helper.loadTranslations(a,i)}}cloneStorage(e){const l=new Map;for(const[a,i]of e.translations)l.set(a,{...i});return{translations:l}}clone(e){const l=this.cloneStorage(this.storage);return new T({locale:e||this._locale,fallbackLocale:this._fallbackLocale,plural:this.pluralFunc,missingWarn:this.missingWarn,missingHandler:this.missingHandler,_storage:l})}get locale(){return this._locale}set locale(e){this._locale=e}get fallbackLocale(){return this._fallbackLocale}set fallbackLocale(e){this._fallbackLocale=e}setRoute(e){this._currentRoute=e}getLocale(){return this._locale}getFallbackLocale(){return this._fallbackLocale}getRoute(){return this._currentRoute}getRouteTranslations(e,l){const a=`${e}:${l}`;return this.storage.translations.get(a)??null}addTranslations(e,l,a=!0){super.loadTranslationsCore(e,l,a)}addRouteTranslations(e,l,a,i=!0){super.loadRouteTranslationsCore(e,l,a,i)}mergeTranslations(e,l,a){this.helper.mergeTranslation(e,l,a,!0)}clearCache(){const e={...this.initialMessages};if(super.clearCache(),Object.keys(e).length>0)for(const[l,a]of Object.entries(e))this.helper.loadTranslations(l,a)}}let F=null;function B(){return F}function C(t){const{locale:e,fallbackLocale:l,translationDir:a,routingStrategy:i}=t;return F=i||null,{name:"@i18n-micro/astro",hooks:{"astro:config:setup":c=>{const{updateConfig:u}=c,n="virtual:i18n-micro/config",s=`\0${n}`,r={defaultLocale:e,fallbackLocale:l||e,locales:t.locales||[],localeCodes:(t.locales||[]).map(o=>o.code),translationDir:a||null,autoDetect:t.autoDetect??!0,redirectToDefault:t.redirectToDefault??!1,localeCookie:t.localeCookie===null?null:t.localeCookie||"i18n-locale",missingWarn:t.missingWarn??!1};u({vite:{plugins:[{name:"vite-plugin-i18n-micro-config",resolveId(o){if(o===n)return s},load(o){if(o===s)return`export const config = ${JSON.stringify(r)}`}}]}})},"astro:config:done":c=>{const{injectTypes:u}=c;u({filename:"i18n-micro-env.d.ts",content:`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("@i18n-micro/core"),T=require("node:fs"),R=require("node:path"),F=require("@i18n-micro/utils/parse-path"),N=require("@i18n-micro/utils/accept-language");class v extends j.BaseI18n{constructor(e){const l=e._storage||{translations:new Map};if(super({storage:l,plural:e.plural,missingWarn:e.missingWarn,missingHandler:e.missingHandler}),this.initialMessages={},this.storage=l,this._locale=e.locale,this._fallbackLocale=e.fallbackLocale||e.locale,this._currentRoute="index",e.messages){this.initialMessages={...e.messages};for(const[r,c]of Object.entries(e.messages))this.helper.loadTranslations(r,c)}}cloneStorage(e){const l=new Map;for(const[r,c]of e.translations)l.set(r,{...c});return{translations:l}}clone(e){const l=this.cloneStorage(this.storage);return new v({locale:e||this._locale,fallbackLocale:this._fallbackLocale,plural:this.pluralFunc,missingWarn:this.missingWarn,missingHandler:this.missingHandler,_storage:l})}get locale(){return this._locale}set locale(e){this._locale=e}get fallbackLocale(){return this._fallbackLocale}set fallbackLocale(e){this._fallbackLocale=e}setRoute(e){this._currentRoute=e}getLocale(){return this._locale}getFallbackLocale(){return this._fallbackLocale}getRoute(){return this._currentRoute}getRouteTranslations(e,l){const r=`${e}:${l}`;return this.storage.translations.get(r)??null}addTranslations(e,l,r=!0){super.loadTranslationsCore(e,l,r)}addRouteTranslations(e,l,r,c=!0){super.loadRouteTranslationsCore(e,l,r,c)}mergeTranslations(e,l,r){this.helper.mergeTranslation(e,l,r,!0)}clearCache(){const e={...this.initialMessages};if(super.clearCache(),Object.keys(e).length>0)for(const[l,r]of Object.entries(e))this.helper.loadTranslations(l,r)}}let O=null;function _(){return O}function q(a){const{locale:e,fallbackLocale:l,translationDir:r,routingStrategy:c}=a;return O=c||null,{name:"@i18n-micro/astro",hooks:{"astro:config:setup":i=>{const{updateConfig:u}=i,t="virtual:i18n-micro/config",o=`\0${t}`,s={defaultLocale:e,fallbackLocale:l||e,locales:a.locales||[],localeCodes:(a.locales||[]).map(n=>n.code),translationDir:r||null,autoDetect:a.autoDetect??!0,redirectToDefault:a.redirectToDefault??!1,localeCookie:a.localeCookie===null?null:a.localeCookie||"i18n-locale",missingWarn:a.missingWarn??!1};u({vite:{plugins:[{name:"vite-plugin-i18n-micro-config",resolveId(n){if(n===t)return o},load(n){if(n===o)return`export const config = ${JSON.stringify(s)}`}}]}})},"astro:config:done":i=>{const{injectTypes:u}=i;u({filename:"i18n-micro-env.d.ts",content:`
2
2
  /// <reference types="@i18n-micro/astro/env" />
3
3
 
4
4
  declare module 'virtual:i18n-micro/config' {
@@ -14,4 +14,4 @@
14
14
  missingWarn: boolean | null;
15
15
  }
16
16
  }
17
- `})}}}}function N(t){return new T(t)}function O(t){const{translationDir:e,rootDir:l=process.cwd(),disablePageLocales:a=!1}=t,i=D.resolve(l,e);if(!j.existsSync(i))return console.warn(`[i18n] Translation directory not found: ${i}`),{root:{},routes:{}};const c={},u={},n=(s,r="")=>{if(!j.existsSync(s))return;const o=j.readdirSync(s);for(const f of o){const g=D.join(s,f);if(j.statSync(g).isDirectory())f==="pages"&&!a?n(g,""):r||a?n(g,r):n(g,f);else if(f.endsWith(".json")){const p=f.replace(".json","");try{const m=j.readFileSync(g,"utf-8"),P=JSON.parse(m);r&&!a?(u[r]||(u[r]={}),u[r][p]=P):c[p]=P}catch(m){console.error(`[i18n] Failed to load translation file: ${g}`,m)}}}};return n(i),{root:c,routes:u}}function q(t,e){const{root:l,routes:a}=O(e);for(const[i,c]of Object.entries(l))t.addTranslations(i,c,!1);for(const[i,c]of Object.entries(a))for(const[u,n]of Object.entries(c)){const s=l[u]||{};t.addRouteTranslations(u,i,{...s,...n},!1)}}function M(t){const{i18n:e,defaultLocale:l,locales:a,localeObjects:i,autoDetect:c=!0,redirectToDefault:u=!1,routingStrategy:n}=t,s=n||B();return async(r,o)=>{if(r.locals.locale&&r.locals.i18n)return o();const f=r.url,g=f.pathname;if(!s){const L=e.clone(l),S=g==="/"||g===""?"index":g.split("/").filter(Boolean).join("-");return L.setRoute(S),r.locals.i18n=L,r.locals.locale=l,r.locals.defaultLocale=l,r.locals.locales=i||a.map(w=>({code:w})),r.locals.currentUrl=f,o()}const h={...s,getCurrentPath:()=>g,getRoute:()=>({fullPath:f.pathname+f.search,query:Object.fromEntries(f.searchParams)})},m=g.split("/").filter(Boolean)[0],P=m!==void 0&&a.includes(m);let y;P&&m?y=m:h.getLocaleFromPath?y=h.getLocaleFromPath(g,l,a):y=l;const d=e.clone(y),b=h.getRouteName?h.getRouteName(g,a):"index";return d.setRoute(b),r.locals.i18n=d,r.locals.locale=y,r.locals.defaultLocale=l,r.locals.locales=i||a.map(L=>({code:L})),r.locals.currentUrl=f,r.locals.routingStrategy=h,o()}}function A(t){const e=[],l=t.split(",");for(const a of l){const[i,c="1.0"]=a.trim().split(";q=");if(Number.parseFloat(c)>0&&i){const n=i.split("-")[0]?.toLowerCase();n&&(e.push(n),i!==n&&e.push(i.toLowerCase()))}}return e}function W(t,e,l,a,i,c="i18n-locale"){const u=B();let n=a;if(u?.getLocaleFromPath)n=u.getLocaleFromPath(t,a,i);else{const r=t.split("/").filter(Boolean)[0];r&&i.includes(r)&&(n=r)}if(c!==null&&n===a&&e.get(c)){const s=e.get(c)?.value;s&&i.includes(s)&&(n=s)}if(n===a)try{const s=l.get("accept-language");if(s){const r=A(s);for(const o of r)if(i.includes(o)){n=o;break}}}catch{}return n}function z(t,e,l){const a=t.map(o=>o.code),i=(o,f=[])=>{const g=o.replace(/^\//,"").replace(/\/$/,"");if(!g)return"index";const h=g.split("/").filter(Boolean),p=h[0];return p&&f.includes(p)&&h.shift(),h.length===0?"index":h.join("-")},c=(o,f="en",g=[])=>{const p=o.split("/").filter(Boolean)[0];return p&&g.includes(p)?p:f},u=(o,f,g=[],h)=>{const p=o.split("/").filter(Boolean),m=p[0];return m&&g.includes(m)&&p.shift(),(f!==h||h===void 0)&&p.unshift(f),`/${p.join("/")}`},n=(o,f,g=[],h)=>{const m=(o.replace(/^\//,"").replace(/\/$/,"")||"").split("/").filter(Boolean),P=m[0];return P&&g.includes(P)&&m.shift(),(f!==h||h===void 0)&&m.unshift(f),`/${m.join("/")}`};return{getCurrentPath:()=>l?l().pathname:typeof window<"u"?window.location.pathname:"/",getRouteName:i,getLocaleFromPath:c,switchLocalePath:u,localizePath:n,removeLocaleFromPath:(o,f=[])=>{const g=o.split("/").filter(Boolean),h=g[0];return h&&f.includes(h)&&g.shift(),`/${g.join("/")}`},resolvePath:(o,f)=>{const g=typeof o=="string"?o:o.path||"/";return n(g,f,a,e)},getRoute:()=>{if(l){const o=l();return{fullPath:o.pathname+o.search,query:Object.fromEntries(o.searchParams)}}if(typeof window<"u"){const o=new URL(window.location.href);return{fullPath:o.pathname+o.search,query:Object.fromEntries(o.searchParams)}}return{fullPath:"/",query:{}}},push:o=>{typeof window<"u"&&(window.location.href=o.path)},replace:o=>{typeof window<"u"&&window.location.replace(o.path)}}}function H(t,e=[]){const l=t.replace(/^\//,"").replace(/\/$/,"");if(!l)return"index";const a=l.split("/").filter(Boolean),i=a[0];return i&&e.includes(i)&&a.shift(),a.length===0?"index":a.join("-")}function U(t,e="en",l=[]){const i=t.split("/").filter(Boolean)[0];return i&&l.includes(i)?i:e}function E(t,e,l=[],a){const i=t.split("/").filter(Boolean),c=i[0];return c&&l.includes(c)&&i.shift(),(e!==a||a===void 0)&&i.unshift(e),`/${i.join("/")}`}function J(t,e,l=[],a){const c=(t.replace(/^\//,"").replace(/\/$/,"")||"").split("/").filter(Boolean),u=c[0];return u&&l.includes(u)&&c.shift(),(e!==a||a===void 0)&&c.unshift(e),`/${c.join("/")}`}function V(t,e=[]){const l=t.split("/").filter(Boolean),a=l[0];return a&&e.includes(a)&&l.shift(),`/${l.join("/")}`}function k(t){const e=t.locals.i18n;if(!e)throw new Error("i18n instance not found. Make sure i18n middleware is configured.");return e}function v(t){return t.locals.locale||"en"}function $(t){return t.locals.defaultLocale||"en"}function I(t){return t.locals.locales||[]}function _(t){return t.locals.routingStrategy||null}function G(t){const e=k(t),l=v(t),a=$(t),i=I(t),c=i.map(n=>n.code),u=_(t);return{locale:l,defaultLocale:a,locales:i,t:(n,s,r,o)=>e.t(n,s,r,o),ts:(n,s,r,o)=>e.ts(n,s,r,o),tc:(n,s,r)=>e.tc(n,s,r),tn:(n,s)=>e.tn(n,s),td:(n,s)=>e.td(n,s),tdr:(n,s)=>e.tdr(n,s),has:(n,s)=>e.has(n,s),getRoute:()=>e.getRoute(),getRouteName:n=>{const s=n||t.url.pathname;if(u?.getRouteName)return u.getRouteName(s,c);const r=s.replace(/^\//,"").replace(/\/$/,"");if(!r)return"index";const o=r.split("/").filter(Boolean),f=o[0];return f&&c.includes(f)&&o.shift(),o.length===0?"index":o.join("-")},getLocaleFromPath:n=>{const s=n||t.url.pathname;if(u?.getLocaleFromPath)return u.getLocaleFromPath(s,a,c);const o=s.split("/").filter(Boolean)[0];return o&&c.includes(o)?o:a},switchLocalePath:n=>{if(u?.switchLocalePath)return u.switchLocalePath(t.url.pathname,n,c,a);const s=t.url.pathname.split("/").filter(Boolean),r=s[0];return r&&c.includes(r)&&s.shift(),n!==a&&s.unshift(n),`/${s.join("/")}`},localizePath:(n,s)=>{if(u?.localizePath)return u.localizePath(n,s||l,c,a);const o=(n.replace(/^\//,"").replace(/\/$/,"")||"").split("/").filter(Boolean),f=o[0];return f&&c.includes(f)&&o.shift(),s&&s!==a&&o.unshift(s),`/${o.join("/")}`},getI18n:()=>e,getBasePath:n=>{const o=(n||t.url).pathname.split("/").filter(Boolean),f=o[0];return f&&c.includes(f)&&o.shift(),o.length>0?`/${o.join("/")}`:"/"},addTranslations:(n,s,r=!0)=>{e.addTranslations(n,s,r)},addRouteTranslations:(n,s,r,o=!0)=>{e.addRouteTranslations(n,s,r,o)},mergeTranslations:(n,s,r)=>{e.mergeTranslations(n,s,r)},clearCache:()=>{e.clearCache()}}}function K(t,e={}){const{baseUrl:l="/",addDirAttribute:a=!0,addSeoAttributes:i=!0}=e,c=v(t),u=$(t),s=I(t).filter(d=>!d.disabled),r=s.filter(d=>d.seo!==!1),o=s.find(d=>d.code===c);if(!o)return{htmlAttrs:{},link:[],meta:[]};const f=o.iso||c,g=o.dir||"auto",h={htmlAttrs:{lang:f,...a?{dir:g}:{}},link:[],meta:[]};if(!i)return h;const p=`${l}${t.url.pathname}`;h.link.push({rel:"canonical",href:p});const m=_(t),P=s.map(d=>d.code);for(const d of r){let b=t.url.pathname;if(m?.switchLocalePath)b=m.switchLocalePath(t.url.pathname,d.code,P,u);else{const S=t.url.pathname.split("/").filter(Boolean),w=S[0];w&&P.includes(w)&&S.shift(),d.code!==u&&S.unshift(d.code),b=`/${S.join("/")}`}const L=`${l}${b}`;h.link.push({rel:"alternate",href:L,hreflang:d.code}),d.iso&&d.iso!==d.code&&h.link.push({rel:"alternate",href:L,hreflang:d.iso})}if(s.find(d=>d.code===u)?.seo!==!1){let d=t.url.pathname;if(m?.switchLocalePath)d=m.switchLocalePath(t.url.pathname,u,P,u);else{const b=t.url.pathname.split("/").filter(Boolean),L=b[0];L&&P.includes(L)&&b.shift(),d=`/${b.join("/")}`}h.link.push({rel:"alternate",href:`${l}${d}`,hreflang:"x-default"})}h.meta.push({property:"og:locale",content:f}),h.meta.push({property:"og:url",content:p});for(const d of r)d.code!==c&&h.meta.push({property:"og:locale:alternate",content:d.og||d.iso||d.code});return h}function Q(t,e,l){const a=e.split(".");let i=t;for(let u=0;u<a.length-1;u++){const n=a[u];i[n]||(i[n]={}),i=i[n]}const c=a[a.length-1];c!==void 0&&(i[c]=l)}function X(t,e){const l=k(t),a=v(t),i=$(t),c=l.getRoute(),u={};if(e&&e.length>0){const n={};for(const s of e){const r=l.t(s,void 0,void 0,c);r!=null&&r!==s&&Q(n,s,r)}Object.keys(n).length>0&&(u[c]=n)}else{const n=l.getRouteTranslations(a,c);n&&(u[c]=n)}return{locale:a,fallbackLocale:i,currentRoute:c,translations:u}}Object.defineProperty(exports,"FormatService",{enumerable:!0,get:()=>R.FormatService});Object.defineProperty(exports,"defaultPlural",{enumerable:!0,get:()=>R.defaultPlural});Object.defineProperty(exports,"interpolate",{enumerable:!0,get:()=>R.interpolate});exports.AstroI18n=T;exports.createAstroRouterAdapter=z;exports.createI18n=N;exports.createI18nMiddleware=M;exports.detectLocale=W;exports.getDefaultLocale=$;exports.getI18n=k;exports.getI18nProps=X;exports.getLocale=v;exports.getLocaleFromPath=U;exports.getLocales=I;exports.getRouteName=H;exports.i18nIntegration=C;exports.loadTranslationsFromDir=O;exports.loadTranslationsIntoI18n=q;exports.localizePath=J;exports.removeLocaleFromPath=V;exports.switchLocalePath=E;exports.useI18n=G;exports.useLocaleHead=K;
17
+ `})}}}}function A(a){return new v(a)}function B(a,e){if(T.existsSync(a))for(const l of T.readdirSync(a)){const r=R.join(a,l);if(T.statSync(r).isDirectory()){B(r,e);continue}l.endsWith(".json")&&e(r)}}function C(a){const{translationDir:e,rootDir:l=process.cwd(),disablePageLocales:r=!1}=a,c=R.resolve(l,e);if(!T.existsSync(c))return console.warn(`[i18n] Translation directory not found: ${c}`),{root:{},routes:{}};const i={root:{},routes:{}};return B(c,u=>{const t=R.relative(c,u).split(R.sep).join("/");try{const o=T.readFileSync(u,"utf-8"),s=JSON.parse(o);F.storeLoadedTranslationFile(i,t,s,r)}catch(o){console.error(`[i18n] Failed to load translation file: ${u}`,o)}}),i}function W(a,e){const{root:l,routes:r}=C(e);for(const[c,i]of Object.entries(l))a.addTranslations(c,i,!1);for(const[c,i]of Object.entries(r))for(const[u,t]of Object.entries(i))a.addRouteTranslations(u,c,F.mergeRouteTranslationsWithRoot(l[u],t),!1)}function H(a){const{i18n:e,defaultLocale:l,locales:r,localeObjects:c,autoDetect:i=!0,redirectToDefault:u=!1,routingStrategy:t}=a,o=t||_();return async(s,n)=>{if(s.locals.locale&&s.locals.i18n)return n();const f=s.url,h=f.pathname;if(!o){const P=e.clone(l),S=h==="/"||h===""?"index":h.split("/").filter(Boolean).join("-");return P.setRoute(S),s.locals.i18n=P,s.locals.locale=l,s.locals.defaultLocale=l,s.locals.locales=c||r.map(w=>({code:w})),s.locals.currentUrl=f,n()}const g={...o,getCurrentPath:()=>h,getRoute:()=>({fullPath:f.pathname+f.search,query:Object.fromEntries(f.searchParams)})},m=h.split("/").filter(Boolean)[0],L=m!==void 0&&r.includes(m);let y;L&&m?y=m:g.getLocaleFromPath?y=g.getLocaleFromPath(h,l,r):y=l;const d=e.clone(y),b=g.getRouteName?g.getRouteName(h,r):"index";return d.setRoute(b),s.locals.i18n=d,s.locals.locale=y,s.locals.defaultLocale=l,s.locals.locales=c||r.map(P=>({code:P})),s.locals.currentUrl=f,s.locals.routingStrategy=g,n()}}function U(a,e,l,r,c,i="i18n-locale"){const u=_();let t=r;if(u?.getLocaleFromPath)t=u.getLocaleFromPath(a,r,c);else{const s=a.split("/").filter(Boolean)[0];s&&c.includes(s)&&(t=s)}if(i!==null&&t===r&&e.get(i)){const o=e.get(i)?.value;o&&c.includes(o)&&(t=o)}if(t===r)try{const o=N.detectLocaleFromAcceptLanguage(l.get("accept-language")??void 0,c);o&&(t=o)}catch{}return t}function z(a,e,l){const r=a.map(n=>n.code),c=(n,f=[])=>{const h=n.replace(/^\//,"").replace(/\/$/,"");if(!h)return"index";const g=h.split("/").filter(Boolean),p=g[0];return p&&f.includes(p)&&g.shift(),g.length===0?"index":g.join("-")},i=(n,f="en",h=[])=>{const p=n.split("/").filter(Boolean)[0];return p&&h.includes(p)?p:f},u=(n,f,h=[],g)=>{const p=n.split("/").filter(Boolean),m=p[0];return m&&h.includes(m)&&p.shift(),(f!==g||g===void 0)&&p.unshift(f),`/${p.join("/")}`},t=(n,f,h=[],g)=>{const m=(n.replace(/^\//,"").replace(/\/$/,"")||"").split("/").filter(Boolean),L=m[0];return L&&h.includes(L)&&m.shift(),(f!==g||g===void 0)&&m.unshift(f),`/${m.join("/")}`};return{getCurrentPath:()=>l?l().pathname:typeof window<"u"?window.location.pathname:"/",getRouteName:c,getLocaleFromPath:i,switchLocalePath:u,localizePath:t,removeLocaleFromPath:(n,f=[])=>{const h=n.split("/").filter(Boolean),g=h[0];return g&&f.includes(g)&&h.shift(),`/${h.join("/")}`},resolvePath:(n,f)=>{const h=typeof n=="string"?n:n.path||"/";return t(h,f,r,e)},getRoute:()=>{if(l){const n=l();return{fullPath:n.pathname+n.search,query:Object.fromEntries(n.searchParams)}}if(typeof window<"u"){const n=new URL(window.location.href);return{fullPath:n.pathname+n.search,query:Object.fromEntries(n.searchParams)}}return{fullPath:"/",query:{}}},push:n=>{typeof window<"u"&&(window.location.href=n.path)},replace:n=>{typeof window<"u"&&window.location.replace(n.path)}}}function D(a){const e=a.locals.i18n;if(!e)throw new Error("i18n instance not found. Make sure i18n middleware is configured.");return e}function k(a){return a.locals.locale||"en"}function I(a){return a.locals.defaultLocale||"en"}function $(a){return a.locals.locales||[]}function M(a){return a.locals.routingStrategy||null}function E(a){const e=D(a),l=k(a),r=I(a),c=$(a),i=c.map(t=>t.code),u=M(a);return{locale:l,defaultLocale:r,locales:c,t:(t,o,s,n)=>e.t(t,o,s,n),ts:(t,o,s,n)=>e.ts(t,o,s,n),tc:(t,o,s)=>e.tc(t,o,s),tn:(t,o)=>e.tn(t,o),td:(t,o)=>e.td(t,o),tdr:(t,o)=>e.tdr(t,o),has:(t,o)=>e.has(t,o),getRoute:()=>e.getRoute(),getRouteName:t=>{const o=t||a.url.pathname;if(u?.getRouteName)return u.getRouteName(o,i);const s=o.replace(/^\//,"").replace(/\/$/,"");if(!s)return"index";const n=s.split("/").filter(Boolean),f=n[0];return f&&i.includes(f)&&n.shift(),n.length===0?"index":n.join("-")},getLocaleFromPath:t=>{const o=t||a.url.pathname;if(u?.getLocaleFromPath)return u.getLocaleFromPath(o,r,i);const n=o.split("/").filter(Boolean)[0];return n&&i.includes(n)?n:r},switchLocalePath:t=>{if(u?.switchLocalePath)return u.switchLocalePath(a.url.pathname,t,i,r);const o=a.url.pathname.split("/").filter(Boolean),s=o[0];return s&&i.includes(s)&&o.shift(),t!==r&&o.unshift(t),`/${o.join("/")}`},localizePath:(t,o)=>{if(u?.localizePath)return u.localizePath(t,o||l,i,r);const n=(t.replace(/^\//,"").replace(/\/$/,"")||"").split("/").filter(Boolean),f=n[0];return f&&i.includes(f)&&n.shift(),o&&o!==r&&n.unshift(o),`/${n.join("/")}`},getI18n:()=>e,getBasePath:t=>{const n=(t||a.url).pathname.split("/").filter(Boolean),f=n[0];return f&&i.includes(f)&&n.shift(),n.length>0?`/${n.join("/")}`:"/"},addTranslations:(t,o,s=!0)=>{e.addTranslations(t,o,s)},addRouteTranslations:(t,o,s,n=!0)=>{e.addRouteTranslations(t,o,s,n)},mergeTranslations:(t,o,s)=>{e.mergeTranslations(t,o,s)},clearCache:()=>{e.clearCache()}}}function x(a,e={}){const{baseUrl:l="/",addDirAttribute:r=!0,addSeoAttributes:c=!0}=e,i=k(a),u=I(a),o=$(a).filter(d=>!d.disabled),s=o.filter(d=>d.seo!==!1),n=o.find(d=>d.code===i);if(!n)return{htmlAttrs:{},link:[],meta:[]};const f=n.iso||i,h=n.dir||"auto",g={htmlAttrs:{lang:f,...r?{dir:h}:{}},link:[],meta:[]};if(!c)return g;const p=`${l}${a.url.pathname}`;g.link.push({rel:"canonical",href:p});const m=M(a),L=o.map(d=>d.code);for(const d of s){let b=a.url.pathname;if(m?.switchLocalePath)b=m.switchLocalePath(a.url.pathname,d.code,L,u);else{const S=a.url.pathname.split("/").filter(Boolean),w=S[0];w&&L.includes(w)&&S.shift(),d.code!==u&&S.unshift(d.code),b=`/${S.join("/")}`}const P=`${l}${b}`;g.link.push({rel:"alternate",href:P,hreflang:d.code}),d.iso&&d.iso!==d.code&&g.link.push({rel:"alternate",href:P,hreflang:d.iso})}if(o.find(d=>d.code===u)?.seo!==!1){let d=a.url.pathname;if(m?.switchLocalePath)d=m.switchLocalePath(a.url.pathname,u,L,u);else{const b=a.url.pathname.split("/").filter(Boolean),P=b[0];P&&L.includes(P)&&b.shift(),d=`/${b.join("/")}`}g.link.push({rel:"alternate",href:`${l}${d}`,hreflang:"x-default"})}g.meta.push({property:"og:locale",content:f}),g.meta.push({property:"og:url",content:p});for(const d of s)d.code!==i&&g.meta.push({property:"og:locale:alternate",content:d.og||d.iso||d.code});return g}function J(a,e,l){const r=e.split(".");let c=a;for(let u=0;u<r.length-1;u++){const t=r[u];c[t]||(c[t]={}),c=c[t]}const i=r[r.length-1];i!==void 0&&(c[i]=l)}function V(a,e){const l=D(a),r=k(a),c=I(a),i=l.getRoute(),u={};if(e&&e.length>0){const t={};for(const o of e){const s=l.t(o,void 0,void 0,i);s!=null&&s!==o&&J(t,o,s)}Object.keys(t).length>0&&(u[i]=t)}else{const t=l.getRouteTranslations(r,i);t&&(u[i]=t)}return{locale:r,fallbackLocale:c,currentRoute:i,translations:u}}Object.defineProperty(exports,"FormatService",{enumerable:!0,get:()=>j.FormatService});Object.defineProperty(exports,"defaultPlural",{enumerable:!0,get:()=>j.defaultPlural});Object.defineProperty(exports,"interpolate",{enumerable:!0,get:()=>j.interpolate});exports.AstroI18n=v;exports.createAstroRouterAdapter=z;exports.createI18n=A;exports.createI18nMiddleware=H;exports.detectLocale=U;exports.getDefaultLocale=I;exports.getI18n=D;exports.getI18nProps=V;exports.getLocale=k;exports.getLocales=$;exports.i18nIntegration=q;exports.loadTranslationsFromDir=C;exports.loadTranslationsIntoI18n=W;exports.useI18n=E;exports.useLocaleHead=x;
package/dist/index.d.cts CHANGED
@@ -1,14 +1,13 @@
1
1
  export { defaultPlural, FormatService, interpolate } from '@i18n-micro/core';
2
- export type { CleanTranslation, Getter, Locale, LocaleCode, Params, PluralFunc, Translations, } from '@i18n-micro/types';
2
+ export type { CleanTranslation, Getter, Locale, LocaleCode, Params, PluralFunc, Translations } from '@i18n-micro/types';
3
3
  export { AstroI18n, type AstroI18nOptions } from './composer';
4
4
  export type { I18nIntegrationOptions } from './integration';
5
5
  export { createI18n, i18nIntegration } from './integration';
6
6
  export type { LoadedTranslations, LoadTranslationsOptions } from './load-translations';
7
- export { loadTranslationsFromDir, loadTranslationsIntoI18n, } from './load-translations';
7
+ export { loadTranslationsFromDir, loadTranslationsIntoI18n } from './load-translations';
8
8
  export type { I18nMiddlewareOptions } from './middleware';
9
9
  export { createI18nMiddleware, detectLocale } from './middleware';
10
10
  export { createAstroRouterAdapter } from './router/adapter';
11
11
  export type { I18nRoutingStrategy } from './router/types';
12
- export { getLocaleFromPath, getRouteName, localizePath, removeLocaleFromPath, switchLocalePath, } from './routing';
13
12
  export type { I18nClientProps, LocaleHeadOptions, LocaleHeadResult } from './utils';
14
- export { getDefaultLocale, getI18n, getI18nProps, getLocale, getLocales, useI18n, useLocaleHead, } from './utils';
13
+ export { getDefaultLocale, getI18n, getI18nProps, getLocale, getLocales, useI18n, useLocaleHead } from './utils';
package/dist/index.d.ts CHANGED
@@ -1,14 +1,13 @@
1
1
  export { defaultPlural, FormatService, interpolate } from '@i18n-micro/core';
2
- export type { CleanTranslation, Getter, Locale, LocaleCode, Params, PluralFunc, Translations, } from '@i18n-micro/types';
2
+ export type { CleanTranslation, Getter, Locale, LocaleCode, Params, PluralFunc, Translations } from '@i18n-micro/types';
3
3
  export { AstroI18n, type AstroI18nOptions } from './composer';
4
4
  export type { I18nIntegrationOptions } from './integration';
5
5
  export { createI18n, i18nIntegration } from './integration';
6
6
  export type { LoadedTranslations, LoadTranslationsOptions } from './load-translations';
7
- export { loadTranslationsFromDir, loadTranslationsIntoI18n, } from './load-translations';
7
+ export { loadTranslationsFromDir, loadTranslationsIntoI18n } from './load-translations';
8
8
  export type { I18nMiddlewareOptions } from './middleware';
9
9
  export { createI18nMiddleware, detectLocale } from './middleware';
10
10
  export { createAstroRouterAdapter } from './router/adapter';
11
11
  export type { I18nRoutingStrategy } from './router/types';
12
- export { getLocaleFromPath, getRouteName, localizePath, removeLocaleFromPath, switchLocalePath, } from './routing';
13
12
  export type { I18nClientProps, LocaleHeadOptions, LocaleHeadResult } from './utils';
14
- export { getDefaultLocale, getI18n, getI18nProps, getLocale, getLocales, useI18n, useLocaleHead, } from './utils';
13
+ export { getDefaultLocale, getI18n, getI18nProps, getLocale, getLocales, useI18n, useLocaleHead } from './utils';