@i18n-micro/solid 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.
@@ -0,0 +1,72 @@
1
+ import type { Locale } from '@i18n-micro/types'
2
+ import type { Accessor } from 'solid-js'
3
+ import type { SolidI18n } from './i18n'
4
+ import { type I18nContextValue, useI18nContextValue } from './injection'
5
+ import type { RouteTarget } from './router/types'
6
+
7
+ export interface UseI18nOptions {
8
+ locales?: Locale[]
9
+ defaultLocale?: string
10
+ }
11
+
12
+ export const routePath = (target: RouteTarget): string => (typeof target === 'string' ? target : target.path || '/')
13
+
14
+ function createApi(context: I18nContextValue, options?: UseI18nOptions) {
15
+ const i18n = context.i18n
16
+ const locales = (): Locale[] => options?.locales ?? context.locales ?? []
17
+ const localeRoute = (to: RouteTarget, code?: string): RouteTarget => {
18
+ const resolve = context.router?.resolvePath
19
+ return resolve ? resolve(to, code || i18n.locale) : to
20
+ }
21
+
22
+ return {
23
+ // Direct access to instance
24
+ instance: i18n as SolidI18n,
25
+
26
+ // An accessor, the Solid way: `locale()` in JSX stays reactive, a plain value would not.
27
+ locale: i18n.localeAccessor as Accessor<string>,
28
+
29
+ getLocales: locales,
30
+ defaultLocale: (): string => options?.defaultLocale ?? context.defaultLocale ?? i18n.fallbackLocale,
31
+ getLocaleName: (): string | null => locales().find((l) => l.code === i18n.locale)?.displayName || null,
32
+
33
+ // Routing helpers: they read the reactive locale, so an effect or memo follows it
34
+ localeRoute,
35
+ localePath: (to: RouteTarget, locale?: string): string => routePath(localeRoute(to, locale)),
36
+ switchLocale: (newLocale: string): void => {
37
+ i18n.locale = newLocale
38
+ const router = context.router
39
+ if (router) router.push({ path: routePath(localeRoute(router.getCurrentPath(), newLocale)) })
40
+ },
41
+
42
+ // Translation methods (delegate to instance)
43
+ t: i18n.t.bind(i18n),
44
+ ts: i18n.ts.bind(i18n),
45
+ tc: i18n.tc.bind(i18n),
46
+ tn: i18n.tn.bind(i18n),
47
+ td: i18n.td.bind(i18n),
48
+ tdr: i18n.tdr.bind(i18n),
49
+ has: i18n.has.bind(i18n),
50
+
51
+ // Route management
52
+ setRoute: i18n.setRoute.bind(i18n),
53
+ getRoute: (): string => i18n.currentRoute,
54
+ getLocale: (): string => i18n.locale,
55
+
56
+ // Translation management
57
+ addTranslations: i18n.addTranslations.bind(i18n),
58
+ addRouteTranslations: i18n.addRouteTranslations.bind(i18n),
59
+ resolveTranslations: i18n.resolveTranslations.bind(i18n),
60
+ setTranslation: i18n.setTranslation.bind(i18n),
61
+ clearCache: i18n.clearCache.bind(i18n),
62
+ }
63
+ }
64
+
65
+ export type UseI18nReturn = ReturnType<typeof createApi>
66
+
67
+ /** One object per provider; hook options make their own, and win over the provider. */
68
+ export function useI18n(options?: UseI18nOptions): UseI18nReturn {
69
+ const context = useI18nContextValue()
70
+ if (options?.locales || options?.defaultLocale) return createApi(context, options)
71
+ return (context.api ??= createApi(context))
72
+ }