@i18n-micro/astro 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/dist/client/core.d.ts +23 -0
  2. package/dist/client/index.d.ts +6 -0
  3. package/dist/client/index.js +17 -0
  4. package/dist/client/preact.d.ts +30 -0
  5. package/dist/client/preact.js +51 -0
  6. package/dist/client/react.d.ts +26 -0
  7. package/dist/client/react.js +51 -0
  8. package/dist/client/svelte.d.ts +28 -0
  9. package/dist/client/svelte.js +70 -0
  10. package/dist/client/vue.d.ts +27 -0
  11. package/dist/client/vue.js +1558 -0
  12. package/dist/composer.d.ts +9 -18
  13. package/dist/core-Bx9n-eFD.cjs +1 -0
  14. package/dist/core-D32Y48CN.js +42 -0
  15. package/dist/env.d.ts +2 -0
  16. package/dist/index.cjs +17 -1
  17. package/dist/index.d.ts +6 -2
  18. package/dist/index.mjs +431 -297
  19. package/dist/integration.d.ts +5 -1
  20. package/dist/load-translations.d.ts +44 -0
  21. package/dist/middleware.d.ts +3 -1
  22. package/dist/router/adapter.d.ts +8 -0
  23. package/dist/router/types.d.ts +65 -0
  24. package/dist/utils.d.ts +17 -1
  25. package/package.json +57 -13
  26. package/src/client/core.ts +121 -0
  27. package/src/client/index.ts +15 -0
  28. package/src/client/preact.tsx +114 -0
  29. package/src/client/react.tsx +111 -0
  30. package/src/client/svelte.ts +124 -0
  31. package/src/client/vue.ts +128 -0
  32. package/src/components/i18n-link.astro +37 -4
  33. package/src/components/i18n-switcher.astro +209 -17
  34. package/src/components/index.ts +8 -2
  35. package/src/composer.ts +138 -0
  36. package/src/env.d.ts +20 -0
  37. package/src/index.ts +59 -0
  38. package/src/integration.ts +120 -0
  39. package/src/load-translations.ts +130 -0
  40. package/src/middleware.ts +203 -0
  41. package/src/router/adapter.ts +184 -0
  42. package/src/router/types.ts +66 -0
  43. package/src/routing.ts +108 -0
  44. package/src/utils.ts +401 -0
  45. package/dist/bridge/astro-bridge.d.ts +0 -13
  46. package/dist/index-C-UMdqSG.cjs +0 -1
  47. package/dist/index-CVhedN6W.js +0 -146
  48. package/dist/toolbar-app.d.ts +0 -2
@@ -0,0 +1,23 @@
1
+ import { Translations, Params } from '@i18n-micro/types';
2
+ /**
3
+ * Состояние i18n для клиентских островов
4
+ */
5
+ export interface I18nState {
6
+ locale: string;
7
+ fallbackLocale: string;
8
+ translations: Record<string, Translations>;
9
+ currentRoute: string;
10
+ }
11
+ /**
12
+ * Чистая функция для получения перевода из состояния
13
+ *
14
+ * Note: This is a simplified version optimized for client-side islands.
15
+ * It supports basic translation lookup, interpolation, and fallback to general translations.
16
+ * Returns CleanTranslation which can be string, number, boolean, object, or null.
17
+ * For advanced features like Linked Messages (@:path.to.key), use the server-side i18n instance.
18
+ */
19
+ export declare function translate(state: I18nState, key: string, params?: Params, defaultValue?: string | null, routeName?: string): string | number | boolean | Translations | null;
20
+ /**
21
+ * Проверяет наличие перевода в состоянии
22
+ */
23
+ export declare function hasTranslation(state: I18nState, key: string, routeName?: string): boolean;
@@ -0,0 +1,6 @@
1
+ export { translate, hasTranslation } from './core';
2
+ export type { I18nState } from './core';
3
+ export { provideI18n, useAstroI18n as useAstroI18nVue } from './vue';
4
+ export { I18nProvider, useAstroI18n as useAstroI18nReact } from './react';
5
+ export { I18nProvider as I18nProviderPreact, useAstroI18n as useAstroI18nPreact } from './preact';
6
+ export { createI18nStore, useAstroI18n as useAstroI18nSvelte } from './svelte';
@@ -0,0 +1,17 @@
1
+ import { h as s, t as o } from "../core-D32Y48CN.js";
2
+ import { provideI18n as a, useAstroI18n as n } from "./vue.js";
3
+ import { I18nProvider as u, useAstroI18n as A } from "./react.js";
4
+ import { I18nProvider as f, useAstroI18n as i } from "./preact.js";
5
+ import { createI18nStore as v, useAstroI18n as x } from "./svelte.js";
6
+ export {
7
+ u as I18nProvider,
8
+ f as I18nProviderPreact,
9
+ v as createI18nStore,
10
+ s as hasTranslation,
11
+ a as provideI18n,
12
+ o as translate,
13
+ i as useAstroI18nPreact,
14
+ A as useAstroI18nReact,
15
+ x as useAstroI18nSvelte,
16
+ n as useAstroI18nVue
17
+ };
@@ -0,0 +1,30 @@
1
+ import { ComponentChildren } from 'preact';
2
+ import { I18nClientProps } from '../utils';
3
+ import { I18nState } from './core';
4
+ import { Params, TranslationKey, CleanTranslation } from '@i18n-micro/types';
5
+ /**
6
+ * Провайдер для i18n в Preact островах
7
+ */
8
+ export declare const I18nProvider: ({ children, value }: {
9
+ children: ComponentChildren;
10
+ value: I18nClientProps;
11
+ }) => import('preact').VNode<{
12
+ value: I18nState | null;
13
+ children?: ComponentChildren;
14
+ }>;
15
+ /**
16
+ * Хук для использования i18n в Preact компонентах
17
+ */
18
+ export declare function useAstroI18n(): {
19
+ t: (key: TranslationKey, params?: Params, defaultValue?: string | null, routeName?: string) => CleanTranslation;
20
+ ts: (key: TranslationKey, params?: Params, defaultValue?: string, routeName?: string) => string;
21
+ tc: (key: TranslationKey, count: number | Params, defaultValue?: string) => string;
22
+ tn: (value: number, options?: Intl.NumberFormatOptions) => string;
23
+ td: (value: Date | number | string, options?: Intl.DateTimeFormatOptions) => string;
24
+ tdr: (value: Date | number | string, options?: Intl.RelativeTimeFormatOptions) => string;
25
+ has: (key: TranslationKey, routeName?: string) => boolean;
26
+ locale: string;
27
+ fallbackLocale: string;
28
+ currentRoute: string;
29
+ getRoute: () => string;
30
+ };
@@ -0,0 +1,51 @@
1
+ import { createContext as g, createElement as w } from "preact";
2
+ import { useState as x, useContext as L, useMemo as a } from "preact/hooks";
3
+ import { t as P, h as S } from "../core-D32Y48CN.js";
4
+ import { FormatService as C, defaultPlural as A } from "@i18n-micro/core";
5
+ const s = new C(), i = g(null), F = ({ children: t, value: o }) => {
6
+ const [l] = x(() => ({
7
+ locale: o.locale,
8
+ fallbackLocale: o.fallbackLocale,
9
+ translations: o.translations,
10
+ currentRoute: o.currentRoute
11
+ }));
12
+ return w(i.Provider, { value: l }, t);
13
+ };
14
+ function M() {
15
+ const t = L(i);
16
+ if (!t)
17
+ throw new Error("useAstroI18n must be used within an I18nProvider");
18
+ const o = a(() => (r, e, n, c) => P(t, r, e, n, c), [t]), l = a(() => (r, e, n, c) => o(r, e, n, c)?.toString() ?? n ?? r, [o]), m = a(() => (r, e, n) => {
19
+ const { count: c, ...u } = typeof e == "number" ? { count: e } : e;
20
+ if (c === void 0)
21
+ return n ?? r;
22
+ const d = (v, I, h) => o(v, I, h);
23
+ return A(
24
+ r,
25
+ Number.parseInt(c.toString(), 10),
26
+ u,
27
+ t.locale,
28
+ d
29
+ ) ?? n ?? r;
30
+ }, [o, t]), f = a(() => (r, e) => s.formatNumber(r, t.locale, e), [t.locale]), b = a(() => (r, e) => s.formatDate(r, t.locale, e), [t.locale]), p = a(() => (r, e) => s.formatRelativeTime(r, t.locale, e), [t.locale]), R = a(() => (r, e) => S(t, r, e), [t]);
31
+ return {
32
+ // Translation methods
33
+ t: o,
34
+ ts: l,
35
+ tc: m,
36
+ tn: f,
37
+ td: b,
38
+ tdr: p,
39
+ has: R,
40
+ // Locale state
41
+ locale: t.locale,
42
+ fallbackLocale: t.fallbackLocale,
43
+ currentRoute: t.currentRoute,
44
+ // Route management (read-only в клиентских островах)
45
+ getRoute: () => t.currentRoute
46
+ };
47
+ }
48
+ export {
49
+ F as I18nProvider,
50
+ M as useAstroI18n
51
+ };
@@ -0,0 +1,26 @@
1
+ import { default as React } from 'react';
2
+ import { I18nClientProps } from '../utils';
3
+ import { Params, TranslationKey, CleanTranslation } from '@i18n-micro/types';
4
+ /**
5
+ * Провайдер для i18n в React островах
6
+ */
7
+ export declare function I18nProvider({ children, value }: {
8
+ children: React.ReactNode;
9
+ value: I18nClientProps;
10
+ }): React.ReactElement;
11
+ /**
12
+ * Хук для использования i18n в React компонентах
13
+ */
14
+ export declare function useAstroI18n(): {
15
+ t: (key: TranslationKey, params?: Params, defaultValue?: string | null, routeName?: string) => CleanTranslation;
16
+ ts: (key: TranslationKey, params?: Params, defaultValue?: string, routeName?: string) => string;
17
+ tc: (key: TranslationKey, count: number | Params, defaultValue?: string) => string;
18
+ tn: (value: number, options?: Intl.NumberFormatOptions) => string;
19
+ td: (value: Date | number | string, options?: Intl.DateTimeFormatOptions) => string;
20
+ tdr: (value: Date | number | string, options?: Intl.RelativeTimeFormatOptions) => string;
21
+ has: (key: TranslationKey, routeName?: string) => boolean;
22
+ locale: string;
23
+ fallbackLocale: string;
24
+ currentRoute: string;
25
+ getRoute: () => string;
26
+ };
@@ -0,0 +1,51 @@
1
+ import g, { createContext as w, useState as x, useContext as L, useMemo as a } from "react";
2
+ import { t as P, h as S } from "../core-D32Y48CN.js";
3
+ import { FormatService as C, defaultPlural as A } from "@i18n-micro/core";
4
+ const s = new C(), i = w(null);
5
+ function D({ children: t, value: o }) {
6
+ const [l] = x(() => ({
7
+ locale: o.locale,
8
+ fallbackLocale: o.fallbackLocale,
9
+ translations: o.translations,
10
+ currentRoute: o.currentRoute
11
+ }));
12
+ return g.createElement(i.Provider, { value: l }, t);
13
+ }
14
+ function F() {
15
+ const t = L(i);
16
+ if (!t)
17
+ throw new Error("useAstroI18n must be used within an I18nProvider");
18
+ const o = a(() => (e, r, n, c) => P(t, e, r, n, c), [t]), l = a(() => (e, r, n, c) => o(e, r, n, c)?.toString() ?? n ?? e, [o]), m = a(() => (e, r, n) => {
19
+ const { count: c, ...u } = typeof r == "number" ? { count: r } : r;
20
+ if (c === void 0)
21
+ return n ?? e;
22
+ const d = (v, I, h) => o(v, I, h);
23
+ return A(
24
+ e,
25
+ Number.parseInt(c.toString(), 10),
26
+ u,
27
+ t.locale,
28
+ d
29
+ ) ?? n ?? e;
30
+ }, [o, t]), f = a(() => (e, r) => s.formatNumber(e, t.locale, r), [t.locale]), b = a(() => (e, r) => s.formatDate(e, t.locale, r), [t.locale]), R = a(() => (e, r) => s.formatRelativeTime(e, t.locale, r), [t.locale]), p = a(() => (e, r) => S(t, e, r), [t]);
31
+ return {
32
+ // Translation methods
33
+ t: o,
34
+ ts: l,
35
+ tc: m,
36
+ tn: f,
37
+ td: b,
38
+ tdr: R,
39
+ has: p,
40
+ // Locale state
41
+ locale: t.locale,
42
+ fallbackLocale: t.fallbackLocale,
43
+ currentRoute: t.currentRoute,
44
+ // Route management (read-only в клиентских островах)
45
+ getRoute: () => t.currentRoute
46
+ };
47
+ }
48
+ export {
49
+ D as I18nProvider,
50
+ F as useAstroI18n
51
+ };
@@ -0,0 +1,28 @@
1
+ import { Writable } from 'svelte/store';
2
+ import { I18nClientProps } from '../utils';
3
+ import { I18nState } from './core';
4
+ import { Params, TranslationKey, CleanTranslation } from '@i18n-micro/types';
5
+ /**
6
+ * Создает Svelte store для i18n состояния
7
+ */
8
+ export declare function createI18nStore(props: I18nClientProps): Writable<I18nState>;
9
+ /**
10
+ * Хук для использования i18n в Svelte компонентах
11
+ * Используйте в <script> блоке компонента
12
+ */
13
+ export declare function useAstroI18n(store: Writable<I18nState>): {
14
+ store: Writable<I18nState>;
15
+ t: (key: TranslationKey, params?: Params, defaultValue?: string | null, routeName?: string) => CleanTranslation;
16
+ ts: (key: TranslationKey, params?: Params, defaultValue?: string, routeName?: string) => string;
17
+ tc: (key: TranslationKey, count: number | Params, defaultValue?: string) => string;
18
+ tn: (value: number, options?: Intl.NumberFormatOptions) => string;
19
+ td: (value: Date | number | string, options?: Intl.DateTimeFormatOptions) => string;
20
+ tdr: (value: Date | number | string, options?: Intl.RelativeTimeFormatOptions) => string;
21
+ has: (key: TranslationKey, routeName?: string) => boolean;
22
+ readonly locale: string;
23
+ readonly fallbackLocale: string;
24
+ readonly currentRoute: string;
25
+ setLocale: (locale: string) => void;
26
+ setRoute: (routeName: string) => void;
27
+ getRoute: () => string;
28
+ };
@@ -0,0 +1,70 @@
1
+ import { writable as R, get as b } from "svelte/store";
2
+ import { h as d, t as v } from "../core-D32Y48CN.js";
3
+ import { FormatService as L, defaultPlural as S } from "@i18n-micro/core";
4
+ const l = new L();
5
+ function F(o) {
6
+ return R({
7
+ locale: o.locale,
8
+ fallbackLocale: o.fallbackLocale,
9
+ translations: o.translations,
10
+ currentRoute: o.currentRoute
11
+ });
12
+ }
13
+ function P(o) {
14
+ const n = () => b(o), c = (t, e, r, a) => v(n(), t, e, r, a);
15
+ return {
16
+ // Store для реактивности в шаблонах (используйте $i18nStore в шаблонах)
17
+ store: o,
18
+ // Translation methods
19
+ t: c,
20
+ ts: (t, e, r, a) => c(t, e, r, a)?.toString() ?? r ?? t,
21
+ tc: (t, e, r) => {
22
+ const a = n(), { count: u, ...s } = typeof e == "number" ? { count: e } : e;
23
+ if (u === void 0)
24
+ return r ?? t;
25
+ const i = (m, f, g) => c(m, f, g);
26
+ return S(
27
+ t,
28
+ Number.parseInt(u.toString(), 10),
29
+ s,
30
+ a.locale,
31
+ i
32
+ ) ?? r ?? t;
33
+ },
34
+ tn: (t, e) => {
35
+ const r = n();
36
+ return l.formatNumber(t, r.locale, e);
37
+ },
38
+ td: (t, e) => {
39
+ const r = n();
40
+ return l.formatDate(t, r.locale, e);
41
+ },
42
+ tdr: (t, e) => {
43
+ const r = n();
44
+ return l.formatRelativeTime(t, r.locale, e);
45
+ },
46
+ has: (t, e) => d(n(), t, e),
47
+ // Геттеры для текущего состояния (для использования в скриптах)
48
+ get locale() {
49
+ return n().locale;
50
+ },
51
+ get fallbackLocale() {
52
+ return n().fallbackLocale;
53
+ },
54
+ get currentRoute() {
55
+ return n().currentRoute;
56
+ },
57
+ // Route management
58
+ setLocale: (t) => {
59
+ o.update((e) => ({ ...e, locale: t }));
60
+ },
61
+ setRoute: (t) => {
62
+ o.update((e) => ({ ...e, currentRoute: t }));
63
+ },
64
+ getRoute: () => n().currentRoute
65
+ };
66
+ }
67
+ export {
68
+ F as createI18nStore,
69
+ P as useAstroI18n
70
+ };
@@ -0,0 +1,27 @@
1
+ import { Ref } from 'vue';
2
+ import { I18nClientProps } from '../utils';
3
+ import { I18nState } from './core';
4
+ import { Params, TranslationKey, CleanTranslation } from '@i18n-micro/types';
5
+ /**
6
+ * Инициализирует i18n провайдер для Vue острова
7
+ * Вызывайте в корневом компоненте острова
8
+ */
9
+ export declare function provideI18n(props: I18nClientProps): Ref<I18nState>;
10
+ /**
11
+ * Хук для использования i18n в Vue компонентах
12
+ */
13
+ export declare function useAstroI18n(): {
14
+ t: (key: TranslationKey, params?: Params, defaultValue?: string | null, routeName?: string) => CleanTranslation;
15
+ ts: (key: TranslationKey, params?: Params, defaultValue?: string, routeName?: string) => string;
16
+ tc: (key: TranslationKey, count: number | Params, defaultValue?: string) => string;
17
+ tn: (value: number, options?: Intl.NumberFormatOptions) => string;
18
+ td: (value: Date | number | string, options?: Intl.DateTimeFormatOptions) => string;
19
+ tdr: (value: Date | number | string, options?: Intl.RelativeTimeFormatOptions) => string;
20
+ has: (key: TranslationKey, routeName?: string) => boolean;
21
+ locale: import('vue').WritableComputedRef<string, string>;
22
+ fallbackLocale: import('vue').ComputedRef<string>;
23
+ currentRoute: import('vue').WritableComputedRef<string, string>;
24
+ setLocale: (locale: string) => void;
25
+ setRoute: (routeName: string) => void;
26
+ getRoute: () => string;
27
+ };