@i18n-micro/astro 1.2.0 → 1.3.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/core.d.ts +4 -4
- package/dist/client/index.d.ts +3 -3
- package/dist/client/index.js +9 -9
- package/dist/client/preact.d.ts +1 -1
- package/dist/client/preact.js +36 -21
- package/dist/client/react.d.ts +1 -1
- package/dist/client/react.js +35 -20
- package/dist/client/svelte.d.ts +4 -4
- package/dist/client/svelte.js +10 -16
- package/dist/client/vue.d.ts +4 -4
- package/dist/client/vue.js +6 -12
- package/dist/composer.d.ts +1 -2
- package/dist/env.d.ts +0 -2
- package/dist/index.cjs +2 -2
- package/dist/index.d.ts +11 -11
- package/dist/index.mjs +220 -233
- package/dist/integration.d.ts +1 -1
- package/dist/load-translations.d.ts +1 -1
- package/dist/middleware.d.ts +2 -2
- package/dist/router/adapter.d.ts +1 -1
- package/dist/utils.d.ts +7 -8
- package/package.json +4 -4
- package/src/client/core.ts +19 -25
- package/src/client/index.ts +7 -9
- package/src/client/preact.tsx +74 -61
- package/src/client/react.tsx +73 -61
- package/src/client/svelte.ts +14 -30
- package/src/client/vue.ts +10 -26
- package/src/components/i18n-link.astro +3 -3
- package/src/components/i18n-switcher.astro +3 -3
- package/src/components/i18n-t.astro +3 -13
- package/src/composer.ts +3 -12
- package/src/env.d.ts +0 -2
- package/src/index.ts +35 -42
- package/src/integration.ts +7 -12
- package/src/load-translations.ts +23 -24
- package/src/middleware.ts +13 -23
- package/src/router/adapter.ts +7 -25
- package/src/routing.ts +4 -18
- package/src/utils.ts +25 -37
package/dist/client/core.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Params, Translations } from '@i18n-micro/types';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* I18n state for client islands
|
|
4
4
|
*/
|
|
5
5
|
export interface I18nState {
|
|
6
6
|
locale: string;
|
|
@@ -9,7 +9,7 @@ export interface I18nState {
|
|
|
9
9
|
currentRoute: string;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* Pure function to get a translation from state
|
|
13
13
|
*
|
|
14
14
|
* Note: This is a simplified version optimized for client-side islands.
|
|
15
15
|
* It supports basic translation lookup, interpolation, and fallback to general translations.
|
|
@@ -18,6 +18,6 @@ export interface I18nState {
|
|
|
18
18
|
*/
|
|
19
19
|
export declare function translate(state: I18nState, key: string, params?: Params, defaultValue?: string | null, routeName?: string): string | number | boolean | Translations | null;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Checks if a translation exists in the state
|
|
22
22
|
*/
|
|
23
23
|
export declare function hasTranslation(state: I18nState, key: string, routeName?: string): boolean;
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { translate, hasTranslation } from './core';
|
|
2
1
|
export type { I18nState } from './core';
|
|
3
|
-
export {
|
|
4
|
-
export { I18nProvider, useAstroI18n as useAstroI18nReact } from './react';
|
|
2
|
+
export { hasTranslation, translate } from './core';
|
|
5
3
|
export { I18nProvider as I18nProviderPreact, useAstroI18n as useAstroI18nPreact } from './preact';
|
|
4
|
+
export { I18nProvider, useAstroI18n as useAstroI18nReact } from './react';
|
|
6
5
|
export { createI18nStore, useAstroI18n as useAstroI18nSvelte } from './svelte';
|
|
6
|
+
export { provideI18n, useAstroI18n as useAstroI18nVue } from './vue';
|
package/dist/client/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { h as s, t as o } from "../core-D32Y48CN.js";
|
|
2
|
-
import {
|
|
2
|
+
import { I18nProvider as a, useAstroI18n as n } from "./preact.js";
|
|
3
3
|
import { I18nProvider as u, useAstroI18n as A } from "./react.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { createI18nStore as f, useAstroI18n as i } from "./svelte.js";
|
|
5
|
+
import { provideI18n as v, useAstroI18n as x } from "./vue.js";
|
|
6
6
|
export {
|
|
7
7
|
u as I18nProvider,
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
a as I18nProviderPreact,
|
|
9
|
+
f as createI18nStore,
|
|
10
10
|
s as hasTranslation,
|
|
11
|
-
|
|
11
|
+
v as provideI18n,
|
|
12
12
|
o as translate,
|
|
13
|
-
|
|
13
|
+
n as useAstroI18nPreact,
|
|
14
14
|
A as useAstroI18nReact,
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
i as useAstroI18nSvelte,
|
|
16
|
+
x as useAstroI18nVue
|
|
17
17
|
};
|
package/dist/client/preact.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { CleanTranslation, Params, TranslationKey } from '@i18n-micro/types';
|
|
1
2
|
import { ComponentChildren } from 'preact';
|
|
2
3
|
import { I18nClientProps } from '../utils';
|
|
3
4
|
import { I18nState } from './core';
|
|
4
|
-
import { Params, TranslationKey, CleanTranslation } from '@i18n-micro/types';
|
|
5
5
|
/**
|
|
6
6
|
* Провайдер для i18n в Preact островах
|
|
7
7
|
*/
|
package/dist/client/preact.js
CHANGED
|
@@ -1,33 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
const s = new
|
|
6
|
-
const [l] =
|
|
1
|
+
import { FormatService as g, defaultPlural as w } from "@i18n-micro/core";
|
|
2
|
+
import { createContext as x, createElement as L } from "preact";
|
|
3
|
+
import { useState as P, useContext as S, useMemo as a } from "preact/hooks";
|
|
4
|
+
import { t as C, h as A } from "../core-D32Y48CN.js";
|
|
5
|
+
const s = new g(), i = x(null), F = ({ children: t, value: o }) => {
|
|
6
|
+
const [l] = P(() => ({
|
|
7
7
|
locale: o.locale,
|
|
8
8
|
fallbackLocale: o.fallbackLocale,
|
|
9
9
|
translations: o.translations,
|
|
10
10
|
currentRoute: o.currentRoute
|
|
11
11
|
}));
|
|
12
|
-
return
|
|
12
|
+
return L(i.Provider, { value: l }, t);
|
|
13
13
|
};
|
|
14
14
|
function M() {
|
|
15
|
-
const t =
|
|
15
|
+
const t = S(i);
|
|
16
16
|
if (!t)
|
|
17
17
|
throw new Error("useAstroI18n must be used within an I18nProvider");
|
|
18
|
-
const o = a(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
u
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
18
|
+
const o = a(
|
|
19
|
+
() => (r, e, n, c) => C(t, r, e, n, c),
|
|
20
|
+
[t]
|
|
21
|
+
), l = a(
|
|
22
|
+
() => (r, e, n, c) => o(r, e, n, c)?.toString() ?? n ?? r,
|
|
23
|
+
[o]
|
|
24
|
+
), m = a(
|
|
25
|
+
() => (r, e, n) => {
|
|
26
|
+
const { count: c, ...u } = typeof e == "number" ? { count: e } : e;
|
|
27
|
+
if (c === void 0)
|
|
28
|
+
return n ?? r;
|
|
29
|
+
const d = (v, I, h) => o(v, I, h);
|
|
30
|
+
return w(r, Number.parseInt(c.toString(), 10), u, t.locale, d) ?? n ?? r;
|
|
31
|
+
},
|
|
32
|
+
[o, t]
|
|
33
|
+
), f = a(
|
|
34
|
+
() => (r, e) => s.formatNumber(r, t.locale, e),
|
|
35
|
+
[t.locale]
|
|
36
|
+
), b = a(
|
|
37
|
+
() => (r, e) => s.formatDate(r, t.locale, e),
|
|
38
|
+
[t.locale]
|
|
39
|
+
), p = a(
|
|
40
|
+
() => (r, e) => s.formatRelativeTime(r, t.locale, e),
|
|
41
|
+
[t.locale]
|
|
42
|
+
), R = a(
|
|
43
|
+
() => (r, e) => A(t, r, e),
|
|
44
|
+
[t]
|
|
45
|
+
);
|
|
31
46
|
return {
|
|
32
47
|
// Translation methods
|
|
33
48
|
t: o,
|
package/dist/client/react.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { CleanTranslation, Params, TranslationKey } from '@i18n-micro/types';
|
|
1
2
|
import { default as React } from 'react';
|
|
2
3
|
import { I18nClientProps } from '../utils';
|
|
3
|
-
import { Params, TranslationKey, CleanTranslation } from '@i18n-micro/types';
|
|
4
4
|
/**
|
|
5
5
|
* Провайдер для i18n в React островах
|
|
6
6
|
*/
|
package/dist/client/react.js
CHANGED
|
@@ -1,33 +1,48 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
const s = new
|
|
1
|
+
import { FormatService as g, defaultPlural as w } from "@i18n-micro/core";
|
|
2
|
+
import x, { createContext as L, useState as P, useContext as S, useMemo as a } from "react";
|
|
3
|
+
import { t as C, h as A } from "../core-D32Y48CN.js";
|
|
4
|
+
const s = new g(), i = L(null);
|
|
5
5
|
function D({ children: t, value: o }) {
|
|
6
|
-
const [l] =
|
|
6
|
+
const [l] = P(() => ({
|
|
7
7
|
locale: o.locale,
|
|
8
8
|
fallbackLocale: o.fallbackLocale,
|
|
9
9
|
translations: o.translations,
|
|
10
10
|
currentRoute: o.currentRoute
|
|
11
11
|
}));
|
|
12
|
-
return
|
|
12
|
+
return x.createElement(i.Provider, { value: l }, t);
|
|
13
13
|
}
|
|
14
14
|
function F() {
|
|
15
|
-
const t =
|
|
15
|
+
const t = S(i);
|
|
16
16
|
if (!t)
|
|
17
17
|
throw new Error("useAstroI18n must be used within an I18nProvider");
|
|
18
|
-
const o = a(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
u
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
18
|
+
const o = a(
|
|
19
|
+
() => (e, r, n, c) => C(t, e, r, n, c),
|
|
20
|
+
[t]
|
|
21
|
+
), l = a(
|
|
22
|
+
() => (e, r, n, c) => o(e, r, n, c)?.toString() ?? n ?? e,
|
|
23
|
+
[o]
|
|
24
|
+
), m = a(
|
|
25
|
+
() => (e, r, n) => {
|
|
26
|
+
const { count: c, ...u } = typeof r == "number" ? { count: r } : r;
|
|
27
|
+
if (c === void 0)
|
|
28
|
+
return n ?? e;
|
|
29
|
+
const d = (v, I, h) => o(v, I, h);
|
|
30
|
+
return w(e, Number.parseInt(c.toString(), 10), u, t.locale, d) ?? n ?? e;
|
|
31
|
+
},
|
|
32
|
+
[o, t]
|
|
33
|
+
), f = a(
|
|
34
|
+
() => (e, r) => s.formatNumber(e, t.locale, r),
|
|
35
|
+
[t.locale]
|
|
36
|
+
), b = a(
|
|
37
|
+
() => (e, r) => s.formatDate(e, t.locale, r),
|
|
38
|
+
[t.locale]
|
|
39
|
+
), R = a(
|
|
40
|
+
() => (e, r) => s.formatRelativeTime(e, t.locale, r),
|
|
41
|
+
[t.locale]
|
|
42
|
+
), p = a(
|
|
43
|
+
() => (e, r) => A(t, e, r),
|
|
44
|
+
[t]
|
|
45
|
+
);
|
|
31
46
|
return {
|
|
32
47
|
// Translation methods
|
|
33
48
|
t: o,
|
package/dist/client/svelte.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
+
import { CleanTranslation, Params, TranslationKey } from '@i18n-micro/types';
|
|
1
2
|
import { Writable } from 'svelte/store';
|
|
2
3
|
import { I18nClientProps } from '../utils';
|
|
3
4
|
import { I18nState } from './core';
|
|
4
|
-
import { Params, TranslationKey, CleanTranslation } from '@i18n-micro/types';
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Creates a Svelte store for i18n state
|
|
7
7
|
*/
|
|
8
8
|
export declare function createI18nStore(props: I18nClientProps): Writable<I18nState>;
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
10
|
+
* Hook for using i18n in Svelte components
|
|
11
|
+
* Use in the component's <script> block
|
|
12
12
|
*/
|
|
13
13
|
export declare function useAstroI18n(store: Writable<I18nState>): {
|
|
14
14
|
store: Writable<I18nState>;
|
package/dist/client/svelte.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
const l = new
|
|
1
|
+
import { FormatService as R, defaultPlural as b } from "@i18n-micro/core";
|
|
2
|
+
import { writable as d, get as v } from "svelte/store";
|
|
3
|
+
import { h as L, t as S } from "../core-D32Y48CN.js";
|
|
4
|
+
const l = new R();
|
|
5
5
|
function F(o) {
|
|
6
|
-
return
|
|
6
|
+
return d({
|
|
7
7
|
locale: o.locale,
|
|
8
8
|
fallbackLocale: o.fallbackLocale,
|
|
9
9
|
translations: o.translations,
|
|
@@ -11,9 +11,9 @@ function F(o) {
|
|
|
11
11
|
});
|
|
12
12
|
}
|
|
13
13
|
function P(o) {
|
|
14
|
-
const n = () =>
|
|
14
|
+
const n = () => v(o), c = (t, e, r, a) => S(n(), t, e, r, a);
|
|
15
15
|
return {
|
|
16
|
-
// Store
|
|
16
|
+
// Store for reactivity in templates (use $i18nStore in templates)
|
|
17
17
|
store: o,
|
|
18
18
|
// Translation methods
|
|
19
19
|
t: c,
|
|
@@ -23,13 +23,7 @@ function P(o) {
|
|
|
23
23
|
if (u === void 0)
|
|
24
24
|
return r ?? t;
|
|
25
25
|
const i = (m, f, g) => c(m, f, g);
|
|
26
|
-
return
|
|
27
|
-
t,
|
|
28
|
-
Number.parseInt(u.toString(), 10),
|
|
29
|
-
s,
|
|
30
|
-
a.locale,
|
|
31
|
-
i
|
|
32
|
-
) ?? r ?? t;
|
|
26
|
+
return b(t, Number.parseInt(u.toString(), 10), s, a.locale, i) ?? r ?? t;
|
|
33
27
|
},
|
|
34
28
|
tn: (t, e) => {
|
|
35
29
|
const r = n();
|
|
@@ -43,8 +37,8 @@ function P(o) {
|
|
|
43
37
|
const r = n();
|
|
44
38
|
return l.formatRelativeTime(t, r.locale, e);
|
|
45
39
|
},
|
|
46
|
-
has: (t, e) =>
|
|
47
|
-
//
|
|
40
|
+
has: (t, e) => L(n(), t, e),
|
|
41
|
+
// Getters for current state (for use in scripts)
|
|
48
42
|
get locale() {
|
|
49
43
|
return n().locale;
|
|
50
44
|
},
|
package/dist/client/vue.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
+
import { CleanTranslation, Params, TranslationKey } from '@i18n-micro/types';
|
|
1
2
|
import { Ref } from 'vue';
|
|
2
3
|
import { I18nClientProps } from '../utils';
|
|
3
4
|
import { I18nState } from './core';
|
|
4
|
-
import { Params, TranslationKey, CleanTranslation } from '@i18n-micro/types';
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* Initializes the i18n provider for a Vue island
|
|
7
|
+
* Call in the island's root component
|
|
8
8
|
*/
|
|
9
9
|
export declare function provideI18n(props: I18nClientProps): Ref<I18nState>;
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* Hook for using i18n in Vue components
|
|
12
12
|
*/
|
|
13
13
|
export declare function useAstroI18n(): {
|
|
14
14
|
t: (key: TranslationKey, params?: Params, defaultValue?: string | null, routeName?: string) => CleanTranslation;
|
package/dist/client/vue.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { FormatService as wt, defaultPlural as yt } from "@i18n-micro/core";
|
|
2
|
+
import { h as St, t as Ot } from "../core-D32Y48CN.js";
|
|
3
3
|
// @__NO_SIDE_EFFECTS__
|
|
4
4
|
function Rt(e) {
|
|
5
5
|
const t = /* @__PURE__ */ Object.create(null);
|
|
@@ -1492,7 +1492,7 @@ function Jn() {
|
|
|
1492
1492
|
Yn();
|
|
1493
1493
|
}
|
|
1494
1494
|
process.env.NODE_ENV !== "production" && Jn();
|
|
1495
|
-
const me = new
|
|
1495
|
+
const me = new wt(), Et = /* @__PURE__ */ Symbol("i18n-astro");
|
|
1496
1496
|
function Gn(e) {
|
|
1497
1497
|
const t = sn({
|
|
1498
1498
|
locale: e.locale,
|
|
@@ -1506,7 +1506,7 @@ function Qn() {
|
|
|
1506
1506
|
const e = Cn(Et);
|
|
1507
1507
|
if (!e)
|
|
1508
1508
|
throw new Error("useAstroI18n must be used within a component that calls provideI18n");
|
|
1509
|
-
const t = (a, u, d, l) =>
|
|
1509
|
+
const t = (a, u, d, l) => Ot(e.value, a, u, d, l);
|
|
1510
1510
|
return {
|
|
1511
1511
|
// Translation methods
|
|
1512
1512
|
t,
|
|
@@ -1516,18 +1516,12 @@ function Qn() {
|
|
|
1516
1516
|
if (l === void 0)
|
|
1517
1517
|
return d ?? a;
|
|
1518
1518
|
const g = (P, bt, Nt) => t(P, bt, Nt);
|
|
1519
|
-
return
|
|
1520
|
-
a,
|
|
1521
|
-
Number.parseInt(l.toString(), 10),
|
|
1522
|
-
f,
|
|
1523
|
-
e.value.locale,
|
|
1524
|
-
g
|
|
1525
|
-
) ?? d ?? a;
|
|
1519
|
+
return yt(a, Number.parseInt(l.toString(), 10), f, e.value.locale, g) ?? d ?? a;
|
|
1526
1520
|
},
|
|
1527
1521
|
tn: (a, u) => me.formatNumber(a, e.value.locale, u),
|
|
1528
1522
|
td: (a, u) => me.formatDate(a, e.value.locale, u),
|
|
1529
1523
|
tdr: (a, u) => me.formatRelativeTime(a, e.value.locale, u),
|
|
1530
|
-
has: (a, u) =>
|
|
1524
|
+
has: (a, u) => St(e.value, a, u),
|
|
1531
1525
|
// Reactive locale state
|
|
1532
1526
|
locale: ve({
|
|
1533
1527
|
get: () => e.value.locale,
|
package/dist/composer.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BaseI18n, TranslationStorage } from '@i18n-micro/core';
|
|
2
|
-
import {
|
|
2
|
+
import { PluralFunc, Translations } from '@i18n-micro/types';
|
|
3
3
|
export interface AstroI18nOptions {
|
|
4
4
|
locale: string;
|
|
5
5
|
fallbackLocale?: string;
|
|
@@ -30,6 +30,5 @@ export declare class AstroI18n extends BaseI18n {
|
|
|
30
30
|
addTranslations(locale: string, translations: Translations, merge?: boolean): void;
|
|
31
31
|
addRouteTranslations(locale: string, routeName: string, translations: Translations, merge?: boolean): void;
|
|
32
32
|
mergeTranslations(locale: string, routeName: string, translations: Translations): void;
|
|
33
|
-
mergeGlobalTranslations(locale: string, translations: Translations): void;
|
|
34
33
|
clearCache(): void;
|
|
35
34
|
}
|
package/dist/env.d.ts
CHANGED
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"),b=require("node:fs"),I=require("node:path");class w extends j.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="index",e.messages){this.initialMessages={...e.messages};for(const[o,i]of Object.entries(e.messages))this.helper.loadTranslations(o,i)}}cloneStorage(e){const s=new Map;for(const[o,i]of e.translations)s.set(o,{...i});return{translations:s}}clone(e){const s=this.cloneStorage(this.storage);return new w({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 o=`${e}:${s}`;return this.storage.translations.get(o)??null}addTranslations(e,s,o=!0){super.loadTranslationsCore(e,s,o)}addRouteTranslations(e,s,o,i=!0){super.loadRouteTranslationsCore(e,s,o,i)}mergeTranslations(e,s,o){this.helper.mergeTranslation(e,s,o,!0)}clearCache(){const e={...this.initialMessages};if(super.clearCache(),Object.keys(e).length>0)for(const[s,o]of Object.entries(e))this.helper.loadTranslations(s,o)}}let $=null;function D(){return $}function C(n){const{locale:e,fallbackLocale:s,translationDir:o,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(a=>a.code),translationDir:o||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(a){if(a===t)return l},load(a){if(a===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' {
|
|
@@ -14,4 +14,4 @@
|
|
|
14
14
|
missingWarn: boolean | null;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
`})}}}}function N(n){return new
|
|
17
|
+
`})}}}}function N(n){return new w(n)}function F(n){const{translationDir:e,rootDir:s=process.cwd(),disablePageLocales:o=!1}=n,i=I.resolve(s,e);if(!b.existsSync(i))return console.warn(`[i18n] Translation directory not found: ${i}`),{root:{},routes:{}};const c={},f={},t=(l,r="")=>{if(!b.existsSync(l))return;const a=b.readdirSync(l);for(const u of a){const h=I.join(l,u);if(b.statSync(h).isDirectory())u==="pages"&&!o?t(h,""):r||o?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&&!o?(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),{root:c,routes:f}}function q(n,e){const{root:s,routes:o}=F(e);for(const[i,c]of Object.entries(s))n.addTranslations(i,c,!1);for(const[i,c]of Object.entries(o))for(const[f,t]of Object.entries(c)){const l=s[f]||{};n.addRouteTranslations(f,i,{...l,...t},!1)}}function M(n){const{i18n:e,defaultLocale:s,locales:o,localeObjects:i,autoDetect:c=!0,redirectToDefault:f=!1,routingStrategy:t}=n,l=t||D();return async(r,a)=>{if(r.locals.locale&&r.locals.i18n)return a();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||o.map(_=>({code:_})),r.locals.currentUrl=u,a()}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&&o.includes(g);let P;p&&g?P=g:d.getLocaleFromPath?P=d.getLocaleFromPath(h,s,o):P=s;const L=e.clone(P),y=d.getRouteName?d.getRouteName(h,o):"index";return L.setRoute(y),r.locals.i18n=L,r.locals.locale=P,r.locals.defaultLocale=s,r.locals.locales=i||o.map(S=>({code:S})),r.locals.currentUrl=u,r.locals.routingStrategy=d,a()}}function A(n){const e=[],s=n.split(",");for(const o of s){const[i,c="1.0"]=o.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 W(n,e,s,o,i,c="i18n-locale"){const f=D();let t=o;if(f?.getLocaleFromPath)t=f.getLocaleFromPath(n,o,i);else{const r=n.split("/").filter(Boolean)[0];r&&i.includes(r)&&(t=r)}if(c!==null&&t===o&&e.get(c)){const l=e.get(c)?.value;l&&i.includes(l)&&(t=l)}if(t===o)try{const l=s.get("accept-language");if(l){const r=A(l);for(const a of r)if(i.includes(a)){t=a;break}}}catch{}return t}function z(n,e,s){const o=n.map(a=>a.code),i=(a,u=[])=>{const h=a.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=(a,u="en",h=[])=>{const m=a.split("/").filter(Boolean)[0];return m&&h.includes(m)?m:u},f=(a,u,h=[],d)=>{const m=a.split("/").filter(Boolean),g=m[0];return g&&h.includes(g)&&m.shift(),(u!==d||d===void 0)&&m.unshift(u),`/${m.join("/")}`},t=(a,u,h=[],d)=>{const g=(a.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:(a,u=[])=>{const h=a.split("/").filter(Boolean),d=h[0];return d&&u.includes(d)&&h.shift(),`/${h.join("/")}`},resolvePath:(a,u)=>{const h=typeof a=="string"?a:a.path||"/";return t(h,u,o,e)},getRoute:()=>{if(s){const a=s();return{fullPath:a.pathname+a.search,query:Object.fromEntries(a.searchParams)}}if(typeof window<"u"){const a=new URL(window.location.href);return{fullPath:a.pathname+a.search,query:Object.fromEntries(a.searchParams)}}return{fullPath:"/",query:{}}},push:a=>{typeof window<"u"&&(window.location.href=a.path)},replace:a=>{typeof window<"u"&&window.location.replace(a.path)}}}function H(n,e=[]){const s=n.replace(/^\//,"").replace(/\/$/,"");if(!s)return"index";const o=s.split("/").filter(Boolean),i=o[0];return i&&e.includes(i)&&o.shift(),o.length===0?"index":o.join("-")}function U(n,e="en",s=[]){const i=n.split("/").filter(Boolean)[0];return i&&s.includes(i)?i:e}function E(n,e,s=[],o){const i=n.split("/").filter(Boolean),c=i[0];return c&&s.includes(c)&&i.shift(),(e!==o||o===void 0)&&i.unshift(e),`/${i.join("/")}`}function J(n,e,s=[],o){const c=(n.replace(/^\//,"").replace(/\/$/,"")||"").split("/").filter(Boolean),f=c[0];return f&&s.includes(f)&&c.shift(),(e!==o||o===void 0)&&c.unshift(e),`/${c.join("/")}`}function V(n,e=[]){const s=n.split("/").filter(Boolean),o=s[0];return o&&e.includes(o)&&s.shift(),`/${s.join("/")}`}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 R(n){return n.locals.locale||"en"}function T(n){return n.locals.defaultLocale||"en"}function k(n){return n.locals.locales||[]}function B(n){return n.locals.routingStrategy||null}function G(n){const e=v(n),s=R(n),o=T(n),i=k(n),c=i.map(t=>t.code),f=B(n);return{locale:s,defaultLocale:o,locales:i,t:(t,l,r,a)=>e.t(t,l,r,a),ts:(t,l,r,a)=>e.ts(t,l,r,a),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 a=r.split("/").filter(Boolean),u=a[0];return u&&c.includes(u)&&a.shift(),a.length===0?"index":a.join("-")},getLocaleFromPath:t=>{const l=t||n.url.pathname;if(f?.getLocaleFromPath)return f.getLocaleFromPath(l,o,c);const a=l.split("/").filter(Boolean)[0];return a&&c.includes(a)?a:o},switchLocalePath:t=>{if(f?.switchLocalePath)return f.switchLocalePath(n.url.pathname,t,c,o);const l=n.url.pathname.split("/").filter(Boolean),r=l[0];return r&&c.includes(r)&&l.shift(),t!==o&&l.unshift(t),`/${l.join("/")}`},localizePath:(t,l)=>{if(f?.localizePath)return f.localizePath(t,l||s,c,o);const a=(t.replace(/^\//,"").replace(/\/$/,"")||"").split("/").filter(Boolean),u=a[0];return u&&c.includes(u)&&a.shift(),l&&l!==o&&a.unshift(l),`/${a.join("/")}`},getI18n:()=>e,getBasePath:t=>{const a=(t||n.url).pathname.split("/").filter(Boolean),u=a[0];return u&&c.includes(u)&&a.shift(),a.length>0?`/${a.join("/")}`:"/"},addTranslations:(t,l,r=!0)=>{e.addTranslations(t,l,r)},addRouteTranslations:(t,l,r,a=!0)=>{e.addRouteTranslations(t,l,r,a)},mergeTranslations:(t,l,r)=>{e.mergeTranslations(t,l,r)},clearCache:()=>{e.clearCache()}}}function K(n,e={}){const{baseUrl:s="/",addDirAttribute:o=!0,addSeoAttributes:i=!0}=e,c=R(n),f=T(n),t=k(n),l=t.find(g=>g.code===c);if(!l)return{htmlAttrs:{},link:[],meta:[]};const r=l.iso||c,a=l.dir||"auto",u={htmlAttrs:{lang:r,...o?{dir:a}:{}},link:[],meta:[]};if(!i)return u;const h=`${s}${n.url.pathname}`;u.link.push({rel:"canonical",href:h});const d=B(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 Q(n,e,s){const o=e.split(".");let i=n;for(let f=0;f<o.length-1;f++){const t=o[f];i[t]||(i[t]={}),i=i[t]}const c=o[o.length-1];c!==void 0&&(i[c]=s)}function X(n,e){const s=v(n),o=R(n),i=T(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&&Q(t,l,r)}Object.keys(t).length>0&&(f[c]=t)}else{const t=s.getRouteTranslations(o,c);t&&(f[c]=t)}return{locale:o,fallbackLocale:i,currentRoute:c,translations:f}}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=w;exports.createAstroRouterAdapter=z;exports.createI18n=N;exports.createI18nMiddleware=M;exports.detectLocale=W;exports.getDefaultLocale=T;exports.getI18n=v;exports.getI18nProps=X;exports.getLocale=R;exports.getLocaleFromPath=U;exports.getLocales=k;exports.getRouteName=H;exports.i18nIntegration=C;exports.loadTranslationsFromDir=F;exports.loadTranslationsIntoI18n=q;exports.localizePath=J;exports.removeLocaleFromPath=V;exports.switchLocalePath=E;exports.useI18n=G;exports.useLocaleHead=K;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { defaultPlural, FormatService, interpolate } from '@i18n-micro/core';
|
|
2
|
+
export type { CleanTranslation, Getter, Locale, LocaleCode, Params, PluralFunc, Translations, } from '@i18n-micro/types';
|
|
2
3
|
export { AstroI18n, type AstroI18nOptions } from './composer';
|
|
3
|
-
export { createI18nMiddleware, detectLocale } from './middleware';
|
|
4
|
-
export type { I18nMiddlewareOptions } from './middleware';
|
|
5
|
-
export { useI18n, getI18n, getLocale, getDefaultLocale, getLocales, useLocaleHead, getI18nProps, } from './utils';
|
|
6
|
-
export type { LocaleHeadOptions, LocaleHeadResult, I18nClientProps } from './utils';
|
|
7
|
-
export type { I18nRoutingStrategy } from './router/types';
|
|
8
|
-
export { createAstroRouterAdapter } from './router/adapter';
|
|
9
|
-
export { getRouteName, getLocaleFromPath, switchLocalePath, localizePath, removeLocaleFromPath, } from './routing';
|
|
10
|
-
export type { Translations, Params, PluralFunc, Getter, Locale, LocaleCode, CleanTranslation, } from '@i18n-micro/types';
|
|
11
|
-
export { interpolate, FormatService, defaultPlural } from '@i18n-micro/core';
|
|
12
4
|
export type { I18nIntegrationOptions } from './integration';
|
|
5
|
+
export { createI18n, i18nIntegration } from './integration';
|
|
6
|
+
export type { LoadedTranslations, LoadTranslationsOptions } from './load-translations';
|
|
13
7
|
export { loadTranslationsFromDir, loadTranslationsIntoI18n, } from './load-translations';
|
|
14
|
-
export type {
|
|
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';
|