@i18n-micro/astro 1.3.2 → 1.3.4
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/index.d.cts +6 -0
- package/dist/components/index.d.cts +0 -0
- package/dist/index.cjs +2 -2
- package/dist/index.d.cts +14 -0
- package/dist/index.mjs +111 -111
- package/package.json +44 -11
- package/src/utils.ts +6 -4
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type { I18nState } from './core';
|
|
2
|
+
export { hasTranslation, translate } from './core';
|
|
3
|
+
export { I18nProvider as I18nProviderPreact, useAstroI18n as useAstroI18nPreact } from './preact';
|
|
4
|
+
export { I18nProvider, useAstroI18n as useAstroI18nReact } from './react';
|
|
5
|
+
export { createI18nStore, useAstroI18n as useAstroI18nSvelte } from './svelte';
|
|
6
|
+
export { provideI18n, useAstroI18n as useAstroI18nVue } from './vue';
|
|
File without changes
|
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 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:`
|
|
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
|
|
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;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { defaultPlural, FormatService, interpolate } from '@i18n-micro/core';
|
|
2
|
+
export type { CleanTranslation, Getter, Locale, LocaleCode, Params, PluralFunc, Translations, } from '@i18n-micro/types';
|
|
3
|
+
export { AstroI18n, type AstroI18nOptions } from './composer';
|
|
4
|
+
export type { I18nIntegrationOptions } from './integration';
|
|
5
|
+
export { createI18n, i18nIntegration } from './integration';
|
|
6
|
+
export type { LoadedTranslations, LoadTranslationsOptions } from './load-translations';
|
|
7
|
+
export { loadTranslationsFromDir, loadTranslationsIntoI18n, } from './load-translations';
|
|
8
|
+
export type { I18nMiddlewareOptions } from './middleware';
|
|
9
|
+
export { createI18nMiddleware, detectLocale } from './middleware';
|
|
10
|
+
export { createAstroRouterAdapter } from './router/adapter';
|
|
11
|
+
export type { I18nRoutingStrategy } from './router/types';
|
|
12
|
+
export { getLocaleFromPath, getRouteName, localizePath, removeLocaleFromPath, switchLocalePath, } from './routing';
|
|
13
|
+
export type { I18nClientProps, LocaleHeadOptions, LocaleHeadResult } from './utils';
|
|
14
|
+
export { getDefaultLocale, getI18n, getI18nProps, getLocale, getLocales, useI18n, useLocaleHead, } from './utils';
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { BaseI18n as I } from "@i18n-micro/core";
|
|
2
2
|
import { FormatService as le, defaultPlural as re, interpolate as ie } from "@i18n-micro/core";
|
|
3
|
-
import { existsSync as
|
|
4
|
-
import { resolve as
|
|
5
|
-
class
|
|
3
|
+
import { existsSync as $, readdirSync as O, statSync as _, readFileSync as C } from "node:fs";
|
|
4
|
+
import { resolve as N, join as q } from "node:path";
|
|
5
|
+
class j extends I {
|
|
6
6
|
constructor(e) {
|
|
7
7
|
const s = e._storage || {
|
|
8
8
|
translations: /* @__PURE__ */ new Map()
|
|
@@ -26,7 +26,7 @@ class S extends I {
|
|
|
26
26
|
}
|
|
27
27
|
clone(e) {
|
|
28
28
|
const s = this.cloneStorage(this.storage);
|
|
29
|
-
return new
|
|
29
|
+
return new j({
|
|
30
30
|
locale: e || this._locale,
|
|
31
31
|
fallbackLocale: this._fallbackLocale,
|
|
32
32
|
plural: this.pluralFunc,
|
|
@@ -79,13 +79,13 @@ class S extends I {
|
|
|
79
79
|
this.helper.loadTranslations(s, a);
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
|
-
let
|
|
83
|
-
function
|
|
84
|
-
return
|
|
82
|
+
let v = null;
|
|
83
|
+
function k() {
|
|
84
|
+
return v;
|
|
85
85
|
}
|
|
86
86
|
function E(t) {
|
|
87
87
|
const { locale: e, fallbackLocale: s, translationDir: a, routingStrategy: i } = t;
|
|
88
|
-
return
|
|
88
|
+
return v = i || null, {
|
|
89
89
|
name: "@i18n-micro/astro",
|
|
90
90
|
hooks: {
|
|
91
91
|
// 1. Vite setup (virtual module) happens here
|
|
@@ -147,26 +147,26 @@ function E(t) {
|
|
|
147
147
|
};
|
|
148
148
|
}
|
|
149
149
|
function J(t) {
|
|
150
|
-
return new
|
|
150
|
+
return new j(t);
|
|
151
151
|
}
|
|
152
152
|
function M(t) {
|
|
153
|
-
const { translationDir: e, rootDir: s = process.cwd(), disablePageLocales: a = !1 } = t, i =
|
|
154
|
-
if (
|
|
153
|
+
const { translationDir: e, rootDir: s = process.cwd(), disablePageLocales: a = !1 } = t, i = N(s, e);
|
|
154
|
+
if (!$(i))
|
|
155
155
|
return console.warn(`[i18n] Translation directory not found: ${i}`), { root: {}, routes: {} };
|
|
156
156
|
const c = {}, u = {}, n = (l, r = "") => {
|
|
157
|
-
if (
|
|
158
|
-
const o =
|
|
159
|
-
for (const
|
|
160
|
-
const
|
|
161
|
-
if (
|
|
162
|
-
|
|
163
|
-
else if (
|
|
164
|
-
const
|
|
157
|
+
if (!$(l)) return;
|
|
158
|
+
const o = O(l);
|
|
159
|
+
for (const f of o) {
|
|
160
|
+
const g = q(l, f);
|
|
161
|
+
if (_(g).isDirectory())
|
|
162
|
+
f === "pages" && !a ? n(g, "") : r || a ? n(g, r) : n(g, f);
|
|
163
|
+
else if (f.endsWith(".json")) {
|
|
164
|
+
const p = f.replace(".json", "");
|
|
165
165
|
try {
|
|
166
|
-
const
|
|
167
|
-
r && !a ? (u[r] || (u[r] = {}), u[r][
|
|
168
|
-
} catch (
|
|
169
|
-
console.error(`[i18n] Failed to load translation file: ${
|
|
166
|
+
const m = C(g, "utf-8"), L = JSON.parse(m);
|
|
167
|
+
r && !a ? (u[r] || (u[r] = {}), u[r][p] = L) : c[p] = L;
|
|
168
|
+
} catch (m) {
|
|
169
|
+
console.error(`[i18n] Failed to load translation file: ${g}`, m);
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
}
|
|
@@ -193,27 +193,27 @@ function G(t) {
|
|
|
193
193
|
autoDetect: c = !0,
|
|
194
194
|
redirectToDefault: u = !1,
|
|
195
195
|
routingStrategy: n
|
|
196
|
-
} = t, l = n ||
|
|
196
|
+
} = t, l = n || k();
|
|
197
197
|
return async (r, o) => {
|
|
198
198
|
if (r.locals.locale && r.locals.i18n)
|
|
199
199
|
return o();
|
|
200
|
-
const
|
|
200
|
+
const f = r.url, g = f.pathname;
|
|
201
201
|
if (!l) {
|
|
202
|
-
const
|
|
203
|
-
return
|
|
202
|
+
const P = e.clone(s), S = g === "/" || g === "" ? "index" : g.split("/").filter(Boolean).join("-");
|
|
203
|
+
return P.setRoute(S), r.locals.i18n = P, r.locals.locale = s, r.locals.defaultLocale = s, r.locals.locales = i || a.map((w) => ({ code: w })), r.locals.currentUrl = f, o();
|
|
204
204
|
}
|
|
205
|
-
const
|
|
205
|
+
const h = {
|
|
206
206
|
...l,
|
|
207
|
-
getCurrentPath: () =>
|
|
207
|
+
getCurrentPath: () => g,
|
|
208
208
|
getRoute: () => ({
|
|
209
|
-
fullPath:
|
|
210
|
-
query: Object.fromEntries(
|
|
209
|
+
fullPath: f.pathname + f.search,
|
|
210
|
+
query: Object.fromEntries(f.searchParams)
|
|
211
211
|
})
|
|
212
|
-
},
|
|
213
|
-
let
|
|
214
|
-
|
|
215
|
-
const
|
|
216
|
-
return
|
|
212
|
+
}, m = g.split("/").filter(Boolean)[0], L = m !== void 0 && a.includes(m);
|
|
213
|
+
let y;
|
|
214
|
+
L && m ? y = m : h.getLocaleFromPath ? y = h.getLocaleFromPath(g, s, a) : y = s;
|
|
215
|
+
const d = e.clone(y), b = h.getRouteName ? h.getRouteName(g, a) : "index";
|
|
216
|
+
return d.setRoute(b), r.locals.i18n = d, r.locals.locale = y, r.locals.defaultLocale = s, r.locals.locales = i || a.map((P) => ({ code: P })), r.locals.currentUrl = f, r.locals.routingStrategy = h, o();
|
|
217
217
|
};
|
|
218
218
|
}
|
|
219
219
|
function W(t) {
|
|
@@ -228,7 +228,7 @@ function W(t) {
|
|
|
228
228
|
return e;
|
|
229
229
|
}
|
|
230
230
|
function K(t, e, s, a, i, c = "i18n-locale") {
|
|
231
|
-
const u =
|
|
231
|
+
const u = k();
|
|
232
232
|
let n = a;
|
|
233
233
|
if (u?.getLocaleFromPath)
|
|
234
234
|
n = u.getLocaleFromPath(t, a, i);
|
|
@@ -256,21 +256,21 @@ function K(t, e, s, a, i, c = "i18n-locale") {
|
|
|
256
256
|
return n;
|
|
257
257
|
}
|
|
258
258
|
function x(t, e, s) {
|
|
259
|
-
const a = t.map((o) => o.code), i = (o,
|
|
260
|
-
const
|
|
261
|
-
if (!
|
|
259
|
+
const a = t.map((o) => o.code), i = (o, f = []) => {
|
|
260
|
+
const g = o.replace(/^\//, "").replace(/\/$/, "");
|
|
261
|
+
if (!g)
|
|
262
262
|
return "index";
|
|
263
|
-
const
|
|
264
|
-
return
|
|
265
|
-
}, c = (o,
|
|
266
|
-
const
|
|
267
|
-
return
|
|
268
|
-
}, u = (o,
|
|
269
|
-
const
|
|
270
|
-
return
|
|
271
|
-
}, n = (o,
|
|
272
|
-
const
|
|
273
|
-
return
|
|
263
|
+
const h = g.split("/").filter(Boolean), p = h[0];
|
|
264
|
+
return p && f.includes(p) && h.shift(), h.length === 0 ? "index" : h.join("-");
|
|
265
|
+
}, c = (o, f = "en", g = []) => {
|
|
266
|
+
const p = o.split("/").filter(Boolean)[0];
|
|
267
|
+
return p && g.includes(p) ? p : f;
|
|
268
|
+
}, u = (o, f, g = [], h) => {
|
|
269
|
+
const p = o.split("/").filter(Boolean), m = p[0];
|
|
270
|
+
return m && g.includes(m) && p.shift(), (f !== h || h === void 0) && p.unshift(f), `/${p.join("/")}`;
|
|
271
|
+
}, n = (o, f, g = [], h) => {
|
|
272
|
+
const m = (o.replace(/^\//, "").replace(/\/$/, "") || "").split("/").filter(Boolean), L = m[0];
|
|
273
|
+
return L && g.includes(L) && m.shift(), (f !== h || h === void 0) && m.unshift(f), `/${m.join("/")}`;
|
|
274
274
|
};
|
|
275
275
|
return {
|
|
276
276
|
getCurrentPath: () => s ? s().pathname : typeof window < "u" ? window.location.pathname : "/",
|
|
@@ -278,13 +278,13 @@ function x(t, e, s) {
|
|
|
278
278
|
getLocaleFromPath: c,
|
|
279
279
|
switchLocalePath: u,
|
|
280
280
|
localizePath: n,
|
|
281
|
-
removeLocaleFromPath: (o,
|
|
282
|
-
const
|
|
283
|
-
return
|
|
281
|
+
removeLocaleFromPath: (o, f = []) => {
|
|
282
|
+
const g = o.split("/").filter(Boolean), h = g[0];
|
|
283
|
+
return h && f.includes(h) && g.shift(), `/${g.join("/")}`;
|
|
284
284
|
},
|
|
285
|
-
resolvePath: (o,
|
|
286
|
-
const
|
|
287
|
-
return n(
|
|
285
|
+
resolvePath: (o, f) => {
|
|
286
|
+
const g = typeof o == "string" ? o : o.path || "/";
|
|
287
|
+
return n(g, f, a, e);
|
|
288
288
|
},
|
|
289
289
|
getRoute: () => {
|
|
290
290
|
if (s) {
|
|
@@ -338,26 +338,26 @@ function ee(t, e = []) {
|
|
|
338
338
|
const s = t.split("/").filter(Boolean), a = s[0];
|
|
339
339
|
return a && e.includes(a) && s.shift(), `/${s.join("/")}`;
|
|
340
340
|
}
|
|
341
|
-
function
|
|
341
|
+
function D(t) {
|
|
342
342
|
const e = t.locals.i18n;
|
|
343
343
|
if (!e)
|
|
344
344
|
throw new Error("i18n instance not found. Make sure i18n middleware is configured.");
|
|
345
345
|
return e;
|
|
346
346
|
}
|
|
347
|
-
function
|
|
347
|
+
function R(t) {
|
|
348
348
|
return t.locals.locale || "en";
|
|
349
349
|
}
|
|
350
|
-
function
|
|
350
|
+
function T(t) {
|
|
351
351
|
return t.locals.defaultLocale || "en";
|
|
352
352
|
}
|
|
353
|
-
function
|
|
353
|
+
function B(t) {
|
|
354
354
|
return t.locals.locales || [];
|
|
355
355
|
}
|
|
356
|
-
function
|
|
356
|
+
function F(t) {
|
|
357
357
|
return t.locals.routingStrategy || null;
|
|
358
358
|
}
|
|
359
359
|
function te(t) {
|
|
360
|
-
const e =
|
|
360
|
+
const e = D(t), s = R(t), a = T(t), i = B(t), c = i.map((n) => n.code), u = F(t);
|
|
361
361
|
return {
|
|
362
362
|
// Current locale
|
|
363
363
|
locale: s,
|
|
@@ -379,8 +379,8 @@ function te(t) {
|
|
|
379
379
|
return u.getRouteName(l, c);
|
|
380
380
|
const r = l.replace(/^\//, "").replace(/\/$/, "");
|
|
381
381
|
if (!r) return "index";
|
|
382
|
-
const o = r.split("/").filter(Boolean),
|
|
383
|
-
return
|
|
382
|
+
const o = r.split("/").filter(Boolean), f = o[0];
|
|
383
|
+
return f && c.includes(f) && o.shift(), o.length === 0 ? "index" : o.join("-");
|
|
384
384
|
},
|
|
385
385
|
getLocaleFromPath: (n) => {
|
|
386
386
|
const l = n || t.url.pathname;
|
|
@@ -399,15 +399,15 @@ function te(t) {
|
|
|
399
399
|
localizePath: (n, l) => {
|
|
400
400
|
if (u?.localizePath)
|
|
401
401
|
return u.localizePath(n, l || s, c, a);
|
|
402
|
-
const o = (n.replace(/^\//, "").replace(/\/$/, "") || "").split("/").filter(Boolean),
|
|
403
|
-
return
|
|
402
|
+
const o = (n.replace(/^\//, "").replace(/\/$/, "") || "").split("/").filter(Boolean), f = o[0];
|
|
403
|
+
return f && c.includes(f) && o.shift(), l && l !== a && o.unshift(l), `/${o.join("/")}`;
|
|
404
404
|
},
|
|
405
405
|
// Get i18n instance
|
|
406
406
|
getI18n: () => e,
|
|
407
407
|
// Get base path without locale (for rewrite)
|
|
408
408
|
getBasePath: (n) => {
|
|
409
|
-
const o = (n || t.url).pathname.split("/").filter(Boolean),
|
|
410
|
-
return
|
|
409
|
+
const o = (n || t.url).pathname.split("/").filter(Boolean), f = o[0];
|
|
410
|
+
return f && c.includes(f) && o.shift(), o.length > 0 ? `/${o.join("/")}` : "/";
|
|
411
411
|
},
|
|
412
412
|
// Translation management
|
|
413
413
|
addTranslations: (n, l, r = !0) => {
|
|
@@ -425,71 +425,71 @@ function te(t) {
|
|
|
425
425
|
};
|
|
426
426
|
}
|
|
427
427
|
function ne(t, e = {}) {
|
|
428
|
-
const { baseUrl: s = "/", addDirAttribute: a = !0, addSeoAttributes: i = !0 } = e, c =
|
|
429
|
-
if (!
|
|
428
|
+
const { baseUrl: s = "/", addDirAttribute: a = !0, addSeoAttributes: i = !0 } = e, c = R(t), u = T(t), l = B(t).filter((d) => !d.disabled), r = l.filter((d) => d.seo !== !1), o = l.find((d) => d.code === c);
|
|
429
|
+
if (!o)
|
|
430
430
|
return { htmlAttrs: {}, link: [], meta: [] };
|
|
431
|
-
const
|
|
431
|
+
const f = o.iso || c, g = o.dir || "auto", h = {
|
|
432
432
|
htmlAttrs: {
|
|
433
|
-
lang:
|
|
433
|
+
lang: f,
|
|
434
434
|
...a ? { dir: g } : {}
|
|
435
435
|
},
|
|
436
436
|
link: [],
|
|
437
437
|
meta: []
|
|
438
438
|
};
|
|
439
439
|
if (!i)
|
|
440
|
-
return
|
|
441
|
-
const
|
|
442
|
-
|
|
440
|
+
return h;
|
|
441
|
+
const p = `${s}${t.url.pathname}`;
|
|
442
|
+
h.link.push({
|
|
443
443
|
rel: "canonical",
|
|
444
|
-
href:
|
|
444
|
+
href: p
|
|
445
445
|
});
|
|
446
|
-
const m =
|
|
447
|
-
for (const
|
|
448
|
-
let
|
|
446
|
+
const m = F(t), L = l.map((d) => d.code);
|
|
447
|
+
for (const d of r) {
|
|
448
|
+
let b = t.url.pathname;
|
|
449
449
|
if (m?.switchLocalePath)
|
|
450
|
-
|
|
450
|
+
b = m.switchLocalePath(t.url.pathname, d.code, L, u);
|
|
451
451
|
else {
|
|
452
|
-
const
|
|
453
|
-
|
|
452
|
+
const S = t.url.pathname.split("/").filter(Boolean), w = S[0];
|
|
453
|
+
w && L.includes(w) && S.shift(), d.code !== u && S.unshift(d.code), b = `/${S.join("/")}`;
|
|
454
454
|
}
|
|
455
|
-
const
|
|
456
|
-
|
|
455
|
+
const P = `${s}${b}`;
|
|
456
|
+
h.link.push({
|
|
457
457
|
rel: "alternate",
|
|
458
|
-
href:
|
|
459
|
-
hreflang:
|
|
460
|
-
}),
|
|
458
|
+
href: P,
|
|
459
|
+
hreflang: d.code
|
|
460
|
+
}), d.iso && d.iso !== d.code && h.link.push({
|
|
461
461
|
rel: "alternate",
|
|
462
|
-
href:
|
|
463
|
-
hreflang:
|
|
462
|
+
href: P,
|
|
463
|
+
hreflang: d.iso
|
|
464
464
|
});
|
|
465
465
|
}
|
|
466
|
-
{
|
|
467
|
-
let
|
|
466
|
+
if (l.find((d) => d.code === u)?.seo !== !1) {
|
|
467
|
+
let d = t.url.pathname;
|
|
468
468
|
if (m?.switchLocalePath)
|
|
469
|
-
|
|
469
|
+
d = m.switchLocalePath(t.url.pathname, u, L, u);
|
|
470
470
|
else {
|
|
471
|
-
const
|
|
472
|
-
|
|
471
|
+
const b = t.url.pathname.split("/").filter(Boolean), P = b[0];
|
|
472
|
+
P && L.includes(P) && b.shift(), d = `/${b.join("/")}`;
|
|
473
473
|
}
|
|
474
|
-
|
|
474
|
+
h.link.push({
|
|
475
475
|
rel: "alternate",
|
|
476
|
-
href: `${s}${
|
|
476
|
+
href: `${s}${d}`,
|
|
477
477
|
hreflang: "x-default"
|
|
478
478
|
});
|
|
479
479
|
}
|
|
480
|
-
|
|
480
|
+
h.meta.push({
|
|
481
481
|
property: "og:locale",
|
|
482
|
-
content:
|
|
483
|
-
}),
|
|
482
|
+
content: f
|
|
483
|
+
}), h.meta.push({
|
|
484
484
|
property: "og:url",
|
|
485
|
-
content:
|
|
485
|
+
content: p
|
|
486
486
|
});
|
|
487
|
-
for (const
|
|
488
|
-
|
|
487
|
+
for (const d of r)
|
|
488
|
+
d.code !== c && h.meta.push({
|
|
489
489
|
property: "og:locale:alternate",
|
|
490
|
-
content:
|
|
490
|
+
content: d.og || d.iso || d.code
|
|
491
491
|
});
|
|
492
|
-
return
|
|
492
|
+
return h;
|
|
493
493
|
}
|
|
494
494
|
function A(t, e, s) {
|
|
495
495
|
const a = e.split(".");
|
|
@@ -502,7 +502,7 @@ function A(t, e, s) {
|
|
|
502
502
|
c !== void 0 && (i[c] = s);
|
|
503
503
|
}
|
|
504
504
|
function oe(t, e) {
|
|
505
|
-
const s =
|
|
505
|
+
const s = D(t), a = R(t), i = T(t), c = s.getRoute(), u = {};
|
|
506
506
|
if (e && e.length > 0) {
|
|
507
507
|
const n = {};
|
|
508
508
|
for (const l of e) {
|
|
@@ -522,19 +522,19 @@ function oe(t, e) {
|
|
|
522
522
|
};
|
|
523
523
|
}
|
|
524
524
|
export {
|
|
525
|
-
|
|
525
|
+
j as AstroI18n,
|
|
526
526
|
le as FormatService,
|
|
527
527
|
x as createAstroRouterAdapter,
|
|
528
528
|
J as createI18n,
|
|
529
529
|
G as createI18nMiddleware,
|
|
530
530
|
re as defaultPlural,
|
|
531
531
|
K as detectLocale,
|
|
532
|
-
|
|
533
|
-
|
|
532
|
+
T as getDefaultLocale,
|
|
533
|
+
D as getI18n,
|
|
534
534
|
oe as getI18nProps,
|
|
535
|
-
|
|
535
|
+
R as getLocale,
|
|
536
536
|
X as getLocaleFromPath,
|
|
537
|
-
|
|
537
|
+
B as getLocales,
|
|
538
538
|
Q as getRouteName,
|
|
539
539
|
E as i18nIntegration,
|
|
540
540
|
ie as interpolate,
|
package/package.json
CHANGED
|
@@ -1,15 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@i18n-micro/astro",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.4",
|
|
4
|
+
"description": "Astro integration for i18n-micro — translations, routing, and islands.",
|
|
4
5
|
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/s00d/nuxt-i18n-micro.git",
|
|
11
|
+
"directory": "packages/astro"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/s00d/nuxt-i18n-micro/tree/main/packages/astro#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/s00d/nuxt-i18n-micro/issues"
|
|
16
|
+
},
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=18"
|
|
19
|
+
},
|
|
5
20
|
"main": "./dist/index.cjs",
|
|
6
|
-
"module": "./dist/index.mjs",
|
|
7
21
|
"types": "./dist/index.d.ts",
|
|
8
22
|
"exports": {
|
|
9
23
|
".": {
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
24
|
+
"import": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.mjs"
|
|
27
|
+
},
|
|
28
|
+
"require": {
|
|
29
|
+
"types": "./dist/index.d.cts",
|
|
30
|
+
"default": "./dist/index.cjs"
|
|
31
|
+
},
|
|
32
|
+
"default": "./dist/index.mjs"
|
|
13
33
|
},
|
|
14
34
|
"./env": {
|
|
15
35
|
"types": "./dist/env.d.ts"
|
|
@@ -41,19 +61,28 @@
|
|
|
41
61
|
"./client/svelte": {
|
|
42
62
|
"types": "./dist/client/svelte.d.ts",
|
|
43
63
|
"import": "./dist/client/svelte.js"
|
|
44
|
-
}
|
|
64
|
+
},
|
|
65
|
+
"./package.json": "./package.json"
|
|
45
66
|
},
|
|
46
67
|
"files": [
|
|
47
68
|
"dist",
|
|
48
|
-
"src"
|
|
69
|
+
"src",
|
|
70
|
+
"README.md",
|
|
71
|
+
"LICENSE"
|
|
49
72
|
],
|
|
50
73
|
"publishConfig": {
|
|
51
74
|
"access": "public"
|
|
52
75
|
},
|
|
76
|
+
"keywords": [
|
|
77
|
+
"astro",
|
|
78
|
+
"i18n",
|
|
79
|
+
"internationalization",
|
|
80
|
+
"translations"
|
|
81
|
+
],
|
|
53
82
|
"dependencies": {
|
|
54
|
-
"@i18n-micro/core": "1.
|
|
55
|
-
"@i18n-micro/
|
|
56
|
-
"@i18n-micro/
|
|
83
|
+
"@i18n-micro/core": "1.3.1",
|
|
84
|
+
"@i18n-micro/node": "1.2.1",
|
|
85
|
+
"@i18n-micro/types": "1.2.2"
|
|
57
86
|
},
|
|
58
87
|
"peerDependencies": {
|
|
59
88
|
"astro": "^5.16.5",
|
|
@@ -86,12 +115,16 @@
|
|
|
86
115
|
"svelte": "^4.0.0 || ^5.0.0",
|
|
87
116
|
"vue": "^3.5.25",
|
|
88
117
|
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
|
|
89
|
-
"preact": "^10.0.0"
|
|
118
|
+
"preact": "^10.0.0",
|
|
119
|
+
"publint": "^0.3.17",
|
|
120
|
+
"vitest": "^3.2.4"
|
|
90
121
|
},
|
|
91
122
|
"scripts": {
|
|
92
123
|
"build": "vite build",
|
|
124
|
+
"check:package": "publint",
|
|
93
125
|
"dev": "cd playground && astro dev",
|
|
94
126
|
"test": "jest",
|
|
127
|
+
"test:dist": "vitest run --config vitest.dist.config.ts",
|
|
95
128
|
"typecheck": "tsc --noEmit"
|
|
96
129
|
}
|
|
97
130
|
}
|
package/src/utils.ts
CHANGED
|
@@ -217,6 +217,7 @@ export function useLocaleHead(astro: AstroGlobal, options: LocaleHeadOptions = {
|
|
|
217
217
|
const defaultLocale = getDefaultLocale(astro)
|
|
218
218
|
const allLocales = getLocales(astro)
|
|
219
219
|
const locales = allLocales.filter((l) => !l.disabled)
|
|
220
|
+
const localesForSeo = locales.filter((l) => l.seo !== false)
|
|
220
221
|
const currentLocaleObj = locales.find((l) => l.code === locale)
|
|
221
222
|
|
|
222
223
|
if (!currentLocaleObj) {
|
|
@@ -251,7 +252,7 @@ export function useLocaleHead(astro: AstroGlobal, options: LocaleHeadOptions = {
|
|
|
251
252
|
const allLocaleCodes = locales.map((l) => l.code)
|
|
252
253
|
|
|
253
254
|
// Alternate languages (includes current locale for self-referencing hreflang, per Google guidelines)
|
|
254
|
-
for (const loc of
|
|
255
|
+
for (const loc of localesForSeo) {
|
|
255
256
|
let alternatePath = astro.url.pathname
|
|
256
257
|
if (routingStrategy?.switchLocalePath) {
|
|
257
258
|
alternatePath = routingStrategy.switchLocalePath(astro.url.pathname, loc.code, allLocaleCodes, defaultLocale)
|
|
@@ -287,7 +288,8 @@ export function useLocaleHead(astro: AstroGlobal, options: LocaleHeadOptions = {
|
|
|
287
288
|
// x-default hreflang — points to the default locale's URL.
|
|
288
289
|
// Tells search engines which URL to show when none of the
|
|
289
290
|
// specified languages match the user's browser settings.
|
|
290
|
-
|
|
291
|
+
const defaultLocaleObj = locales.find((l) => l.code === defaultLocale)
|
|
292
|
+
if (defaultLocaleObj?.seo !== false) {
|
|
291
293
|
let xDefaultPath = astro.url.pathname
|
|
292
294
|
if (routingStrategy?.switchLocalePath) {
|
|
293
295
|
xDefaultPath = routingStrategy.switchLocalePath(astro.url.pathname, defaultLocale, allLocaleCodes, defaultLocale)
|
|
@@ -318,11 +320,11 @@ export function useLocaleHead(astro: AstroGlobal, options: LocaleHeadOptions = {
|
|
|
318
320
|
})
|
|
319
321
|
|
|
320
322
|
// Alternate OG locales
|
|
321
|
-
for (const loc of
|
|
323
|
+
for (const loc of localesForSeo) {
|
|
322
324
|
if (loc.code === locale) continue
|
|
323
325
|
result.meta.push({
|
|
324
326
|
property: 'og:locale:alternate',
|
|
325
|
-
content: loc.iso || loc.code,
|
|
327
|
+
content: loc.og || loc.iso || loc.code,
|
|
326
328
|
})
|
|
327
329
|
}
|
|
328
330
|
|