@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 +2 -2
- package/dist/index.d.cts +3 -4
- package/dist/index.d.ts +3 -4
- package/dist/index.mjs +264 -308
- package/dist/load-translations.d.ts +2 -3
- package/package.json +42 -41
- package/src/index.ts +3 -31
- package/src/load-translations.ts +34 -56
- package/src/middleware.ts +4 -35
- package/dist/routing.d.ts +0 -23
- package/src/routing.ts +0 -94
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
13
|
+
export { getDefaultLocale, getI18n, getI18nProps, getLocale, getLocales, useI18n, useLocaleHead } from './utils';
|