@i18n-micro/solid 1.2.15 → 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,107 +1,109 @@
1
1
  import { Accessor } from 'solid-js';
2
- import { BaseI18n } from '@i18n-micro/core';
3
2
  import { CleanTranslation } from '@i18n-micro/types';
4
3
  import { Component } from 'solid-js';
5
4
  import { Context } from 'solid-js';
6
5
  import { defaultPlural } from '@i18n-micro/core';
7
6
  import { FormatService } from '@i18n-micro/core';
8
7
  import { Getter } from '@i18n-micro/types';
8
+ import { I18n } from '@i18n-micro/runtime';
9
+ import { I18nOptions } from '@i18n-micro/runtime';
9
10
  import { interpolate } from '@i18n-micro/core';
10
11
  import { JSX } from 'solid-js';
11
12
  import { Locale } from '@i18n-micro/types';
12
13
  import { LocaleCode } from '@i18n-micro/types';
13
- import { Location as Location_2 } from '@solidjs/router';
14
14
  import { ModuleOptions } from '@i18n-micro/types';
15
15
  import { Params } from '@i18n-micro/types';
16
16
  import { ParentComponent } from 'solid-js';
17
17
  import { PluralFunc } from '@i18n-micro/types';
18
+ import { ResolveTextOptions } from '@i18n-micro/core';
18
19
  import { TranslationKey } from '@i18n-micro/types';
19
20
  import { Translations } from '@i18n-micro/types';
20
- import { TranslationStorage } from '@i18n-micro/core';
21
- import { useNavigate } from '@solidjs/router';
22
21
 
23
22
  export { CleanTranslation }
24
23
 
25
- export declare function createI18n(options: SolidI18nOptions): SolidI18n;
26
-
27
- /**
28
- * Factory for Solid Router adapter
29
- * Implements routing utilities for Solid Router
30
- * Uses @solidjs/router APIs for navigation and path resolution
31
- *
32
- * **IMPORTANT**: This function must be called inside a SolidJS component or reactive context
33
- * (e.g., inside a component function, not at module level). This is required because
34
- * `createSignal` and `createEffect` need to be within a reactive owner context.
35
- *
36
- * @example
37
- * ```tsx
38
- * const RouterRoot: Component = () => {
39
- * const navigate = useNavigate()
40
- * const location = useLocation()
41
- * const routingStrategy = createSolidRouterAdapter(
42
- * locales,
43
- * defaultLocale,
44
- * navigate,
45
- * location,
46
- * )
47
- * // ...
48
- * }
49
- * ```
50
- *
51
- * @param locales - Array of locale configurations
52
- * @param defaultLocale - Default locale code
53
- * @param navigate - Navigation function from @solidjs/router's useNavigate hook
54
- * @param location - Location object from @solidjs/router's useLocation hook
55
- * @returns I18nRoutingStrategy with reactive pathname accessor
56
- */
57
- export declare function createSolidRouterAdapter(locales: Locale[], defaultLocale: string, navigate: NavigateFunction, location: Location_2): I18nRoutingStrategy & {
58
- getCurrentPathAccessor: Accessor<string>;
24
+ declare function createApi(context: I18nContextValue, options?: UseI18nOptions): {
25
+ instance: SolidI18n;
26
+ locale: Accessor<string>;
27
+ getLocales: () => Locale[];
28
+ defaultLocale: () => string;
29
+ getLocaleName: () => string | null;
30
+ localeRoute: (to: RouteTarget, code?: string) => RouteTarget;
31
+ localePath: (to: RouteTarget, locale?: string) => string;
32
+ switchLocale: (newLocale: string) => void;
33
+ t: (key: TranslationKey, params?: Params, defaultValue?: string | null, routeContext?: unknown) => CleanTranslation;
34
+ ts: (key: TranslationKey, params?: Params, defaultValue?: string, routeContext?: unknown) => string;
35
+ tc: (key: TranslationKey, count: number | Params, defaultValue?: string) => string;
36
+ tn: {
37
+ (value: number, options?: Intl.NumberFormatOptions): string;
38
+ (value: number, key: string, overrides?: Intl.NumberFormatOptions): string;
39
+ (value: number, key: string, locale: string, overrides?: Intl.NumberFormatOptions): string;
40
+ };
41
+ td: {
42
+ (value: Date | number | string, options?: Intl.DateTimeFormatOptions): string;
43
+ (value: Date | number | string, key: string, overrides?: Intl.DateTimeFormatOptions): string;
44
+ (value: Date | number | string, key: string, locale: string, overrides?: Intl.DateTimeFormatOptions): string;
45
+ };
46
+ tdr: (value: Date | number | string, options?: Intl.RelativeTimeFormatOptions) => string;
47
+ has: (key: TranslationKey, routeContext?: unknown) => boolean;
48
+ setRoute: (routeName: string) => void;
49
+ getRoute: () => string;
50
+ getLocale: () => string;
51
+ addTranslations: (locale: string, translations: Translations, merge?: boolean) => void;
52
+ addRouteTranslations: (locale: string, routeName: string, translations: Translations, merge?: boolean) => void;
53
+ resolveTranslations: (routeContext?: unknown) => Translations;
54
+ setTranslation: (key: TranslationKey, value: unknown) => void;
55
+ clearCache: () => void;
59
56
  };
60
57
 
58
+ export declare function createI18n(options: SolidI18nOptions): SolidI18n;
59
+
61
60
  export { defaultPlural }
62
61
 
63
62
  export { FormatService }
64
63
 
65
64
  export { Getter }
66
65
 
67
- export declare const I18nContext: Context<SolidI18n | undefined>;
66
+ export declare const I18nContext: Context<I18nContextValue | undefined>;
68
67
 
69
- export declare const I18nDefaultLocaleContext: Context<string | undefined>;
68
+ /** Everything a provider hands down: getters over its props, so they stay reactive. */
69
+ export declare interface I18nContextValue {
70
+ readonly i18n: SolidI18n;
71
+ readonly locales: Locale[] | undefined;
72
+ readonly defaultLocale: string | undefined;
73
+ readonly router: I18nRoutingStrategy | undefined;
74
+ /** The `useI18n()` object: one per provider, signals do the rest. */
75
+ api?: UseI18nReturn;
76
+ }
70
77
 
71
78
  export declare const I18nGroup: Component<I18nGroupProps>;
72
79
 
73
- declare interface I18nGroupProps extends JSX.HTMLAttributes<HTMLDivElement> {
80
+ export declare interface I18nGroupProps extends Omit<JSX.HTMLAttributes<HTMLDivElement>, 'children'> {
74
81
  prefix: string;
75
82
  groupClass?: string;
83
+ children?: JSX.Element | ((props: {
84
+ prefix: string;
85
+ t: (key: string, params?: Record<string, string | number | boolean>) => string;
86
+ }) => JSX.Element);
76
87
  }
77
88
 
78
89
  export declare const I18nLink: Component<I18nLinkProps>;
79
90
 
80
- declare interface I18nLinkProps extends JSX.AnchorHTMLAttributes<HTMLAnchorElement> {
81
- to: string | {
82
- path?: string;
83
- };
84
- localeRoute?: (to: string | {
85
- path?: string;
86
- }, locale?: string) => string | {
87
- path?: string;
88
- };
91
+ export declare interface I18nLinkProps extends JSX.AnchorHTMLAttributes<HTMLAnchorElement> {
92
+ to: RouteTarget;
93
+ localeRoute?: (to: RouteTarget, locale?: string) => RouteTarget;
89
94
  activeStyle?: JSX.CSSProperties;
90
95
  }
91
96
 
92
- export declare const I18nLocalesContext: Context<Locale[] | undefined>;
93
-
94
97
  export declare const I18nProvider: ParentComponent<I18nProviderProps>;
95
98
 
96
99
  export declare interface I18nProviderProps {
97
- i18n: SolidI18n;
100
+ /** Without it, the outer provider's instance. */
101
+ i18n?: SolidI18n;
98
102
  locales?: Locale[];
99
103
  defaultLocale?: string;
100
104
  routingStrategy?: I18nRoutingStrategy;
101
105
  }
102
106
 
103
- export declare const I18nRouterContext: Context<I18nRoutingStrategy | undefined>;
104
-
105
107
  /**
106
108
  * Routing strategy interface for i18n
107
109
  * Implement this interface to integrate with any router library
@@ -109,32 +111,17 @@ export declare const I18nRouterContext: Context<I18nRoutingStrategy | undefined>
109
111
  */
110
112
  export declare interface I18nRoutingStrategy {
111
113
  /**
112
- * Returns current path (without locale prefix if needed, or full path)
113
- * Used for determining active classes in links
114
- * Note: For SolidJS, this should return the current value synchronously.
115
- * For reactive updates, use `getCurrentPathAccessor` instead.
114
+ * Returns the current path with its query and hash, so a locale switch keeps them. Links compare
115
+ * it by page. Read a signal or a reactive location here and links and the switcher follow it.
116
116
  */
117
117
  getCurrentPath: () => string;
118
- /**
119
- * (SolidJS-specific, highly recommended) Returns reactive accessor for current path
120
- * This is REQUIRED for proper reactivity in SolidJS components.
121
- * Without this, active link states and path-dependent UI won't update reactively.
122
- *
123
- * @example
124
- * ```tsx
125
- * const pathnameAccessor = router.getCurrentPathAccessor?.()
126
- * // Use inside createMemo for reactivity:
127
- * const currentPath = createMemo(() => pathnameAccessor?.() ?? router.getCurrentPath())
128
- * ```
129
- */
130
- getCurrentPathAccessor?: () => Accessor<string>;
131
118
  /**
132
119
  * Component to use for rendering links (e.g., RouterLink)
133
120
  */
134
121
  linkComponent?: string | Component<{
135
122
  href: string;
136
123
  children?: JSX.Element;
137
- style?: JSX.CSSProperties;
124
+ style?: JSX.CSSProperties | string;
138
125
  class?: string;
139
126
  [key: string]: unknown;
140
127
  }>;
@@ -153,11 +140,7 @@ export declare interface I18nRoutingStrategy {
153
140
  /**
154
141
  * Generate path for specific locale
155
142
  */
156
- resolvePath?: (to: string | {
157
- path?: string;
158
- }, locale: string) => string | {
159
- path?: string;
160
- };
143
+ resolvePath?: (to: RouteTarget, locale: string) => RouteTarget;
161
144
  /**
162
145
  * (Optional) Get current route object for SEO/Meta tags
163
146
  */
@@ -169,16 +152,12 @@ export declare interface I18nRoutingStrategy {
169
152
 
170
153
  export declare const I18nSwitcher: Component<I18nSwitcherProps>;
171
154
 
172
- declare interface I18nSwitcherProps extends JSX.HTMLAttributes<HTMLDivElement> {
155
+ export declare interface I18nSwitcherProps extends JSX.HTMLAttributes<HTMLDivElement> {
173
156
  locales?: Locale[];
174
157
  currentLocale?: string | (() => string);
175
158
  getLocaleName?: () => string | null;
176
159
  switchLocale?: (locale: string) => void;
177
- localeRoute?: (to: string | {
178
- path?: string;
179
- }, locale?: string) => string | {
180
- path?: string;
181
- };
160
+ localeRoute?: (to: RouteTarget, locale?: string) => RouteTarget;
182
161
  customLabels?: Record<string, string>;
183
162
  customWrapperStyle?: JSX.CSSProperties;
184
163
  customButtonStyle?: JSX.CSSProperties;
@@ -198,18 +177,17 @@ declare interface I18nSwitcherProps extends JSX.HTMLAttributes<HTMLDivElement> {
198
177
 
199
178
  export declare const I18nT: Component<I18nTProps>;
200
179
 
201
- declare interface I18nTProps extends JSX.HTMLAttributes<HTMLElement> {
202
- keypath: TranslationKey;
203
- params?: Record<string, string | number | boolean>;
180
+ /** What fills a `{name}` placeholder. A function may run per occurrence; one node can only stand in one place. */
181
+ export declare type I18nTComponent = JSX.Element | (() => JSX.Element);
182
+
183
+ export declare interface I18nTProps extends Omit<JSX.HTMLAttributes<HTMLElement>, 'children'>, ResolveTextOptions {
204
184
  tag?: string;
205
- plural?: number | string;
206
- defaultValue?: string;
207
- html?: boolean;
208
- hideIfEmpty?: boolean;
209
- customPluralRule?: PluralFunc;
210
- number?: number | string;
211
- date?: Date | string | number;
212
- relativeDate?: Date | string | number;
185
+ /** Elements for `{name}` placeholders. Beats a param of the same name. */
186
+ components?: Record<string, I18nTComponent>;
187
+ /** Replaces the text. As a function it gets the translation, like Vue's default slot. */
188
+ children?: JSX.Element | ((props: {
189
+ translation: string;
190
+ }) => JSX.Element);
213
191
  }
214
192
 
215
193
  export { interpolate }
@@ -220,105 +198,59 @@ export { LocaleCode }
220
198
 
221
199
  export { ModuleOptions }
222
200
 
223
- declare type NavigateFunction = ReturnType<typeof useNavigate>;
224
-
225
201
  export { Params }
226
202
 
227
203
  export { PluralFunc }
228
204
 
229
- export declare class SolidI18n extends BaseI18n {
230
- private readonly store;
231
- private readonly _localeSignal;
232
- private readonly _setLocaleSignal;
233
- private readonly _routeSignal;
234
- private readonly _setRouteSignal;
235
- private readonly _fallbackSignal;
236
- readonly storage: TranslationStorage;
205
+ /** Where a link or a switch goes. */
206
+ export declare type RouteTarget = string | {
207
+ path?: string;
208
+ };
209
+
210
+ /**
211
+ * The runtime instance with signals over its store. A translation read tracks the revision, which
212
+ * every change bumps. Reading the locale tracks the locale alone, so `createEffect(() => load(locale()))`
213
+ * does not wake itself. `getLocale()` and `getRoute()` stay plain, as a write reads them too: track
214
+ * with `locale` and `currentRoute`.
215
+ */
216
+ export declare class SolidI18n extends I18n {
217
+ private readonly revision;
218
+ private readonly localeSignal;
219
+ private readonly fallbackSignal;
220
+ private readonly routeSignal;
221
+ readonly localeAccessor: Accessor<string>;
222
+ readonly routeAccessor: Accessor<string>;
237
223
  constructor(options: SolidI18nOptions);
238
- getLocale(): string;
239
- getFallbackLocale(): string;
240
- getRoute(): string;
241
- set locale(val: string);
224
+ protected touch(): void;
242
225
  get locale(): string;
243
- get localeAccessor(): Accessor<string>;
226
+ set locale(value: string);
244
227
  get fallbackLocale(): string;
228
+ set fallbackLocale(value: string);
245
229
  get currentRoute(): string;
246
- get routeAccessor(): Accessor<string>;
247
- setRoute(val: string): void;
248
- addTranslations(locale: string, translations: Translations, merge?: boolean): void;
249
- addRouteTranslations(locale: string, routeName: string, translations: Translations, merge?: boolean): void;
250
- clearCache(): void;
251
- protected onTranslationsChanged(): void;
252
- private listeners;
253
- subscribe(cb: () => void): () => void;
254
- getSnapshot(): string;
255
- private notifyListeners;
256
230
  }
257
231
 
258
- export declare interface SolidI18nOptions {
259
- locale: string;
260
- fallbackLocale?: string;
261
- messages?: Record<string, Translations>;
262
- plural?: PluralFunc;
263
- missingWarn?: boolean;
264
- missingHandler?: (locale: string, key: string, routeName: string) => void;
265
- }
232
+ export declare type SolidI18nOptions = I18nOptions;
266
233
 
267
234
  export { TranslationKey }
268
235
 
269
236
  export { Translations }
270
237
 
271
- export declare function useI18n(options?: UseI18nOptions): {
272
- instance: SolidI18n;
273
- locale: Accessor<string>;
274
- getLocales: () => Locale[];
275
- defaultLocale: () => string;
276
- getLocaleName: () => string | null;
277
- localeRoute: (to: string | {
278
- path?: string;
279
- }, localeCode?: string) => string | {
280
- path?: string;
281
- };
282
- localePath: (to: string | {
283
- path?: string;
284
- }, locale?: string) => string;
285
- switchLocale: (newLocale: string) => void;
286
- t: (key: TranslationKey, params?: Params, defaultValue?: string | null, routeContext?: unknown) => CleanTranslation;
287
- ts: (key: TranslationKey, params?: Params, defaultValue?: string, routeContext?: unknown) => string;
288
- tc: (key: TranslationKey, count: number | Params, defaultValue?: string) => string;
289
- tn: {
290
- (value: number, options?: Intl.NumberFormatOptions): string;
291
- (value: number, key: string, overrides?: Intl.NumberFormatOptions): string;
292
- (value: number, key: string, locale: string, overrides?: Intl.NumberFormatOptions): string;
293
- };
294
- td: {
295
- (value: Date | number | string, options?: Intl.DateTimeFormatOptions): string;
296
- (value: Date | number | string, key: string, overrides?: Intl.DateTimeFormatOptions): string;
297
- (value: Date | number | string, key: string, locale: string, overrides?: Intl.DateTimeFormatOptions): string;
298
- };
299
- tdr: (value: Date | number | string, options?: Intl.RelativeTimeFormatOptions) => string;
300
- has: (key: TranslationKey, routeContext?: unknown) => boolean;
301
- setRoute: (val: string) => void;
302
- getRoute: () => string;
303
- getLocale: () => string;
304
- addTranslations: (locale: string, translations: Translations, merge?: boolean) => void;
305
- addRouteTranslations: (locale: string, routeName: string, translations: Translations, merge?: boolean) => void;
306
- resolveTranslations: (routeContext?: unknown) => Translations;
307
- setTranslation: (key: TranslationKey, value: unknown) => void;
308
- clearCache: () => void;
309
- };
238
+ /** One object per provider; hook options make their own, and win over the provider. */
239
+ export declare function useI18n(options?: UseI18nOptions): UseI18nReturn;
310
240
 
311
241
  export declare const useI18nContext: () => SolidI18n;
312
242
 
313
- export declare const useI18nDefaultLocale: () => string;
243
+ export declare const useI18nDefaultLocale: () => string | undefined;
314
244
 
315
- export declare const useI18nLocales: () => Locale[];
245
+ export declare const useI18nLocales: () => Locale[] | undefined;
316
246
 
317
247
  export declare interface UseI18nOptions {
318
248
  locales?: Locale[];
319
249
  defaultLocale?: string;
320
250
  }
321
251
 
252
+ export declare type UseI18nReturn = ReturnType<typeof createApi>;
253
+
322
254
  export declare const useI18nRouter: () => I18nRoutingStrategy | undefined;
323
255
 
324
256
  export { }