@i18n-micro/preact 1.2.16 → 2.0.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/index.d.cts CHANGED
@@ -1,15 +1,15 @@
1
1
  import { AnchorHTMLAttributes } from 'preact';
2
- import { Attributes } from 'preact';
3
2
  import { CleanTranslation } from '@i18n-micro/types';
4
3
  import { ComponentChildren } from 'preact';
4
+ import { ComponentType } from 'preact';
5
5
  import { Context } from 'preact';
6
- import { default as default_2 } from 'react';
6
+ import { createI18n } from '@i18n-micro/runtime';
7
+ import { CSSProperties } from 'preact';
7
8
  import { defaultPlural } from '@i18n-micro/core';
8
9
  import { FormatService } from '@i18n-micro/core';
9
10
  import { Getter } from '@i18n-micro/types';
10
11
  import { HTMLAttributes } from 'preact';
11
- import { I18n } from '@i18n-micro/runtime';
12
- import { I18nOptions } from '@i18n-micro/runtime';
12
+ import { I18nTRenderProps } from '@i18n-micro/core/render';
13
13
  import { interpolate } from '@i18n-micro/core';
14
14
  import { JSX } from 'preact';
15
15
  import { Locale } from '@i18n-micro/types';
@@ -17,21 +17,15 @@ import { LocaleCode } from '@i18n-micro/types';
17
17
  import { ModuleOptions } from '@i18n-micro/types';
18
18
  import { Params } from '@i18n-micro/types';
19
19
  import { PluralFunc } from '@i18n-micro/types';
20
+ import { I18n as PreactI18n } from '@i18n-micro/runtime';
21
+ import { I18nOptions as PreactI18nOptions } from '@i18n-micro/runtime';
20
22
  import { TranslationKey } from '@i18n-micro/types';
21
23
  import { Translations } from '@i18n-micro/types';
22
24
  import { VNode } from 'preact';
23
25
 
24
26
  export { CleanTranslation }
25
27
 
26
- /**
27
- * Создает адаптер для работы с History API (универсальный для Pure Preact)
28
- * Работает с любым роутером, который использует History API (wouter, preact-router, и т.д.)
29
- */
30
- export declare function createBrowserHistoryAdapter(locales: Locale[], defaultLocale: string): I18nRoutingStrategy;
31
-
32
- export declare function createI18n(options: PreactI18nOptions): PreactI18n;
33
-
34
- export declare const createPreactRouterAdapter: typeof createBrowserHistoryAdapter;
28
+ export { createI18n }
35
29
 
36
30
  export { defaultPlural }
37
31
 
@@ -39,52 +33,75 @@ export { FormatService }
39
33
 
40
34
  export { Getter }
41
35
 
42
- export declare const I18nContext: Context<PreactI18n | null>;
36
+ declare type HostAnchorAttrs<E extends EventTarget> = AnchorHTMLAttributes<E>;
37
+
38
+ declare type HostAttrs<E extends EventTarget> = HTMLAttributes<E>;
39
+
40
+ declare type HostComponent<P> = ComponentType<P>;
41
+
42
+ declare type HostElement = VNode<any> | null;
43
+
44
+ declare type HostNode = ComponentChildren;
45
+
46
+ declare type HostStyle = CSSProperties;
47
+
48
+ declare type HostTag = keyof JSX.IntrinsicElements;
43
49
 
44
- export declare const I18nDefaultLocaleContext: Context<string | null>;
50
+ /** What a provider adds to its instance. It changes with its props, an adapter built in render on every render. */
51
+ export declare interface I18nConfig {
52
+ locales: Locale[] | null;
53
+ defaultLocale: string | null;
54
+ router: I18nRoutingStrategy | null;
55
+ /** The `useI18n()` object of the current snapshot, shared by every consumer of this provider. */
56
+ api: {
57
+ snapshot: string;
58
+ value: UseI18nReturn;
59
+ } | null;
60
+ /** Translation methods by snapshot: they outlive the config, so `t` does not change with an adapter. */
61
+ methods: {
62
+ current: TranslationMethods | null;
63
+ };
64
+ }
65
+
66
+ export declare const I18nConfigContext: Context<I18nConfig | null>;
45
67
 
46
- export declare const I18nGroup: (props: I18nGroupProps) => VNode;
68
+ /**
69
+ * The instance alone, stable while the app runs: `useT` and `<I18nT>` read only this, so a new
70
+ * adapter object does not render them.
71
+ */
72
+ export declare const I18nContext: Context<PreactI18n | null>;
47
73
 
48
- export declare interface I18nGroupProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {
74
+ export declare const I18nGroup: (props: I18nGroupProps) => HostElement;
75
+
76
+ export declare interface I18nGroupProps extends Omit<HostAttrs<HTMLDivElement>, 'children'> {
49
77
  prefix: string;
50
78
  groupClass?: string;
51
- children?: (props: {
79
+ children?: HostNode | ((props: {
52
80
  prefix: string;
53
81
  t: (key: string, params?: Record<string, string | number | boolean>) => string;
54
- }) => ComponentChildren;
82
+ }) => HostNode);
55
83
  }
56
84
 
57
- export declare const I18nLink: (props: I18nLinkProps) => VNode;
58
-
59
- export declare interface I18nLinkProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
60
- to: string | {
61
- path?: string;
62
- };
63
- activeStyle?: Record<string, string | number>;
64
- localeRoute?: (to: string | {
65
- path?: string;
66
- }, locale?: string) => string | {
67
- path?: string;
68
- };
69
- }
85
+ export declare const I18nLink: (props: I18nLinkProps) => HostElement;
70
86
 
71
- export declare const I18nLocalesContext: Context<Locale[] | null>;
87
+ export declare type I18nLinkProps = HostAnchorAttrs<HTMLAnchorElement> & {
88
+ to: RouteTarget;
89
+ activeStyle?: HostStyle;
90
+ localeRoute?: (to: RouteTarget, locale?: string) => RouteTarget;
91
+ children?: HostNode;
92
+ };
72
93
 
73
- export declare function I18nProvider({ i18n, locales, defaultLocale, routingStrategy, children }: I18nProviderProps): VNode< Attributes & {
74
- value: PreactI18n | null;
75
- children?: ComponentChildren;
76
- }>;
94
+ export declare function I18nProvider({ i18n, locales, defaultLocale, routingStrategy, children, ...options }: I18nProviderProps): HostElement;
77
95
 
78
- export declare interface I18nProviderProps {
79
- i18n: PreactI18n;
96
+ export declare interface I18nProviderProps extends Partial<PreactI18nOptions> {
97
+ /** An instance you created. Without it, the outer provider's; without that, one built from the options next to it: plain data, so a server component can pass them. */
98
+ i18n?: PreactI18n;
80
99
  locales?: Locale[];
81
100
  defaultLocale?: string;
82
101
  routingStrategy?: I18nRoutingStrategy;
83
- children?: ComponentChildren;
102
+ children?: HostNode;
84
103
  }
85
104
 
86
- export declare const I18nRouterContext: Context<I18nRoutingStrategy | null>;
87
-
88
105
  /**
89
106
  * Routing strategy interface for i18n
90
107
  * Implement this interface to integrate with any router library
@@ -92,17 +109,17 @@ export declare const I18nRouterContext: Context<I18nRoutingStrategy | null>;
92
109
  */
93
110
  export declare interface I18nRoutingStrategy {
94
111
  /**
95
- * Returns current path (without locale prefix if needed, or full path)
96
- * Used for determining active classes in links
112
+ * Returns the current path with its query and hash, so a locale switch keeps them.
113
+ * Links compare it by page: query and hash do not count.
97
114
  */
98
115
  getCurrentPath: () => string;
99
116
  /**
100
117
  * Component to use for rendering links (e.g., RouterLink)
101
118
  */
102
- linkComponent?: string | default_2.ComponentType<{
119
+ linkComponent?: string | HostComponent<{
103
120
  href: string;
104
- children?: default_2.ReactNode;
105
- style?: default_2.CSSProperties;
121
+ children?: HostNode;
122
+ style?: HostStyle;
106
123
  className?: string;
107
124
  [key: string]: unknown;
108
125
  }>;
@@ -121,11 +138,7 @@ export declare interface I18nRoutingStrategy {
121
138
  /**
122
139
  * Generate path for specific locale
123
140
  */
124
- resolvePath?: (to: string | {
125
- path?: string;
126
- }, locale: string) => string | {
127
- path?: string;
128
- };
141
+ resolvePath?: (to: RouteTarget, locale: string) => RouteTarget;
129
142
  /**
130
143
  * (Optional) Get current route object for SEO/Meta tags
131
144
  */
@@ -135,50 +148,45 @@ export declare interface I18nRoutingStrategy {
135
148
  };
136
149
  }
137
150
 
138
- export declare const I18nSwitcher: (props: I18nSwitcherProps) => VNode;
151
+ export declare const I18nSwitcher: (props: I18nSwitcherProps) => HostElement;
139
152
 
140
- export declare interface I18nSwitcherProps extends HTMLAttributes<HTMLDivElement> {
153
+ export declare interface I18nSwitcherProps extends HostAttrs<HTMLDivElement> {
141
154
  locales?: Locale[];
142
155
  currentLocale?: string | (() => string);
143
156
  getLocaleName?: () => string | null;
144
157
  switchLocale?: (locale: string) => void;
145
- localeRoute?: (to: string | {
146
- path?: string;
147
- }, locale?: string) => string | {
148
- path?: string;
149
- };
158
+ localeRoute?: (to: RouteTarget, locale?: string) => RouteTarget;
150
159
  customLabels?: Record<string, string>;
151
- customWrapperStyle?: Record<string, string | number>;
152
- customButtonStyle?: Record<string, string | number>;
153
- customDropdownStyle?: Record<string, string | number>;
154
- customItemStyle?: Record<string, string | number>;
155
- customLinkStyle?: Record<string, string | number>;
156
- customActiveLinkStyle?: Record<string, string | number>;
160
+ customWrapperStyle?: HostStyle;
161
+ customButtonStyle?: HostStyle;
162
+ customDropdownStyle?: HostStyle;
163
+ customItemStyle?: HostStyle;
164
+ customLinkStyle?: HostStyle;
165
+ customActiveLinkStyle?: HostStyle;
157
166
  /**
158
167
  * Inline style applied to the main switcher button when the current locale is
159
- * marked as `disabled: true` in locale config.
168
+ * marked as `disabled: true` in the locale config.
160
169
  *
161
- * Disabled locales are omitted from dropdown items.
170
+ * Disabled locales are omitted from the dropdown items, but still shown as the
171
+ * active button label.
162
172
  */
163
- customDisabledLinkStyle?: Record<string, string | number>;
164
- customIconStyle?: Record<string, string | number>;
173
+ customDisabledLinkStyle?: HostStyle;
174
+ customIconStyle?: HostStyle;
165
175
  }
166
176
 
167
- export declare const I18nT: (props: I18nTProps) => VNode | null;
168
-
169
- export declare interface I18nTProps extends Omit<HTMLAttributes<HTMLElement>, 'children'> {
170
- keypath: TranslationKey;
171
- params?: Record<string, string | number | boolean>;
172
- tag?: keyof JSX.IntrinsicElements;
173
- plural?: number | string;
174
- defaultValue?: string;
175
- html?: boolean;
176
- hideIfEmpty?: boolean;
177
- customPluralRule?: PluralFunc;
178
- number?: number | string;
179
- date?: Date | string | number;
180
- relativeDate?: Date | string | number;
181
- children?: never;
177
+ export declare const I18nT: (props: I18nTProps) => HostElement;
178
+
179
+ /** What fills a `{name}` placeholder: a node, or a function returning one (it may use hooks). */
180
+ export declare type I18nTComponent = HostNode | (() => HostNode);
181
+
182
+ export declare interface I18nTProps extends Omit<HostAttrs<HTMLElement>, 'children' | 'defaultValue'>, Omit<I18nTRenderProps, 'tag'> {
183
+ tag?: HostTag;
184
+ /** Elements for `{name}` placeholders. Beats a param of the same name. */
185
+ components?: Record<string, I18nTComponent>;
186
+ /** Replaces the text. As a function it gets the translation, like Vue's default slot. */
187
+ children?: HostNode | ((props: {
188
+ translation: string;
189
+ }) => HostNode);
182
190
  }
183
191
 
184
192
  export { interpolate }
@@ -193,18 +201,22 @@ export { Params }
193
201
 
194
202
  export { PluralFunc }
195
203
 
196
- /**
197
- * Preact-facing i18n instance. Same surface as `@i18n-micro/runtime`, kept as a
198
- * named subclass so `instanceof PreactI18n` and context typing stay stable.
199
- */
200
- export declare class PreactI18n extends I18n {
201
- }
204
+ export { PreactI18n }
202
205
 
203
- export declare interface PreactI18nOptions extends I18nOptions {
204
- }
206
+ export { PreactI18nOptions }
207
+
208
+ /** Where a link or a switch goes. */
209
+ export declare type RouteTarget = string | {
210
+ path?: string;
211
+ };
205
212
 
206
213
  export { TranslationKey }
207
214
 
215
+ declare type TranslationMethods = Pick<UseI18nReturn, 't' | 'ts' | 'tc' | 'tn' | 'td' | 'tdr' | 'has'> & Writers & {
216
+ i18n: PreactI18n;
217
+ snapshot: string;
218
+ };
219
+
208
220
  export { Translations }
209
221
 
210
222
  export declare const useI18n: (options?: UseI18nOptions) => UseI18nReturn;
@@ -237,15 +249,9 @@ export declare interface UseI18nReturn {
237
249
  getLocales: () => Locale[];
238
250
  defaultLocale: () => string;
239
251
  getLocaleName: () => string | null;
240
- switchLocale?: (locale: string) => void;
241
- localeRoute?: (to: string | {
242
- path?: string;
243
- }, locale?: string) => string | {
244
- path?: string;
245
- };
246
- localePath?: (to: string | {
247
- path?: string;
248
- }, locale?: string) => string;
252
+ switchLocale: (locale: string) => void;
253
+ localeRoute: (to: RouteTarget, locale?: string) => RouteTarget;
254
+ localePath: (to: RouteTarget, locale?: string) => string;
249
255
  addTranslations: (locale: string, translations: Translations, merge?: boolean) => void;
250
256
  addRouteTranslations: (locale: string, routeName: string, translations: Translations, merge?: boolean) => void;
251
257
  clearCache: () => void;
@@ -253,17 +259,20 @@ export declare interface UseI18nReturn {
253
259
 
254
260
  export declare const useI18nRouter: () => I18nRoutingStrategy | null;
255
261
 
262
+ /**
263
+ * `setLocale` that loads first. A store write is synchronous for React, even inside
264
+ * `startTransition`, so a reader that suspends on a missing dictionary would show its fallback:
265
+ * this keeps the old UI until the messages are in. The last call wins; a failed `load` rejects
266
+ * and leaves the locale as it was.
267
+ */
268
+ export declare function useLocaleSwitch(load: (locale: string) => unknown): [(locale: string) => Promise<void>, boolean];
269
+
270
+ /**
271
+ * One translation as a value: the component renders only when this string changes, not on every
272
+ * locale or message write, nor when a provider gets a new adapter object.
273
+ */
274
+ export declare function useT(key: TranslationKey, params?: Params, defaultValue?: string, routeName?: string): string;
275
+
276
+ declare type Writers = Pick<UseI18nReturn, 'addTranslations' | 'addRouteTranslations' | 'clearCache'>;
277
+
256
278
  export { }
257
-
258
- // Type definitions for React package
259
-
260
- // Extend Window interface for SSR hydration state
261
- declare global {
262
- interface Window {
263
- __INITIAL_STATE__?: {
264
- locale: string
265
- route: string
266
- // Add other SSR state fields as needed
267
- }
268
- }
269
- }