@i18n-micro/astro 1.1.0 → 1.2.0
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/client/vue.js +6 -6
- package/dist/composer.d.ts +4 -19
- package/dist/index.cjs +4 -4
- package/dist/index.mjs +264 -290
- package/dist/integration.d.ts +1 -1
- package/dist/middleware.d.ts +1 -1
- package/package.json +11 -11
- package/src/composer.ts +18 -90
- package/src/integration.ts +4 -4
- package/src/middleware.ts +3 -3
- package/src/utils.ts +8 -4
package/dist/client/vue.js
CHANGED
|
@@ -214,11 +214,11 @@ function Je(e) {
|
|
|
214
214
|
n !== e && (e.prevSub = n, n && (n.nextSub = e)), process.env.NODE_ENV !== "production" && e.dep.subsHead === void 0 && (e.dep.subsHead = e), e.dep.subs = e;
|
|
215
215
|
}
|
|
216
216
|
}
|
|
217
|
-
const Ne = /* @__PURE__ */ new WeakMap(), $ = Symbol(
|
|
217
|
+
const Ne = /* @__PURE__ */ new WeakMap(), $ = /* @__PURE__ */ Symbol(
|
|
218
218
|
process.env.NODE_ENV !== "production" ? "Object iterate" : ""
|
|
219
|
-
), we = Symbol(
|
|
219
|
+
), we = /* @__PURE__ */ Symbol(
|
|
220
220
|
process.env.NODE_ENV !== "production" ? "Map keys iterate" : ""
|
|
221
|
-
), G = Symbol(
|
|
221
|
+
), G = /* @__PURE__ */ Symbol(
|
|
222
222
|
process.env.NODE_ENV !== "production" ? "Array iterate" : ""
|
|
223
223
|
);
|
|
224
224
|
function v(e, t, n) {
|
|
@@ -1120,7 +1120,7 @@ function at(e, t) {
|
|
|
1120
1120
|
}
|
|
1121
1121
|
le().requestIdleCallback;
|
|
1122
1122
|
le().cancelIdleCallback;
|
|
1123
|
-
const xn = Symbol.for("v-ndc"), Vn = {};
|
|
1123
|
+
const xn = /* @__PURE__ */ Symbol.for("v-ndc"), Vn = {};
|
|
1124
1124
|
process.env.NODE_ENV !== "production" && (Vn.ownKeys = (e) => (V(
|
|
1125
1125
|
"Avoid app logic that relies on enumerating keys on a component instance. The keys will be empty in production mode to avoid performance overhead."
|
|
1126
1126
|
), Reflect.ownKeys(e)));
|
|
@@ -1143,7 +1143,7 @@ function Cn(e, t, n = !1) {
|
|
|
1143
1143
|
process.env.NODE_ENV !== "production" && V(`injection "${String(e)}" not found.`);
|
|
1144
1144
|
} else process.env.NODE_ENV !== "production" && V("inject() can only be used inside setup() or functional components.");
|
|
1145
1145
|
}
|
|
1146
|
-
const An = {}, lt = (e) => Object.getPrototypeOf(e) === An, Mn = (e) => e.__isSuspense, ut = Symbol.for("v-fgt"), Pn = Symbol.for("v-txt"), $n = Symbol.for("v-cmt");
|
|
1146
|
+
const An = {}, lt = (e) => Object.getPrototypeOf(e) === An, Mn = (e) => e.__isSuspense, ut = /* @__PURE__ */ Symbol.for("v-fgt"), Pn = /* @__PURE__ */ Symbol.for("v-txt"), $n = /* @__PURE__ */ Symbol.for("v-cmt");
|
|
1147
1147
|
function Fn(e) {
|
|
1148
1148
|
return e ? e.__v_isVNode === !0 : !1;
|
|
1149
1149
|
}
|
|
@@ -1492,7 +1492,7 @@ function Jn() {
|
|
|
1492
1492
|
Yn();
|
|
1493
1493
|
}
|
|
1494
1494
|
process.env.NODE_ENV !== "production" && Jn();
|
|
1495
|
-
const me = new St(), Et = Symbol("i18n-astro");
|
|
1495
|
+
const me = new St(), Et = /* @__PURE__ */ Symbol("i18n-astro");
|
|
1496
1496
|
function Gn(e) {
|
|
1497
1497
|
const t = sn({
|
|
1498
1498
|
locale: e.locale,
|
package/dist/composer.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BaseI18n,
|
|
1
|
+
import { BaseI18n, TranslationStorage } from '@i18n-micro/core';
|
|
2
2
|
import { Translations, PluralFunc } from '@i18n-micro/types';
|
|
3
3
|
export interface AstroI18nOptions {
|
|
4
4
|
locale: string;
|
|
@@ -7,40 +7,25 @@ export interface AstroI18nOptions {
|
|
|
7
7
|
plural?: PluralFunc;
|
|
8
8
|
missingWarn?: boolean;
|
|
9
9
|
missingHandler?: (locale: string, key: string, routeName: string) => void;
|
|
10
|
-
|
|
10
|
+
_storage?: TranslationStorage;
|
|
11
11
|
}
|
|
12
12
|
export declare class AstroI18n extends BaseI18n {
|
|
13
13
|
private _locale;
|
|
14
14
|
private _fallbackLocale;
|
|
15
15
|
private _currentRoute;
|
|
16
|
-
readonly
|
|
16
|
+
readonly storage: TranslationStorage;
|
|
17
17
|
private initialMessages;
|
|
18
18
|
constructor(options: AstroI18nOptions);
|
|
19
|
-
|
|
20
|
-
* Clone cache with shallow copy to prevent memory leaks
|
|
21
|
-
* Each request-scoped instance gets its own cache structure,
|
|
22
|
-
* but can read from the global cache (read-only access to existing translations)
|
|
23
|
-
*/
|
|
24
|
-
private cloneCache;
|
|
25
|
-
/**
|
|
26
|
-
* Create a request-scoped instance with isolated cache
|
|
27
|
-
* Prevents memory leaks by isolating per-request translations from global cache
|
|
28
|
-
*/
|
|
19
|
+
private cloneStorage;
|
|
29
20
|
clone(newLocale?: string): AstroI18n;
|
|
30
21
|
get locale(): string;
|
|
31
22
|
set locale(val: string);
|
|
32
23
|
get fallbackLocale(): string;
|
|
33
24
|
set fallbackLocale(val: string);
|
|
34
|
-
get currentRoute(): string;
|
|
35
25
|
setRoute(routeName: string): void;
|
|
36
26
|
getLocale(): string;
|
|
37
27
|
getFallbackLocale(): string;
|
|
38
28
|
getRoute(): string;
|
|
39
|
-
/**
|
|
40
|
-
* Get route-specific translations for a given locale and route
|
|
41
|
-
* This method encapsulates the cache key format, making it safe to use
|
|
42
|
-
* without direct cache access
|
|
43
|
-
*/
|
|
44
29
|
getRouteTranslations(locale: string, routeName: string): Translations | null;
|
|
45
30
|
addTranslations(locale: string, translations: Translations, merge?: boolean): void;
|
|
46
31
|
addRouteTranslations(locale: string, routeName: string, translations: Translations, merge?: boolean): void;
|
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 T=require("@i18n-micro/core"),b=require("node:fs"),I=require("node:path");class j extends T.BaseI18n{constructor(e){const s=e._storage||{translations:new Map};if(super({storage:s,plural:e.plural,missingWarn:e.missingWarn,missingHandler:e.missingHandler}),this.initialMessages={},this.storage=s,this._locale=e.locale,this._fallbackLocale=e.fallbackLocale||e.locale,this._currentRoute="general",e.messages){this.initialMessages={...e.messages};for(const[a,i]of Object.entries(e.messages))this.helper.loadTranslations(a,i)}}cloneStorage(e){const s=new Map;for(const[a,i]of e.translations)s.set(a,{...i});return{translations:s}}clone(e){const s=this.cloneStorage(this.storage);return new j({locale:e||this._locale,fallbackLocale:this._fallbackLocale,plural:this.pluralFunc,missingWarn:this.missingWarn,missingHandler:this.missingHandler,_storage:s})}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,s){const a=`${e}:${s}`;return this.storage.translations.get(a)??null}addTranslations(e,s,a=!0){super.loadTranslationsCore(e,s,a)}addRouteTranslations(e,s,a,i=!0){super.loadRouteTranslationsCore(e,s,a,i)}mergeTranslations(e,s,a){this.helper.mergeTranslation(e,s,a,!0)}mergeGlobalTranslations(e,s){this.helper.mergeGlobalTranslation(e,s,!0)}clearCache(){const e={...this.initialMessages};if(super.clearCache(),Object.keys(e).length>0)for(const[s,a]of Object.entries(e))this.helper.loadTranslations(s,a)}}let $=null;function D(){return $}function C(n){const{locale:e,fallbackLocale:s,translationDir:a,routingStrategy:i}=n;return $=i||null,{name:"@i18n-micro/astro",hooks:{"astro:config:setup":c=>{const{updateConfig:f}=c,t="virtual:i18n-micro/config",l="\0"+t,r={defaultLocale:e,fallbackLocale:s||e,locales:n.locales||[],localeCodes:(n.locales||[]).map(o=>o.code),translationDir:a||null,autoDetect:n.autoDetect??!0,redirectToDefault:n.redirectToDefault??!1,localeCookie:n.localeCookie===null?null:n.localeCookie||"i18n-locale",missingWarn:n.missingWarn??!1};f({vite:{plugins:[{name:"vite-plugin-i18n-micro-config",resolveId(o){if(o===t)return l},load(o){if(o===l)return`export const config = ${JSON.stringify(r)}`}}]}})},"astro:config:done":c=>{const{injectTypes:f}=c;f({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' {
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
translationDir: string | null;
|
|
11
11
|
autoDetect: boolean;
|
|
12
12
|
redirectToDefault: boolean;
|
|
13
|
-
localeCookie: string;
|
|
14
|
-
missingWarn: boolean;
|
|
13
|
+
localeCookie: string | null;
|
|
14
|
+
missingWarn: boolean | null;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
`})}}}}function
|
|
17
|
+
`})}}}}function N(n){return new j(n)}function q(n){const{i18n:e,defaultLocale:s,locales:a,localeObjects:i,autoDetect:c=!0,redirectToDefault:f=!1,routingStrategy:t}=n,l=t||D();return async(r,o)=>{if(r.locals.locale&&r.locals.i18n)return o();const u=r.url,h=u.pathname;if(!l){const S=e.clone(s),O=h==="/"||h===""?"index":h.split("/").filter(Boolean).join("-");return S.setRoute(O),r.locals.i18n=S,r.locals.locale=s,r.locals.defaultLocale=s,r.locals.locales=i||a.map(_=>({code:_})),r.locals.currentUrl=u,o()}const d={...l,getCurrentPath:()=>h,getRoute:()=>({fullPath:u.pathname+u.search,query:Object.fromEntries(u.searchParams)})},g=h.split("/").filter(Boolean)[0],p=g!==void 0&&a.includes(g);let P;p&&g?P=g:d.getLocaleFromPath?P=d.getLocaleFromPath(h,s,a):P=s;const L=e.clone(P),y=d.getRouteName?d.getRouteName(h,a):"general";return L.setRoute(y),r.locals.i18n=L,r.locals.locale=P,r.locals.defaultLocale=s,r.locals.locales=i||a.map(S=>({code:S})),r.locals.currentUrl=u,r.locals.routingStrategy=d,o()}}function M(n){const e=[],s=n.split(",");for(const a of s){const[i,c="1.0"]=a.trim().split(";q=");if(Number.parseFloat(c)>0&&i){const t=i.split("-")[0]?.toLowerCase();t&&(e.push(t),i!==t&&e.push(i.toLowerCase()))}}return e}function A(n,e,s,a,i,c="i18n-locale"){const f=D();let t=a;if(f?.getLocaleFromPath)t=f.getLocaleFromPath(n,a,i);else{const r=n.split("/").filter(Boolean)[0];r&&i.includes(r)&&(t=r)}if(c!==null&&t===a&&e.get(c)){const l=e.get(c)?.value;l&&i.includes(l)&&(t=l)}if(t===a)try{const l=s.get("accept-language");if(l){const r=M(l);for(const o of r)if(i.includes(o)){t=o;break}}}catch{}return t}function v(n){const e=n.locals.i18n;if(!e)throw new Error("i18n instance not found. Make sure i18n middleware is configured.");return e}function w(n){return n.locals.locale||"en"}function R(n){return n.locals.defaultLocale||"en"}function k(n){return n.locals.locales||[]}function F(n){return n.locals.routingStrategy||null}function W(n){const e=v(n),s=w(n),a=R(n),i=k(n),c=i.map(t=>t.code),f=F(n);return{locale:s,defaultLocale:a,locales:i,t:(t,l,r,o)=>e.t(t,l,r,o),ts:(t,l,r,o)=>e.ts(t,l,r,o),tc:(t,l,r)=>e.tc(t,l,r),tn:(t,l)=>e.tn(t,l),td:(t,l)=>e.td(t,l),tdr:(t,l)=>e.tdr(t,l),has:(t,l)=>e.has(t,l),getRoute:()=>e.getRoute(),getRouteName:t=>{const l=t||n.url.pathname;if(f?.getRouteName)return f.getRouteName(l,c);const r=l.replace(/^\//,"").replace(/\/$/,"");if(!r)return"index";const o=r.split("/").filter(Boolean),u=o[0];return u&&c.includes(u)&&o.shift(),o.length===0?"index":o.join("-")},getLocaleFromPath:t=>{const l=t||n.url.pathname;if(f?.getLocaleFromPath)return f.getLocaleFromPath(l,a,c);const o=l.split("/").filter(Boolean)[0];return o&&c.includes(o)?o:a},switchLocalePath:t=>{if(f?.switchLocalePath)return f.switchLocalePath(n.url.pathname,t,c,a);const l=n.url.pathname.split("/").filter(Boolean),r=l[0];return r&&c.includes(r)&&l.shift(),t!==a&&l.unshift(t),`/${l.join("/")}`},localizePath:(t,l)=>{if(f?.localizePath)return f.localizePath(t,l||s,c,a);const o=(t.replace(/^\//,"").replace(/\/$/,"")||"").split("/").filter(Boolean),u=o[0];return u&&c.includes(u)&&o.shift(),l&&l!==a&&o.unshift(l),`/${o.join("/")}`},getI18n:()=>e,getBasePath:t=>{const o=(t||n.url).pathname.split("/").filter(Boolean),u=o[0];return u&&c.includes(u)&&o.shift(),o.length>0?`/${o.join("/")}`:"/"},addTranslations:(t,l,r=!0)=>{e.addTranslations(t,l,r)},addRouteTranslations:(t,l,r,o=!0)=>{e.addRouteTranslations(t,l,r,o)},mergeTranslations:(t,l,r)=>{e.mergeTranslations(t,l,r)},mergeGlobalTranslations:(t,l)=>{e.mergeGlobalTranslations(t,l)},clearCache:()=>{e.clearCache()}}}function z(n,e={}){const{baseUrl:s="/",addDirAttribute:a=!0,addSeoAttributes:i=!0}=e,c=w(n),f=R(n),t=k(n),l=t.find(g=>g.code===c);if(!l)return{htmlAttrs:{},link:[],meta:[]};const r=l.iso||c,o=l.dir||"auto",u={htmlAttrs:{lang:r,...a?{dir:o}:{}},link:[],meta:[]};if(!i)return u;const h=`${s}${n.url.pathname}`;u.link.push({rel:"canonical",href:h});const d=F(n),m=t.map(g=>g.code);for(const g of t){if(g.code===c)continue;let p=n.url.pathname;if(d?.switchLocalePath)p=d.switchLocalePath(n.url.pathname,g.code,m,f);else{const L=n.url.pathname.split("/").filter(Boolean),y=L[0];y&&m.includes(y)&&L.shift(),g.code!==f&&L.unshift(g.code),p=`/${L.join("/")}`}const P=`${s}${p}`;u.link.push({rel:"alternate",href:P,hreflang:g.code}),g.iso&&g.iso!==g.code&&u.link.push({rel:"alternate",href:P,hreflang:g.iso})}u.meta.push({property:"og:locale",content:r}),u.meta.push({property:"og:url",content:h});for(const g of t)g.code!==c&&u.meta.push({property:"og:locale:alternate",content:g.iso||g.code});return u}function H(n,e,s){const a=e.split(".");let i=n;for(let f=0;f<a.length-1;f++){const t=a[f];i[t]||(i[t]={}),i=i[t]}const c=a[a.length-1];c!==void 0&&(i[c]=s)}function G(n,e){const s=v(n),a=w(n),i=R(n),c=s.getRoute(),f={};if(e&&e.length>0){const t={};for(const l of e){const r=s.t(l,void 0,void 0,c);r!=null&&r!==l&&H(t,l,r)}Object.keys(t).length>0&&(f[c]=t)}else{const t=s.getRouteTranslations(a,c);t&&(f[c]=t)}return{locale:a,fallbackLocale:i,currentRoute:c,translations:f}}function U(n,e,s){const a=n.map(o=>o.code),i=(o,u=[])=>{const h=o.replace(/^\//,"").replace(/\/$/,"");if(!h)return"index";const d=h.split("/").filter(Boolean),m=d[0];return m&&u.includes(m)&&d.shift(),d.length===0?"index":d.join("-")},c=(o,u="en",h=[])=>{const m=o.split("/").filter(Boolean)[0];return m&&h.includes(m)?m:u},f=(o,u,h=[],d)=>{const m=o.split("/").filter(Boolean),g=m[0];return g&&h.includes(g)&&m.shift(),(u!==d||d===void 0)&&m.unshift(u),`/${m.join("/")}`},t=(o,u,h=[],d)=>{const g=(o.replace(/^\//,"").replace(/\/$/,"")||"").split("/").filter(Boolean),p=g[0];return p&&h.includes(p)&&g.shift(),(u!==d||d===void 0)&&g.unshift(u),`/${g.join("/")}`};return{getCurrentPath:()=>s?s().pathname:typeof window<"u"?window.location.pathname:"/",getRouteName:i,getLocaleFromPath:c,switchLocalePath:f,localizePath:t,removeLocaleFromPath:(o,u=[])=>{const h=o.split("/").filter(Boolean),d=h[0];return d&&u.includes(d)&&h.shift(),`/${h.join("/")}`},resolvePath:(o,u)=>{const h=typeof o=="string"?o:o.path||"/";return t(h,u,a,e)},getRoute:()=>{if(s){const o=s();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 E(n,e=[]){const s=n.replace(/^\//,"").replace(/\/$/,"");if(!s)return"index";const a=s.split("/").filter(Boolean),i=a[0];return i&&e.includes(i)&&a.shift(),a.length===0?"index":a.join("-")}function J(n,e="en",s=[]){const i=n.split("/").filter(Boolean)[0];return i&&s.includes(i)?i:e}function V(n,e,s=[],a){const i=n.split("/").filter(Boolean),c=i[0];return c&&s.includes(c)&&i.shift(),(e!==a||a===void 0)&&i.unshift(e),`/${i.join("/")}`}function K(n,e,s=[],a){const c=(n.replace(/^\//,"").replace(/\/$/,"")||"").split("/").filter(Boolean),f=c[0];return f&&s.includes(f)&&c.shift(),(e!==a||a===void 0)&&c.unshift(e),`/${c.join("/")}`}function Q(n,e=[]){const s=n.split("/").filter(Boolean),a=s[0];return a&&e.includes(a)&&s.shift(),`/${s.join("/")}`}function B(n){const{translationDir:e,rootDir:s=process.cwd(),disablePageLocales:a=!1}=n,i=I.resolve(s,e);if(!b.existsSync(i))return console.warn(`[i18n] Translation directory not found: ${i}`),{general:{},routes:{}};const c={},f={},t=(l,r="")=>{if(!b.existsSync(l))return;const o=b.readdirSync(l);for(const u of o){const h=I.join(l,u);if(b.statSync(h).isDirectory())u==="pages"&&!a?t(h,""):r||a?t(h,r):t(h,u);else if(u.endsWith(".json")){const m=u.replace(".json","");try{const g=b.readFileSync(h,"utf-8"),p=JSON.parse(g);r&&!a?(f[r]||(f[r]={}),f[r][m]=p):c[m]=p}catch(g){console.error(`[i18n] Failed to load translation file: ${h}`,g)}}}};return t(i),{general:c,routes:f}}function X(n,e){const{general:s,routes:a}=B(e);for(const[i,c]of Object.entries(s))n.addTranslations(i,c,!1);for(const[i,c]of Object.entries(a))for(const[f,t]of Object.entries(c))n.addRouteTranslations(f,i,t,!1)}Object.defineProperty(exports,"FormatService",{enumerable:!0,get:()=>T.FormatService});Object.defineProperty(exports,"defaultPlural",{enumerable:!0,get:()=>T.defaultPlural});Object.defineProperty(exports,"interpolate",{enumerable:!0,get:()=>T.interpolate});exports.AstroI18n=j;exports.createAstroRouterAdapter=U;exports.createI18n=N;exports.createI18nMiddleware=q;exports.detectLocale=A;exports.getDefaultLocale=R;exports.getI18n=v;exports.getI18nProps=G;exports.getLocale=w;exports.getLocaleFromPath=J;exports.getLocales=k;exports.getRouteName=E;exports.i18nIntegration=C;exports.loadTranslationsFromDir=B;exports.loadTranslationsIntoI18n=X;exports.localizePath=K;exports.removeLocaleFromPath=Q;exports.switchLocalePath=V;exports.useI18n=W;exports.useLocaleHead=z;
|