@i18n-micro/astro 1.3.5 → 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 +240 -268
- package/package.json +42 -42
- package/src/index.ts +3 -31
- 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 A(
|
|
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';
|
package/dist/index.mjs
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
1
|
import { BaseI18n as O } from "@i18n-micro/core";
|
|
2
|
-
import { FormatService as
|
|
3
|
-
import { existsSync as
|
|
2
|
+
import { FormatService as ie, defaultPlural as ce, interpolate as ue } from "@i18n-micro/core";
|
|
3
|
+
import { existsSync as k, readdirSync as _, statSync as C, readFileSync as N } from "node:fs";
|
|
4
4
|
import { resolve as M, join as W, relative as A, sep as q } from "node:path";
|
|
5
|
-
import { mergeRouteTranslationsWithRoot as H, storeLoadedTranslationFile as
|
|
5
|
+
import { mergeRouteTranslationsWithRoot as H, storeLoadedTranslationFile as U } from "@i18n-micro/utils/parse-path";
|
|
6
6
|
import { detectLocaleFromAcceptLanguage as z } from "@i18n-micro/utils/accept-language";
|
|
7
|
-
class
|
|
7
|
+
class R extends O {
|
|
8
8
|
constructor(e) {
|
|
9
|
-
const
|
|
9
|
+
const l = e._storage || {
|
|
10
10
|
translations: /* @__PURE__ */ new Map()
|
|
11
11
|
};
|
|
12
12
|
if (super({
|
|
13
|
-
storage:
|
|
13
|
+
storage: l,
|
|
14
14
|
plural: e.plural,
|
|
15
15
|
missingWarn: e.missingWarn,
|
|
16
16
|
missingHandler: e.missingHandler
|
|
17
|
-
}), this.initialMessages = {}, this.storage =
|
|
17
|
+
}), this.initialMessages = {}, this.storage = l, this._locale = e.locale, this._fallbackLocale = e.fallbackLocale || e.locale, this._currentRoute = "index", e.messages) {
|
|
18
18
|
this.initialMessages = { ...e.messages };
|
|
19
|
-
for (const [
|
|
20
|
-
this.helper.loadTranslations(
|
|
19
|
+
for (const [r, i] of Object.entries(e.messages))
|
|
20
|
+
this.helper.loadTranslations(r, i);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
cloneStorage(e) {
|
|
24
|
-
const
|
|
25
|
-
for (const [
|
|
26
|
-
|
|
27
|
-
return { translations:
|
|
24
|
+
const l = /* @__PURE__ */ new Map();
|
|
25
|
+
for (const [r, i] of e.translations)
|
|
26
|
+
l.set(r, { ...i });
|
|
27
|
+
return { translations: l };
|
|
28
28
|
}
|
|
29
29
|
clone(e) {
|
|
30
|
-
const
|
|
31
|
-
return new
|
|
30
|
+
const l = this.cloneStorage(this.storage);
|
|
31
|
+
return new R({
|
|
32
32
|
locale: e || this._locale,
|
|
33
33
|
fallbackLocale: this._fallbackLocale,
|
|
34
34
|
plural: this.pluralFunc,
|
|
35
35
|
missingWarn: this.missingWarn,
|
|
36
36
|
missingHandler: this.missingHandler,
|
|
37
|
-
_storage:
|
|
37
|
+
_storage: l
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
get locale() {
|
|
@@ -61,60 +61,60 @@ class j extends O {
|
|
|
61
61
|
getRoute() {
|
|
62
62
|
return this._currentRoute;
|
|
63
63
|
}
|
|
64
|
-
getRouteTranslations(e,
|
|
65
|
-
const
|
|
66
|
-
return this.storage.translations.get(
|
|
64
|
+
getRouteTranslations(e, l) {
|
|
65
|
+
const r = `${e}:${l}`;
|
|
66
|
+
return this.storage.translations.get(r) ?? null;
|
|
67
67
|
}
|
|
68
|
-
addTranslations(e,
|
|
69
|
-
super.loadTranslationsCore(e,
|
|
68
|
+
addTranslations(e, l, r = !0) {
|
|
69
|
+
super.loadTranslationsCore(e, l, r);
|
|
70
70
|
}
|
|
71
|
-
addRouteTranslations(e,
|
|
72
|
-
super.loadRouteTranslationsCore(e,
|
|
71
|
+
addRouteTranslations(e, l, r, i = !0) {
|
|
72
|
+
super.loadRouteTranslationsCore(e, l, r, i);
|
|
73
73
|
}
|
|
74
|
-
mergeTranslations(e,
|
|
75
|
-
this.helper.mergeTranslation(e,
|
|
74
|
+
mergeTranslations(e, l, r) {
|
|
75
|
+
this.helper.mergeTranslation(e, l, r, !0);
|
|
76
76
|
}
|
|
77
77
|
clearCache() {
|
|
78
78
|
const e = { ...this.initialMessages };
|
|
79
79
|
if (super.clearCache(), Object.keys(e).length > 0)
|
|
80
|
-
for (const [
|
|
81
|
-
this.helper.loadTranslations(
|
|
80
|
+
for (const [l, r] of Object.entries(e))
|
|
81
|
+
this.helper.loadTranslations(l, r);
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
|
-
let
|
|
85
|
-
function
|
|
86
|
-
return
|
|
84
|
+
let v = null;
|
|
85
|
+
function $() {
|
|
86
|
+
return v;
|
|
87
87
|
}
|
|
88
|
-
function X(
|
|
89
|
-
const { locale: e, fallbackLocale:
|
|
90
|
-
return
|
|
88
|
+
function X(o) {
|
|
89
|
+
const { locale: e, fallbackLocale: l, translationDir: r, routingStrategy: i } = o;
|
|
90
|
+
return v = i || null, {
|
|
91
91
|
name: "@i18n-micro/astro",
|
|
92
92
|
hooks: {
|
|
93
93
|
// 1. Vite setup (virtual module) happens here
|
|
94
94
|
"astro:config:setup": (c) => {
|
|
95
|
-
const { updateConfig: u } = c,
|
|
95
|
+
const { updateConfig: u } = c, t = "virtual:i18n-micro/config", a = `\0${t}`, s = {
|
|
96
96
|
defaultLocale: e,
|
|
97
|
-
fallbackLocale:
|
|
98
|
-
locales:
|
|
99
|
-
localeCodes: (
|
|
100
|
-
translationDir:
|
|
101
|
-
autoDetect:
|
|
102
|
-
redirectToDefault:
|
|
103
|
-
localeCookie:
|
|
104
|
-
missingWarn:
|
|
97
|
+
fallbackLocale: l || e,
|
|
98
|
+
locales: o.locales || [],
|
|
99
|
+
localeCodes: (o.locales || []).map((n) => n.code),
|
|
100
|
+
translationDir: r || null,
|
|
101
|
+
autoDetect: o.autoDetect ?? !0,
|
|
102
|
+
redirectToDefault: o.redirectToDefault ?? !1,
|
|
103
|
+
localeCookie: o.localeCookie === null ? null : o.localeCookie || "i18n-locale",
|
|
104
|
+
missingWarn: o.missingWarn ?? !1
|
|
105
105
|
};
|
|
106
106
|
u({
|
|
107
107
|
vite: {
|
|
108
108
|
plugins: [
|
|
109
109
|
{
|
|
110
110
|
name: "vite-plugin-i18n-micro-config",
|
|
111
|
-
resolveId(
|
|
112
|
-
if (
|
|
113
|
-
return
|
|
111
|
+
resolveId(n) {
|
|
112
|
+
if (n === t)
|
|
113
|
+
return a;
|
|
114
114
|
},
|
|
115
|
-
load(
|
|
116
|
-
if (
|
|
117
|
-
return `export const config = ${JSON.stringify(
|
|
115
|
+
load(n) {
|
|
116
|
+
if (n === a)
|
|
117
|
+
return `export const config = ${JSON.stringify(s)}`;
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
120
|
]
|
|
@@ -148,141 +148,141 @@ function X(t) {
|
|
|
148
148
|
}
|
|
149
149
|
};
|
|
150
150
|
}
|
|
151
|
-
function Y(
|
|
152
|
-
return new
|
|
151
|
+
function Y(o) {
|
|
152
|
+
return new R(o);
|
|
153
153
|
}
|
|
154
|
-
function D(
|
|
155
|
-
if (
|
|
156
|
-
for (const
|
|
157
|
-
const
|
|
158
|
-
if (C(
|
|
159
|
-
D(
|
|
154
|
+
function D(o, e) {
|
|
155
|
+
if (k(o))
|
|
156
|
+
for (const l of _(o)) {
|
|
157
|
+
const r = W(o, l);
|
|
158
|
+
if (C(r).isDirectory()) {
|
|
159
|
+
D(r, e);
|
|
160
160
|
continue;
|
|
161
161
|
}
|
|
162
|
-
|
|
162
|
+
l.endsWith(".json") && e(r);
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
|
-
function
|
|
166
|
-
const { translationDir: e, rootDir:
|
|
167
|
-
if (!
|
|
168
|
-
return console.warn(`[i18n] Translation directory not found: ${
|
|
165
|
+
function E(o) {
|
|
166
|
+
const { translationDir: e, rootDir: l = process.cwd(), disablePageLocales: r = !1 } = o, i = M(l, e);
|
|
167
|
+
if (!k(i))
|
|
168
|
+
return console.warn(`[i18n] Translation directory not found: ${i}`), { root: {}, routes: {} };
|
|
169
169
|
const c = { root: {}, routes: {} };
|
|
170
|
-
return D(
|
|
171
|
-
const
|
|
170
|
+
return D(i, (u) => {
|
|
171
|
+
const t = A(i, u).split(q).join("/");
|
|
172
172
|
try {
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
} catch (
|
|
176
|
-
console.error(`[i18n] Failed to load translation file: ${u}`,
|
|
173
|
+
const a = N(u, "utf-8"), s = JSON.parse(a);
|
|
174
|
+
U(c, t, s, r);
|
|
175
|
+
} catch (a) {
|
|
176
|
+
console.error(`[i18n] Failed to load translation file: ${u}`, a);
|
|
177
177
|
}
|
|
178
178
|
}), c;
|
|
179
179
|
}
|
|
180
|
-
function Z(
|
|
181
|
-
const { root:
|
|
182
|
-
for (const [
|
|
183
|
-
|
|
184
|
-
for (const [
|
|
185
|
-
for (const [u,
|
|
186
|
-
|
|
180
|
+
function Z(o, e) {
|
|
181
|
+
const { root: l, routes: r } = E(e);
|
|
182
|
+
for (const [i, c] of Object.entries(l))
|
|
183
|
+
o.addTranslations(i, c, !1);
|
|
184
|
+
for (const [i, c] of Object.entries(r))
|
|
185
|
+
for (const [u, t] of Object.entries(c))
|
|
186
|
+
o.addRouteTranslations(u, i, H(l[u], t), !1);
|
|
187
187
|
}
|
|
188
|
-
function ee(
|
|
188
|
+
function ee(o) {
|
|
189
189
|
const {
|
|
190
190
|
i18n: e,
|
|
191
191
|
// This is the global singleton with cache
|
|
192
|
-
defaultLocale:
|
|
193
|
-
locales:
|
|
194
|
-
localeObjects:
|
|
192
|
+
defaultLocale: l,
|
|
193
|
+
locales: r,
|
|
194
|
+
localeObjects: i,
|
|
195
195
|
autoDetect: c = !0,
|
|
196
196
|
redirectToDefault: u = !1,
|
|
197
|
-
routingStrategy:
|
|
198
|
-
} =
|
|
199
|
-
return async (
|
|
200
|
-
if (
|
|
201
|
-
return
|
|
202
|
-
const f =
|
|
203
|
-
if (!
|
|
204
|
-
const
|
|
205
|
-
return
|
|
197
|
+
routingStrategy: t
|
|
198
|
+
} = o, a = t || $();
|
|
199
|
+
return async (s, n) => {
|
|
200
|
+
if (s.locals.locale && s.locals.i18n)
|
|
201
|
+
return n();
|
|
202
|
+
const f = s.url, d = f.pathname;
|
|
203
|
+
if (!a) {
|
|
204
|
+
const L = e.clone(l), S = d === "/" || d === "" ? "index" : d.split("/").filter(Boolean).join("-");
|
|
205
|
+
return L.setRoute(S), s.locals.i18n = L, s.locals.locale = l, s.locals.defaultLocale = l, s.locals.locales = i || r.map((w) => ({ code: w })), s.locals.currentUrl = f, n();
|
|
206
206
|
}
|
|
207
207
|
const g = {
|
|
208
|
-
...
|
|
208
|
+
...a,
|
|
209
209
|
getCurrentPath: () => d,
|
|
210
210
|
getRoute: () => ({
|
|
211
211
|
fullPath: f.pathname + f.search,
|
|
212
212
|
query: Object.fromEntries(f.searchParams)
|
|
213
213
|
})
|
|
214
|
-
}, m = d.split("/").filter(Boolean)[0],
|
|
215
|
-
let
|
|
216
|
-
|
|
217
|
-
const h = e.clone(
|
|
218
|
-
return h.setRoute(
|
|
214
|
+
}, m = d.split("/").filter(Boolean)[0], P = m !== void 0 && r.includes(m);
|
|
215
|
+
let y;
|
|
216
|
+
P && m ? y = m : g.getLocaleFromPath ? y = g.getLocaleFromPath(d, l, r) : y = l;
|
|
217
|
+
const h = e.clone(y), b = g.getRouteName ? g.getRouteName(d, r) : "index";
|
|
218
|
+
return h.setRoute(b), s.locals.i18n = h, s.locals.locale = y, s.locals.defaultLocale = l, s.locals.locales = i || r.map((L) => ({ code: L })), s.locals.currentUrl = f, s.locals.routingStrategy = g, n();
|
|
219
219
|
};
|
|
220
220
|
}
|
|
221
|
-
function te(
|
|
222
|
-
const u =
|
|
223
|
-
let
|
|
221
|
+
function te(o, e, l, r, i, c = "i18n-locale") {
|
|
222
|
+
const u = $();
|
|
223
|
+
let t = r;
|
|
224
224
|
if (u?.getLocaleFromPath)
|
|
225
|
-
|
|
225
|
+
t = u.getLocaleFromPath(o, r, i);
|
|
226
226
|
else {
|
|
227
|
-
const
|
|
228
|
-
|
|
227
|
+
const s = o.split("/").filter(Boolean)[0];
|
|
228
|
+
s && i.includes(s) && (t = s);
|
|
229
229
|
}
|
|
230
|
-
if (c !== null &&
|
|
231
|
-
const
|
|
232
|
-
|
|
230
|
+
if (c !== null && t === r && e.get(c)) {
|
|
231
|
+
const a = e.get(c)?.value;
|
|
232
|
+
a && i.includes(a) && (t = a);
|
|
233
233
|
}
|
|
234
|
-
if (
|
|
234
|
+
if (t === r)
|
|
235
235
|
try {
|
|
236
|
-
const
|
|
237
|
-
|
|
236
|
+
const a = z(l.get("accept-language") ?? void 0, i);
|
|
237
|
+
a && (t = a);
|
|
238
238
|
} catch {
|
|
239
239
|
}
|
|
240
|
-
return
|
|
240
|
+
return t;
|
|
241
241
|
}
|
|
242
|
-
function ne(
|
|
243
|
-
const
|
|
244
|
-
const d =
|
|
242
|
+
function ne(o, e, l) {
|
|
243
|
+
const r = o.map((n) => n.code), i = (n, f = []) => {
|
|
244
|
+
const d = n.replace(/^\//, "").replace(/\/$/, "");
|
|
245
245
|
if (!d)
|
|
246
246
|
return "index";
|
|
247
247
|
const g = d.split("/").filter(Boolean), p = g[0];
|
|
248
248
|
return p && f.includes(p) && g.shift(), g.length === 0 ? "index" : g.join("-");
|
|
249
|
-
}, c = (
|
|
250
|
-
const p =
|
|
249
|
+
}, c = (n, f = "en", d = []) => {
|
|
250
|
+
const p = n.split("/").filter(Boolean)[0];
|
|
251
251
|
return p && d.includes(p) ? p : f;
|
|
252
|
-
}, u = (
|
|
253
|
-
const p =
|
|
252
|
+
}, u = (n, f, d = [], g) => {
|
|
253
|
+
const p = n.split("/").filter(Boolean), m = p[0];
|
|
254
254
|
return m && d.includes(m) && p.shift(), (f !== g || g === void 0) && p.unshift(f), `/${p.join("/")}`;
|
|
255
|
-
},
|
|
256
|
-
const m = (
|
|
257
|
-
return
|
|
255
|
+
}, t = (n, f, d = [], g) => {
|
|
256
|
+
const m = (n.replace(/^\//, "").replace(/\/$/, "") || "").split("/").filter(Boolean), P = m[0];
|
|
257
|
+
return P && d.includes(P) && m.shift(), (f !== g || g === void 0) && m.unshift(f), `/${m.join("/")}`;
|
|
258
258
|
};
|
|
259
259
|
return {
|
|
260
|
-
getCurrentPath: () =>
|
|
261
|
-
getRouteName:
|
|
260
|
+
getCurrentPath: () => l ? l().pathname : typeof window < "u" ? window.location.pathname : "/",
|
|
261
|
+
getRouteName: i,
|
|
262
262
|
getLocaleFromPath: c,
|
|
263
263
|
switchLocalePath: u,
|
|
264
|
-
localizePath:
|
|
265
|
-
removeLocaleFromPath: (
|
|
266
|
-
const d =
|
|
264
|
+
localizePath: t,
|
|
265
|
+
removeLocaleFromPath: (n, f = []) => {
|
|
266
|
+
const d = n.split("/").filter(Boolean), g = d[0];
|
|
267
267
|
return g && f.includes(g) && d.shift(), `/${d.join("/")}`;
|
|
268
268
|
},
|
|
269
|
-
resolvePath: (
|
|
270
|
-
const d = typeof
|
|
271
|
-
return
|
|
269
|
+
resolvePath: (n, f) => {
|
|
270
|
+
const d = typeof n == "string" ? n : n.path || "/";
|
|
271
|
+
return t(d, f, r, e);
|
|
272
272
|
},
|
|
273
273
|
getRoute: () => {
|
|
274
|
-
if (
|
|
275
|
-
const
|
|
274
|
+
if (l) {
|
|
275
|
+
const n = l();
|
|
276
276
|
return {
|
|
277
|
-
fullPath:
|
|
278
|
-
query: Object.fromEntries(
|
|
277
|
+
fullPath: n.pathname + n.search,
|
|
278
|
+
query: Object.fromEntries(n.searchParams)
|
|
279
279
|
};
|
|
280
280
|
}
|
|
281
281
|
if (typeof window < "u") {
|
|
282
|
-
const
|
|
282
|
+
const n = new URL(window.location.href);
|
|
283
283
|
return {
|
|
284
|
-
fullPath:
|
|
285
|
-
query: Object.fromEntries(
|
|
284
|
+
fullPath: n.pathname + n.search,
|
|
285
|
+
query: Object.fromEntries(n.searchParams)
|
|
286
286
|
};
|
|
287
287
|
}
|
|
288
288
|
return {
|
|
@@ -291,173 +291,150 @@ function ne(t, e, o) {
|
|
|
291
291
|
};
|
|
292
292
|
},
|
|
293
293
|
// Optional: client-side navigation for islands
|
|
294
|
-
push: (
|
|
295
|
-
typeof window < "u" && (window.location.href =
|
|
294
|
+
push: (n) => {
|
|
295
|
+
typeof window < "u" && (window.location.href = n.path);
|
|
296
296
|
},
|
|
297
|
-
replace: (
|
|
298
|
-
typeof window < "u" && window.location.replace(
|
|
297
|
+
replace: (n) => {
|
|
298
|
+
typeof window < "u" && window.location.replace(n.path);
|
|
299
299
|
}
|
|
300
300
|
};
|
|
301
301
|
}
|
|
302
|
-
function
|
|
303
|
-
const
|
|
304
|
-
if (!o)
|
|
305
|
-
return "index";
|
|
306
|
-
const n = o.split("/").filter(Boolean), r = n[0];
|
|
307
|
-
return r && e.includes(r) && n.shift(), n.length === 0 ? "index" : n.join("-");
|
|
308
|
-
}
|
|
309
|
-
function ae(t, e = "en", o = []) {
|
|
310
|
-
const r = t.split("/").filter(Boolean)[0];
|
|
311
|
-
return r && o.includes(r) ? r : e;
|
|
312
|
-
}
|
|
313
|
-
function le(t, e, o = [], n) {
|
|
314
|
-
const r = t.split("/").filter(Boolean), c = r[0];
|
|
315
|
-
return c && o.includes(c) && r.shift(), (e !== n || n === void 0) && r.unshift(e), `/${r.join("/")}`;
|
|
316
|
-
}
|
|
317
|
-
function se(t, e, o = [], n) {
|
|
318
|
-
const c = (t.replace(/^\//, "").replace(/\/$/, "") || "").split("/").filter(Boolean), u = c[0];
|
|
319
|
-
return u && o.includes(u) && c.shift(), (e !== n || n === void 0) && c.unshift(e), `/${c.join("/")}`;
|
|
320
|
-
}
|
|
321
|
-
function re(t, e = []) {
|
|
322
|
-
const o = t.split("/").filter(Boolean), n = o[0];
|
|
323
|
-
return n && e.includes(n) && o.shift(), `/${o.join("/")}`;
|
|
324
|
-
}
|
|
325
|
-
function B(t) {
|
|
326
|
-
const e = t.locals.i18n;
|
|
302
|
+
function F(o) {
|
|
303
|
+
const e = o.locals.i18n;
|
|
327
304
|
if (!e)
|
|
328
305
|
throw new Error("i18n instance not found. Make sure i18n middleware is configured.");
|
|
329
306
|
return e;
|
|
330
307
|
}
|
|
331
|
-
function
|
|
332
|
-
return
|
|
308
|
+
function T(o) {
|
|
309
|
+
return o.locals.locale || "en";
|
|
333
310
|
}
|
|
334
|
-
function
|
|
335
|
-
return
|
|
311
|
+
function j(o) {
|
|
312
|
+
return o.locals.defaultLocale || "en";
|
|
336
313
|
}
|
|
337
|
-
function
|
|
338
|
-
return
|
|
314
|
+
function B(o) {
|
|
315
|
+
return o.locals.locales || [];
|
|
339
316
|
}
|
|
340
|
-
function I(
|
|
341
|
-
return
|
|
317
|
+
function I(o) {
|
|
318
|
+
return o.locals.routingStrategy || null;
|
|
342
319
|
}
|
|
343
|
-
function
|
|
344
|
-
const e =
|
|
320
|
+
function oe(o) {
|
|
321
|
+
const e = F(o), l = T(o), r = j(o), i = B(o), c = i.map((t) => t.code), u = I(o);
|
|
345
322
|
return {
|
|
346
323
|
// Current locale
|
|
347
|
-
locale:
|
|
348
|
-
defaultLocale:
|
|
349
|
-
locales:
|
|
324
|
+
locale: l,
|
|
325
|
+
defaultLocale: r,
|
|
326
|
+
locales: i,
|
|
350
327
|
// Translation methods
|
|
351
|
-
t: (a, s,
|
|
352
|
-
ts: (a, s,
|
|
353
|
-
tc: (a, s
|
|
354
|
-
tn: (
|
|
355
|
-
td: (
|
|
356
|
-
tdr: (
|
|
357
|
-
has: (
|
|
328
|
+
t: (t, a, s, n) => e.t(t, a, s, n),
|
|
329
|
+
ts: (t, a, s, n) => e.ts(t, a, s, n),
|
|
330
|
+
tc: (t, a, s) => e.tc(t, a, s),
|
|
331
|
+
tn: (t, a) => e.tn(t, a),
|
|
332
|
+
td: (t, a) => e.td(t, a),
|
|
333
|
+
tdr: (t, a) => e.tdr(t, a),
|
|
334
|
+
has: (t, a) => e.has(t, a),
|
|
358
335
|
// Route management
|
|
359
336
|
getRoute: () => e.getRoute(),
|
|
360
|
-
getRouteName: (
|
|
361
|
-
const
|
|
337
|
+
getRouteName: (t) => {
|
|
338
|
+
const a = t || o.url.pathname;
|
|
362
339
|
if (u?.getRouteName)
|
|
363
|
-
return u.getRouteName(
|
|
364
|
-
const
|
|
365
|
-
if (!
|
|
366
|
-
const
|
|
367
|
-
return f && c.includes(f) &&
|
|
340
|
+
return u.getRouteName(a, c);
|
|
341
|
+
const s = a.replace(/^\//, "").replace(/\/$/, "");
|
|
342
|
+
if (!s) return "index";
|
|
343
|
+
const n = s.split("/").filter(Boolean), f = n[0];
|
|
344
|
+
return f && c.includes(f) && n.shift(), n.length === 0 ? "index" : n.join("-");
|
|
368
345
|
},
|
|
369
|
-
getLocaleFromPath: (
|
|
370
|
-
const
|
|
346
|
+
getLocaleFromPath: (t) => {
|
|
347
|
+
const a = t || o.url.pathname;
|
|
371
348
|
if (u?.getLocaleFromPath)
|
|
372
|
-
return u.getLocaleFromPath(
|
|
373
|
-
const
|
|
374
|
-
return
|
|
349
|
+
return u.getLocaleFromPath(a, r, c);
|
|
350
|
+
const n = a.split("/").filter(Boolean)[0];
|
|
351
|
+
return n && c.includes(n) ? n : r;
|
|
375
352
|
},
|
|
376
353
|
// Path utilities
|
|
377
|
-
switchLocalePath: (
|
|
354
|
+
switchLocalePath: (t) => {
|
|
378
355
|
if (u?.switchLocalePath)
|
|
379
|
-
return u.switchLocalePath(
|
|
380
|
-
const
|
|
381
|
-
return
|
|
356
|
+
return u.switchLocalePath(o.url.pathname, t, c, r);
|
|
357
|
+
const a = o.url.pathname.split("/").filter(Boolean), s = a[0];
|
|
358
|
+
return s && c.includes(s) && a.shift(), t !== r && a.unshift(t), `/${a.join("/")}`;
|
|
382
359
|
},
|
|
383
|
-
localizePath: (
|
|
360
|
+
localizePath: (t, a) => {
|
|
384
361
|
if (u?.localizePath)
|
|
385
|
-
return u.localizePath(
|
|
386
|
-
const
|
|
387
|
-
return f && c.includes(f) &&
|
|
362
|
+
return u.localizePath(t, a || l, c, r);
|
|
363
|
+
const n = (t.replace(/^\//, "").replace(/\/$/, "") || "").split("/").filter(Boolean), f = n[0];
|
|
364
|
+
return f && c.includes(f) && n.shift(), a && a !== r && n.unshift(a), `/${n.join("/")}`;
|
|
388
365
|
},
|
|
389
366
|
// Get i18n instance
|
|
390
367
|
getI18n: () => e,
|
|
391
368
|
// Get base path without locale (for rewrite)
|
|
392
|
-
getBasePath: (
|
|
393
|
-
const
|
|
394
|
-
return f && c.includes(f) &&
|
|
369
|
+
getBasePath: (t) => {
|
|
370
|
+
const n = (t || o.url).pathname.split("/").filter(Boolean), f = n[0];
|
|
371
|
+
return f && c.includes(f) && n.shift(), n.length > 0 ? `/${n.join("/")}` : "/";
|
|
395
372
|
},
|
|
396
373
|
// Translation management
|
|
397
|
-
addTranslations: (a, s
|
|
398
|
-
e.addTranslations(a, s
|
|
374
|
+
addTranslations: (t, a, s = !0) => {
|
|
375
|
+
e.addTranslations(t, a, s);
|
|
399
376
|
},
|
|
400
|
-
addRouteTranslations: (a, s,
|
|
401
|
-
e.addRouteTranslations(a, s,
|
|
377
|
+
addRouteTranslations: (t, a, s, n = !0) => {
|
|
378
|
+
e.addRouteTranslations(t, a, s, n);
|
|
402
379
|
},
|
|
403
|
-
mergeTranslations: (a, s
|
|
404
|
-
e.mergeTranslations(a, s
|
|
380
|
+
mergeTranslations: (t, a, s) => {
|
|
381
|
+
e.mergeTranslations(t, a, s);
|
|
405
382
|
},
|
|
406
383
|
clearCache: () => {
|
|
407
384
|
e.clearCache();
|
|
408
385
|
}
|
|
409
386
|
};
|
|
410
387
|
}
|
|
411
|
-
function
|
|
412
|
-
const { baseUrl:
|
|
413
|
-
if (!
|
|
388
|
+
function ae(o, e = {}) {
|
|
389
|
+
const { baseUrl: l = "/", addDirAttribute: r = !0, addSeoAttributes: i = !0 } = e, c = T(o), u = j(o), a = B(o).filter((h) => !h.disabled), s = a.filter((h) => h.seo !== !1), n = a.find((h) => h.code === c);
|
|
390
|
+
if (!n)
|
|
414
391
|
return { htmlAttrs: {}, link: [], meta: [] };
|
|
415
|
-
const f =
|
|
392
|
+
const f = n.iso || c, d = n.dir || "auto", g = {
|
|
416
393
|
htmlAttrs: {
|
|
417
394
|
lang: f,
|
|
418
|
-
...
|
|
395
|
+
...r ? { dir: d } : {}
|
|
419
396
|
},
|
|
420
397
|
link: [],
|
|
421
398
|
meta: []
|
|
422
399
|
};
|
|
423
|
-
if (!
|
|
400
|
+
if (!i)
|
|
424
401
|
return g;
|
|
425
|
-
const p = `${
|
|
402
|
+
const p = `${l}${o.url.pathname}`;
|
|
426
403
|
g.link.push({
|
|
427
404
|
rel: "canonical",
|
|
428
405
|
href: p
|
|
429
406
|
});
|
|
430
|
-
const m = I(
|
|
431
|
-
for (const h of
|
|
432
|
-
let
|
|
407
|
+
const m = I(o), P = a.map((h) => h.code);
|
|
408
|
+
for (const h of s) {
|
|
409
|
+
let b = o.url.pathname;
|
|
433
410
|
if (m?.switchLocalePath)
|
|
434
|
-
|
|
411
|
+
b = m.switchLocalePath(o.url.pathname, h.code, P, u);
|
|
435
412
|
else {
|
|
436
|
-
const
|
|
437
|
-
|
|
413
|
+
const S = o.url.pathname.split("/").filter(Boolean), w = S[0];
|
|
414
|
+
w && P.includes(w) && S.shift(), h.code !== u && S.unshift(h.code), b = `/${S.join("/")}`;
|
|
438
415
|
}
|
|
439
|
-
const
|
|
416
|
+
const L = `${l}${b}`;
|
|
440
417
|
g.link.push({
|
|
441
418
|
rel: "alternate",
|
|
442
|
-
href:
|
|
419
|
+
href: L,
|
|
443
420
|
hreflang: h.code
|
|
444
421
|
}), h.iso && h.iso !== h.code && g.link.push({
|
|
445
422
|
rel: "alternate",
|
|
446
|
-
href:
|
|
423
|
+
href: L,
|
|
447
424
|
hreflang: h.iso
|
|
448
425
|
});
|
|
449
426
|
}
|
|
450
|
-
if (
|
|
451
|
-
let h =
|
|
427
|
+
if (a.find((h) => h.code === u)?.seo !== !1) {
|
|
428
|
+
let h = o.url.pathname;
|
|
452
429
|
if (m?.switchLocalePath)
|
|
453
|
-
h = m.switchLocalePath(
|
|
430
|
+
h = m.switchLocalePath(o.url.pathname, u, P, u);
|
|
454
431
|
else {
|
|
455
|
-
const
|
|
456
|
-
|
|
432
|
+
const b = o.url.pathname.split("/").filter(Boolean), L = b[0];
|
|
433
|
+
L && P.includes(L) && b.shift(), h = `/${b.join("/")}`;
|
|
457
434
|
}
|
|
458
435
|
g.link.push({
|
|
459
436
|
rel: "alternate",
|
|
460
|
-
href: `${
|
|
437
|
+
href: `${l}${h}`,
|
|
461
438
|
hreflang: "x-default"
|
|
462
439
|
});
|
|
463
440
|
}
|
|
@@ -468,65 +445,60 @@ function ce(t, e = {}) {
|
|
|
468
445
|
property: "og:url",
|
|
469
446
|
content: p
|
|
470
447
|
});
|
|
471
|
-
for (const h of
|
|
448
|
+
for (const h of s)
|
|
472
449
|
h.code !== c && g.meta.push({
|
|
473
450
|
property: "og:locale:alternate",
|
|
474
451
|
content: h.og || h.iso || h.code
|
|
475
452
|
});
|
|
476
453
|
return g;
|
|
477
454
|
}
|
|
478
|
-
function
|
|
479
|
-
const
|
|
480
|
-
let
|
|
481
|
-
for (let u = 0; u <
|
|
482
|
-
const
|
|
483
|
-
|
|
455
|
+
function x(o, e, l) {
|
|
456
|
+
const r = e.split(".");
|
|
457
|
+
let i = o;
|
|
458
|
+
for (let u = 0; u < r.length - 1; u++) {
|
|
459
|
+
const t = r[u];
|
|
460
|
+
i[t] || (i[t] = {}), i = i[t];
|
|
484
461
|
}
|
|
485
|
-
const c =
|
|
486
|
-
c !== void 0 && (
|
|
462
|
+
const c = r[r.length - 1];
|
|
463
|
+
c !== void 0 && (i[c] = l);
|
|
487
464
|
}
|
|
488
|
-
function
|
|
489
|
-
const
|
|
465
|
+
function le(o, e) {
|
|
466
|
+
const l = F(o), r = T(o), i = j(o), c = l.getRoute(), u = {};
|
|
490
467
|
if (e && e.length > 0) {
|
|
491
|
-
const
|
|
492
|
-
for (const
|
|
493
|
-
const
|
|
494
|
-
|
|
468
|
+
const t = {};
|
|
469
|
+
for (const a of e) {
|
|
470
|
+
const s = l.t(a, void 0, void 0, c);
|
|
471
|
+
s != null && s !== a && x(t, a, s);
|
|
495
472
|
}
|
|
496
|
-
Object.keys(
|
|
473
|
+
Object.keys(t).length > 0 && (u[c] = t);
|
|
497
474
|
} else {
|
|
498
|
-
const
|
|
499
|
-
|
|
475
|
+
const t = l.getRouteTranslations(r, c);
|
|
476
|
+
t && (u[c] = t);
|
|
500
477
|
}
|
|
501
478
|
return {
|
|
502
|
-
locale:
|
|
503
|
-
fallbackLocale:
|
|
479
|
+
locale: r,
|
|
480
|
+
fallbackLocale: i,
|
|
504
481
|
currentRoute: c,
|
|
505
482
|
translations: u
|
|
506
483
|
};
|
|
507
484
|
}
|
|
508
485
|
export {
|
|
509
|
-
|
|
510
|
-
|
|
486
|
+
R as AstroI18n,
|
|
487
|
+
ie as FormatService,
|
|
511
488
|
ne as createAstroRouterAdapter,
|
|
512
489
|
Y as createI18n,
|
|
513
490
|
ee as createI18nMiddleware,
|
|
514
|
-
|
|
491
|
+
ce as defaultPlural,
|
|
515
492
|
te as detectLocale,
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
F as getLocales,
|
|
522
|
-
oe as getRouteName,
|
|
493
|
+
j as getDefaultLocale,
|
|
494
|
+
F as getI18n,
|
|
495
|
+
le as getI18nProps,
|
|
496
|
+
T as getLocale,
|
|
497
|
+
B as getLocales,
|
|
523
498
|
X as i18nIntegration,
|
|
524
|
-
|
|
525
|
-
|
|
499
|
+
ue as interpolate,
|
|
500
|
+
E as loadTranslationsFromDir,
|
|
526
501
|
Z as loadTranslationsIntoI18n,
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
le as switchLocalePath,
|
|
530
|
-
ie as useI18n,
|
|
531
|
-
ce as useLocaleHead
|
|
502
|
+
oe as useI18n,
|
|
503
|
+
ae as useLocaleHead
|
|
532
504
|
};
|
package/package.json
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@i18n-micro/astro",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.6",
|
|
4
4
|
"description": "Astro integration for i18n-micro — translations, routing, and islands.",
|
|
5
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"astro",
|
|
7
|
+
"i18n",
|
|
8
|
+
"internationalization",
|
|
9
|
+
"translations"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/s00d/nuxt-i18n-micro/tree/main/packages/astro#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/s00d/nuxt-i18n-micro/issues"
|
|
14
|
+
},
|
|
6
15
|
"license": "MIT",
|
|
7
|
-
"sideEffects": false,
|
|
8
16
|
"repository": {
|
|
9
17
|
"type": "git",
|
|
10
18
|
"url": "git+https://github.com/s00d/nuxt-i18n-micro.git",
|
|
11
19
|
"directory": "packages/astro"
|
|
12
20
|
},
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"src",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"sideEffects": false,
|
|
20
29
|
"main": "./dist/index.cjs",
|
|
21
30
|
"types": "./dist/index.d.ts",
|
|
22
31
|
"exports": {
|
|
@@ -64,33 +73,35 @@
|
|
|
64
73
|
},
|
|
65
74
|
"./package.json": "./package.json"
|
|
66
75
|
},
|
|
67
|
-
"files": [
|
|
68
|
-
"dist",
|
|
69
|
-
"src",
|
|
70
|
-
"README.md",
|
|
71
|
-
"LICENSE"
|
|
72
|
-
],
|
|
73
76
|
"publishConfig": {
|
|
74
77
|
"access": "public"
|
|
75
78
|
},
|
|
76
|
-
"keywords": [
|
|
77
|
-
"astro",
|
|
78
|
-
"i18n",
|
|
79
|
-
"internationalization",
|
|
80
|
-
"translations"
|
|
81
|
-
],
|
|
82
79
|
"dependencies": {
|
|
83
|
-
"@i18n-micro/
|
|
84
|
-
"@i18n-micro/core": "1.3.
|
|
85
|
-
"@i18n-micro/
|
|
86
|
-
"@i18n-micro/
|
|
80
|
+
"@i18n-micro/utils": "1.0.1",
|
|
81
|
+
"@i18n-micro/core": "1.3.2",
|
|
82
|
+
"@i18n-micro/node": "1.2.3",
|
|
83
|
+
"@i18n-micro/types": "1.2.4"
|
|
87
84
|
},
|
|
88
|
-
"
|
|
85
|
+
"devDependencies": {
|
|
86
|
+
"@types/jest": "^29.5.14",
|
|
89
87
|
"astro": "^5.16.5",
|
|
88
|
+
"jest": "^29.7.0",
|
|
89
|
+
"preact": "^10.0.0",
|
|
90
|
+
"publint": "^0.3.17",
|
|
91
|
+
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
|
|
90
92
|
"svelte": "^4.0.0 || ^5.0.0",
|
|
91
|
-
"
|
|
93
|
+
"ts-jest": "^29.4.6",
|
|
94
|
+
"vite": "^7.3.1",
|
|
95
|
+
"vite-plugin-dts": "^4.5.4",
|
|
96
|
+
"vitest": "^3.2.4",
|
|
97
|
+
"vue": "^3.5.25"
|
|
98
|
+
},
|
|
99
|
+
"peerDependencies": {
|
|
100
|
+
"astro": "^5.16.5",
|
|
101
|
+
"preact": "^10.0.0",
|
|
92
102
|
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
|
|
93
|
-
"
|
|
103
|
+
"svelte": "^4.0.0 || ^5.0.0",
|
|
104
|
+
"vue": "^3.5.25"
|
|
94
105
|
},
|
|
95
106
|
"peerDependenciesMeta": {
|
|
96
107
|
"svelte": {
|
|
@@ -106,19 +117,8 @@
|
|
|
106
117
|
"optional": true
|
|
107
118
|
}
|
|
108
119
|
},
|
|
109
|
-
"
|
|
110
|
-
"
|
|
111
|
-
"vite": "^7.3.1",
|
|
112
|
-
"vite-plugin-dts": "^4.5.4",
|
|
113
|
-
"jest": "^29.7.0",
|
|
114
|
-
"ts-jest": "^29.4.6",
|
|
115
|
-
"@types/jest": "^29.5.14",
|
|
116
|
-
"svelte": "^4.0.0 || ^5.0.0",
|
|
117
|
-
"vue": "^3.5.25",
|
|
118
|
-
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
|
|
119
|
-
"preact": "^10.0.0",
|
|
120
|
-
"publint": "^0.3.17",
|
|
121
|
-
"vitest": "^3.2.4"
|
|
120
|
+
"engines": {
|
|
121
|
+
"node": ">=18"
|
|
122
122
|
},
|
|
123
123
|
"scripts": {
|
|
124
124
|
"build": "vite build",
|
package/src/index.ts
CHANGED
|
@@ -4,15 +4,7 @@ import './env.d'
|
|
|
4
4
|
// Re-export utilities from core
|
|
5
5
|
export { defaultPlural, FormatService, interpolate } from '@i18n-micro/core'
|
|
6
6
|
// Re-export types from @i18n-micro/types
|
|
7
|
-
export type {
|
|
8
|
-
CleanTranslation,
|
|
9
|
-
Getter,
|
|
10
|
-
Locale,
|
|
11
|
-
LocaleCode,
|
|
12
|
-
Params,
|
|
13
|
-
PluralFunc,
|
|
14
|
-
Translations,
|
|
15
|
-
} from '@i18n-micro/types'
|
|
7
|
+
export type { CleanTranslation, Getter, Locale, LocaleCode, Params, PluralFunc, Translations } from '@i18n-micro/types'
|
|
16
8
|
export { AstroI18n, type AstroI18nOptions } from './composer'
|
|
17
9
|
// Export integration options type
|
|
18
10
|
export type { I18nIntegrationOptions } from './integration'
|
|
@@ -20,33 +12,13 @@ export type { I18nIntegrationOptions } from './integration'
|
|
|
20
12
|
export { createI18n, i18nIntegration } from './integration'
|
|
21
13
|
export type { LoadedTranslations, LoadTranslationsOptions } from './load-translations'
|
|
22
14
|
// Export translation loading utilities
|
|
23
|
-
export {
|
|
24
|
-
loadTranslationsFromDir,
|
|
25
|
-
loadTranslationsIntoI18n,
|
|
26
|
-
} from './load-translations'
|
|
15
|
+
export { loadTranslationsFromDir, loadTranslationsIntoI18n } from './load-translations'
|
|
27
16
|
export type { I18nMiddlewareOptions } from './middleware'
|
|
28
17
|
export { createI18nMiddleware, detectLocale } from './middleware'
|
|
29
18
|
export { createAstroRouterAdapter } from './router/adapter'
|
|
30
19
|
// Router abstraction
|
|
31
20
|
export type { I18nRoutingStrategy } from './router/types'
|
|
32
|
-
// Legacy routing utilities (deprecated, use routingStrategy instead)
|
|
33
|
-
// Kept for backward compatibility
|
|
34
|
-
export {
|
|
35
|
-
getLocaleFromPath,
|
|
36
|
-
getRouteName,
|
|
37
|
-
localizePath,
|
|
38
|
-
removeLocaleFromPath,
|
|
39
|
-
switchLocalePath,
|
|
40
|
-
} from './routing'
|
|
41
21
|
export type { I18nClientProps, LocaleHeadOptions, LocaleHeadResult } from './utils'
|
|
42
22
|
// Utilities
|
|
43
|
-
export {
|
|
44
|
-
getDefaultLocale,
|
|
45
|
-
getI18n,
|
|
46
|
-
getI18nProps,
|
|
47
|
-
getLocale,
|
|
48
|
-
getLocales,
|
|
49
|
-
useI18n,
|
|
50
|
-
useLocaleHead,
|
|
51
|
-
} from './utils'
|
|
23
|
+
export { getDefaultLocale, getI18n, getI18nProps, getLocale, getLocales, useI18n, useLocaleHead } from './utils'
|
|
52
24
|
// It's loaded directly by Astro via entrypoint in integration config
|
package/dist/routing.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Get route name from Astro path
|
|
3
|
-
* Extracts route name from path (e.g., /en/about -> about)
|
|
4
|
-
*/
|
|
5
|
-
export declare function getRouteName(path: string, locales?: string[]): string;
|
|
6
|
-
/**
|
|
7
|
-
* Get locale from path
|
|
8
|
-
* Checks if first segment is a locale code
|
|
9
|
-
*/
|
|
10
|
-
export declare function getLocaleFromPath(path: string, defaultLocale?: string, locales?: string[]): string;
|
|
11
|
-
/**
|
|
12
|
-
* Switch locale in path
|
|
13
|
-
* Replaces or adds locale prefix to path
|
|
14
|
-
*/
|
|
15
|
-
export declare function switchLocalePath(path: string, newLocale: string, locales?: string[], defaultLocale?: string): string;
|
|
16
|
-
/**
|
|
17
|
-
* Localize path with locale prefix
|
|
18
|
-
*/
|
|
19
|
-
export declare function localizePath(path: string, locale: string, locales?: string[], defaultLocale?: string): string;
|
|
20
|
-
/**
|
|
21
|
-
* Remove locale from path
|
|
22
|
-
*/
|
|
23
|
-
export declare function removeLocaleFromPath(path: string, locales?: string[]): string;
|
package/src/routing.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Get route name from Astro path
|
|
3
|
-
* Extracts route name from path (e.g., /en/about -> about)
|
|
4
|
-
*/
|
|
5
|
-
export function getRouteName(path: string, locales: string[] = []): string {
|
|
6
|
-
// Normalize the path
|
|
7
|
-
const cleanPath = path.replace(/^\//, '').replace(/\/$/, '')
|
|
8
|
-
|
|
9
|
-
if (!cleanPath) {
|
|
10
|
-
return 'index'
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const segments = cleanPath.split('/').filter(Boolean)
|
|
14
|
-
|
|
15
|
-
// Remove locale from path if present
|
|
16
|
-
const firstSegment = segments[0]
|
|
17
|
-
if (firstSegment && locales.includes(firstSegment)) {
|
|
18
|
-
segments.shift()
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (segments.length === 0) {
|
|
22
|
-
return 'index'
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return segments.join('-')
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Get locale from path
|
|
30
|
-
* Checks if first segment is a locale code
|
|
31
|
-
*/
|
|
32
|
-
export function getLocaleFromPath(path: string, defaultLocale: string = 'en', locales: string[] = []): string {
|
|
33
|
-
const segments = path.split('/').filter(Boolean)
|
|
34
|
-
const firstSegment = segments[0]
|
|
35
|
-
if (firstSegment && locales.includes(firstSegment)) {
|
|
36
|
-
return firstSegment
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return defaultLocale
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Switch locale in path
|
|
44
|
-
* Replaces or adds locale prefix to path
|
|
45
|
-
*/
|
|
46
|
-
export function switchLocalePath(path: string, newLocale: string, locales: string[] = [], defaultLocale?: string): string {
|
|
47
|
-
const segments = path.split('/').filter(Boolean)
|
|
48
|
-
|
|
49
|
-
// Remove existing locale if present
|
|
50
|
-
const firstSegment = segments[0]
|
|
51
|
-
if (firstSegment && locales.includes(firstSegment)) {
|
|
52
|
-
segments.shift()
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Add new locale if not default or if default should be included
|
|
56
|
-
if (newLocale !== defaultLocale || defaultLocale === undefined) {
|
|
57
|
-
segments.unshift(newLocale)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return `/${segments.join('/')}`
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Localize path with locale prefix
|
|
65
|
-
*/
|
|
66
|
-
export function localizePath(path: string, locale: string, locales: string[] = [], defaultLocale?: string): string {
|
|
67
|
-
const cleanPath = path.replace(/^\//, '').replace(/\/$/, '') || ''
|
|
68
|
-
const segments = cleanPath.split('/').filter(Boolean)
|
|
69
|
-
|
|
70
|
-
// Remove existing locale if present
|
|
71
|
-
const firstSegment = segments[0]
|
|
72
|
-
if (firstSegment && locales.includes(firstSegment)) {
|
|
73
|
-
segments.shift()
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Add locale if not default or if default should be included
|
|
77
|
-
if (locale !== defaultLocale || defaultLocale === undefined) {
|
|
78
|
-
segments.unshift(locale)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return `/${segments.join('/')}`
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Remove locale from path
|
|
86
|
-
*/
|
|
87
|
-
export function removeLocaleFromPath(path: string, locales: string[] = []): string {
|
|
88
|
-
const segments = path.split('/').filter(Boolean)
|
|
89
|
-
const firstSegment = segments[0]
|
|
90
|
-
if (firstSegment && locales.includes(firstSegment)) {
|
|
91
|
-
segments.shift()
|
|
92
|
-
}
|
|
93
|
-
return `/${segments.join('/')}`
|
|
94
|
-
}
|