i18n-typed-store 0.1.0 → 0.1.1

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,296 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import * as react from 'react';
3
+ import { ReactNode, FC } from 'react';
4
+ import { I as II18nTypedStoreContext, T as TranslationStore } from '../context-Dp43aQ0V.mjs';
5
+
6
+ /**
7
+ * React context for I18n typed store.
8
+ * Used internally by hooks to access the translation store.
9
+ */
10
+ declare const I18nTypedStoreContext: react.Context<II18nTypedStoreContext<any, any, any> | null>;
11
+ /**
12
+ * Provider component for I18n typed store.
13
+ * Wraps your application to provide translation store context to child components.
14
+ *
15
+ * @template T - Type of translations object (e.g., { common: 'common', errors: 'errors' })
16
+ * @template L - Type of locales object (e.g., { en: 'en', ru: 'ru' })
17
+ * @template M - Type of translation modules mapping
18
+ *
19
+ * @param props - Provider props
20
+ * @param props.store - Translation store instance
21
+ * @param props.children - React children
22
+ * @param props.suspenseMode - Suspense mode: 'once' | 'first-load-locale' | 'change-locale'
23
+ * @returns Provider component
24
+ *
25
+ * @example
26
+ * ```tsx
27
+ * const store = storeFactory.type<MyTranslations>();
28
+ *
29
+ * function App() {
30
+ * return (
31
+ * <I18nTypedStoreProvider store={store}>
32
+ * <MyComponent />
33
+ * </I18nTypedStoreProvider>
34
+ * );
35
+ * }
36
+ * ```
37
+ */
38
+ declare const I18nTypedStoreProvider: <T extends Record<string, string>, L extends Record<string, string>, M extends { [K in keyof T]: any; }>({ store, children, suspenseMode, }: {
39
+ store: TranslationStore<T, L, M>;
40
+ children: ReactNode;
41
+ suspenseMode?: II18nTypedStoreContext<T, L, M>["suspenseMode"];
42
+ }) => react_jsx_runtime.JSX.Element;
43
+
44
+ /**
45
+ * Safe component that catches errors during string extraction from translation objects.
46
+ * Useful for safely accessing nested translation keys that might throw errors.
47
+ *
48
+ * @param props - Component props
49
+ * @param props.children - Function that returns a string (called during render)
50
+ * @param props.errorComponent - Component to display if an error occurs (default: empty string)
51
+ * @param props.errorHandler - Optional error handler callback
52
+ * @returns Rendered string wrapped in a span or error component
53
+ *
54
+ * @example
55
+ * ```tsx
56
+ * <Safe
57
+ * errorComponent={<span>N/A</span>}
58
+ * errorHandler={(error) => console.error(error)}
59
+ * >
60
+ * {() => translations.common.pages.main.title}
61
+ * </Safe>
62
+ * ```
63
+ */
64
+ declare const Safe: FC<{
65
+ children: () => string;
66
+ errorComponent?: ReactNode;
67
+ errorHandler?: (error: unknown) => void;
68
+ }>;
69
+
70
+ /**
71
+ * Hook for accessing translations with automatic loading and locale change handling.
72
+ * Returns undefined if translation is not yet loaded.
73
+ *
74
+ * @template T - Type of translations object (e.g., { common: 'common', errors: 'errors' })
75
+ * @template L - Type of locales object (e.g., { en: 'en', ru: 'ru' })
76
+ * @template M - Type of translation modules mapping
77
+ * @template K - Namespace key from translations object
78
+ *
79
+ * @param namespace - Namespace key to load translations for
80
+ * @param fromCache - Whether to use cached translation if available (default: true)
81
+ * @returns Translation object for the specified namespace, or undefined if not loaded
82
+ *
83
+ * @example
84
+ * ```tsx
85
+ * const translations = useI18nTranslation('common');
86
+ * if (translations) {
87
+ * console.log(translations.greeting);
88
+ * }
89
+ * ```
90
+ */
91
+ declare const useI18nTranslation: <T extends Record<string, string>, L extends Record<string, string>, M extends { [K_1 in keyof T]: any; }, K extends keyof T>(namespace: K, fromCache?: boolean) => M[K] | undefined;
92
+
93
+ /**
94
+ * Hook for accessing translations with lazy loading and suspense support.
95
+ * Throws a promise if translation is not yet loaded (for React Suspense).
96
+ * Always returns a translation object (never undefined).
97
+ *
98
+ * @template T - Type of translations object (e.g., { common: 'common', errors: 'errors' })
99
+ * @template L - Type of locales object (e.g., { en: 'en', ru: 'ru' })
100
+ * @template M - Type of translation modules mapping
101
+ * @template K - Namespace key from translations object
102
+ *
103
+ * @param namespace - Namespace key to load translations for
104
+ * @param fromCache - Whether to use cached translation if available (default: true)
105
+ * @returns Translation object for the specified namespace (never undefined)
106
+ * @throws Promise if translation is not yet loaded (for React Suspense)
107
+ *
108
+ * @example
109
+ * ```tsx
110
+ * function MyComponent() {
111
+ * const translations = useI18nTranslationLazy('common');
112
+ * return <div>{translations.greeting}</div>;
113
+ * }
114
+ *
115
+ * function App() {
116
+ * return (
117
+ * <Suspense fallback={<Loading />}>
118
+ * <MyComponent />
119
+ * </Suspense>
120
+ * );
121
+ * }
122
+ * ```
123
+ */
124
+ declare const useI18nTranslationLazy: <T extends Record<string, string>, L extends Record<string, string>, M extends { [K_1 in keyof T]: any; }, K extends keyof T>(namespace: K, fromCache?: boolean) => M[K];
125
+
126
+ /**
127
+ * Hook for accessing the I18n typed store context.
128
+ * Must be used within an I18nTypedStoreProvider.
129
+ *
130
+ * @template T - Type of translations object (e.g., { common: 'common', errors: 'errors' })
131
+ * @template L - Type of locales object (e.g., { en: 'en', ru: 'ru' })
132
+ * @template M - Type of translation modules mapping
133
+ *
134
+ * @returns I18n typed store context value
135
+ * @throws Error if used outside of I18nTypedStoreProvider
136
+ *
137
+ * @example
138
+ * ```tsx
139
+ * function MyComponent() {
140
+ * const { store, suspenseMode } = useI18nTypedStoreContext();
141
+ * // Use store, suspenseMode
142
+ * }
143
+ * ```
144
+ */
145
+ declare const useI18nTypedStoreContext: <T extends Record<string, string>, L extends Record<string, string>, M extends { [K in keyof T]: any; }>() => II18nTypedStoreContext<T, L, M>;
146
+
147
+ /**
148
+ * Hook for accessing and managing the current locale.
149
+ * Returns the current locale and a function to change it.
150
+ * Supports SSR/SSG by using useSyncExternalStore for proper hydration.
151
+ *
152
+ * @template T - Type of translations object
153
+ * @template L - Type of locales object
154
+ * @template M - Type of translation modules mapping
155
+ *
156
+ * @returns Object with current locale and setLocale function
157
+ *
158
+ * @example
159
+ * ```tsx
160
+ * function LocaleSwitcher() {
161
+ * const { locale, setLocale } = useI18nLocale();
162
+ * return (
163
+ * <select value={locale} onChange={(e) => setLocale(e.target.value as keyof Locales)}>
164
+ * <option value="en">English</option>
165
+ * <option value="ru">Русский</option>
166
+ * </select>
167
+ * );
168
+ * }
169
+ * ```
170
+ */
171
+ declare const useI18nLocale: <T extends Record<string, string>, L extends Record<string, string>, M extends { [K in keyof T]: any; }>() => {
172
+ locale: keyof L;
173
+ setLocale: (locale: keyof L) => void;
174
+ };
175
+
176
+ /**
177
+ * Request context interface for locale detection in SSR environments.
178
+ * Compatible with various SSR frameworks (Next.js, Remix, SvelteKit, etc.)
179
+ * and standard request objects that provide query, cookies, and headers.
180
+ */
181
+ interface RequestContext {
182
+ query?: Record<string, string | string[]>;
183
+ cookies?: Record<string, string>;
184
+ headers?: Record<string, string | string[]> | Headers;
185
+ }
186
+ /**
187
+ * Options for getting locale from SSR request.
188
+ */
189
+ interface GetLocaleFromRequestOptions {
190
+ /**
191
+ * Header name to read locale from (e.g., 'accept-language', 'x-locale')
192
+ * @default 'accept-language'
193
+ */
194
+ headerName?: string;
195
+ /**
196
+ * Cookie name to read locale from
197
+ */
198
+ cookieName?: string;
199
+ /**
200
+ * Query parameter name to read locale from (e.g., 'locale', 'lang')
201
+ */
202
+ queryParamName?: string;
203
+ /**
204
+ * Default locale to use if locale cannot be determined
205
+ */
206
+ defaultLocale: string;
207
+ /**
208
+ * Available locales to validate against
209
+ */
210
+ availableLocales: readonly string[];
211
+ /**
212
+ * Whether to parse Accept-Language header and find best match
213
+ * @default true
214
+ */
215
+ parseAcceptLanguage?: boolean;
216
+ }
217
+ /**
218
+ * Gets locale from SSR request context.
219
+ * Checks query params, cookies, and headers in that order.
220
+ * Works with any SSR framework that provides these standard request properties.
221
+ *
222
+ * @template L - Type of locales object
223
+ * @param context - SSR request context with query, cookies, and headers
224
+ * @param options - Options for locale detection
225
+ * @returns Detected locale key
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * // Next.js example
230
+ * import type { GetServerSidePropsContext } from 'next';
231
+ *
232
+ * export async function getServerSideProps(context: GetServerSidePropsContext) {
233
+ * const locale = getLocaleFromRequest(context, {
234
+ * defaultLocale: 'en',
235
+ * availableLocales: ['en', 'ru'],
236
+ * cookieName: 'locale',
237
+ * queryParamName: 'locale',
238
+ * });
239
+ *
240
+ * const store = storeFactory.type<MyTranslations>();
241
+ * initializeStore(store, locale);
242
+ *
243
+ * return { props: { locale } };
244
+ * }
245
+ * ```
246
+ *
247
+ * @example
248
+ * ```ts
249
+ * // Remix example
250
+ * export async function loader({ request }: LoaderFunctionArgs) {
251
+ * const url = new URL(request.url);
252
+ * const context: RequestContext = {
253
+ * query: Object.fromEntries(url.searchParams),
254
+ * headers: Object.fromEntries(request.headers),
255
+ * };
256
+ *
257
+ * const locale = getLocaleFromRequest(context, {
258
+ * defaultLocale: 'en',
259
+ * availableLocales: ['en', 'ru'],
260
+ * });
261
+ *
262
+ * return { locale };
263
+ * }
264
+ * ```
265
+ */
266
+ declare function getLocaleFromRequest<L extends Record<string, string>>(context: RequestContext, options: GetLocaleFromRequestOptions): keyof L;
267
+ /**
268
+ * Initializes translation store with a specific locale.
269
+ * Useful for SSR/SSG where you need to set the locale before rendering.
270
+ *
271
+ * @template T - Type of translations object
272
+ * @template L - Type of locales object
273
+ * @template M - Type of translation modules mapping
274
+ * @param store - Translation store instance
275
+ * @param locale - Locale to initialize with
276
+ *
277
+ * @example
278
+ * ```ts
279
+ * // In SSR handler
280
+ * const locale = getLocaleFromRequest(context, {
281
+ * defaultLocale: 'en',
282
+ * availableLocales: ['en', 'ru'],
283
+ * });
284
+ *
285
+ * const store = storeFactory.type<MyTranslations>();
286
+ * initializeStore(store, locale);
287
+ *
288
+ * // Preload translations if needed
289
+ * await store.common.load(locale);
290
+ * ```
291
+ */
292
+ declare function initializeStore<T extends Record<string, string>, L extends Record<string, string>, M extends {
293
+ [K in keyof T]: any;
294
+ }>(store: TranslationStore<T, L, M>, locale: keyof L): void;
295
+
296
+ export { type GetLocaleFromRequestOptions, I18nTypedStoreContext, I18nTypedStoreProvider, type RequestContext, Safe, getLocaleFromRequest, initializeStore, useI18nLocale, useI18nTranslation, useI18nTranslationLazy, useI18nTypedStoreContext };
@@ -0,0 +1,296 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import * as react from 'react';
3
+ import { ReactNode, FC } from 'react';
4
+ import { I as II18nTypedStoreContext, T as TranslationStore } from '../context-Dp43aQ0V.js';
5
+
6
+ /**
7
+ * React context for I18n typed store.
8
+ * Used internally by hooks to access the translation store.
9
+ */
10
+ declare const I18nTypedStoreContext: react.Context<II18nTypedStoreContext<any, any, any> | null>;
11
+ /**
12
+ * Provider component for I18n typed store.
13
+ * Wraps your application to provide translation store context to child components.
14
+ *
15
+ * @template T - Type of translations object (e.g., { common: 'common', errors: 'errors' })
16
+ * @template L - Type of locales object (e.g., { en: 'en', ru: 'ru' })
17
+ * @template M - Type of translation modules mapping
18
+ *
19
+ * @param props - Provider props
20
+ * @param props.store - Translation store instance
21
+ * @param props.children - React children
22
+ * @param props.suspenseMode - Suspense mode: 'once' | 'first-load-locale' | 'change-locale'
23
+ * @returns Provider component
24
+ *
25
+ * @example
26
+ * ```tsx
27
+ * const store = storeFactory.type<MyTranslations>();
28
+ *
29
+ * function App() {
30
+ * return (
31
+ * <I18nTypedStoreProvider store={store}>
32
+ * <MyComponent />
33
+ * </I18nTypedStoreProvider>
34
+ * );
35
+ * }
36
+ * ```
37
+ */
38
+ declare const I18nTypedStoreProvider: <T extends Record<string, string>, L extends Record<string, string>, M extends { [K in keyof T]: any; }>({ store, children, suspenseMode, }: {
39
+ store: TranslationStore<T, L, M>;
40
+ children: ReactNode;
41
+ suspenseMode?: II18nTypedStoreContext<T, L, M>["suspenseMode"];
42
+ }) => react_jsx_runtime.JSX.Element;
43
+
44
+ /**
45
+ * Safe component that catches errors during string extraction from translation objects.
46
+ * Useful for safely accessing nested translation keys that might throw errors.
47
+ *
48
+ * @param props - Component props
49
+ * @param props.children - Function that returns a string (called during render)
50
+ * @param props.errorComponent - Component to display if an error occurs (default: empty string)
51
+ * @param props.errorHandler - Optional error handler callback
52
+ * @returns Rendered string wrapped in a span or error component
53
+ *
54
+ * @example
55
+ * ```tsx
56
+ * <Safe
57
+ * errorComponent={<span>N/A</span>}
58
+ * errorHandler={(error) => console.error(error)}
59
+ * >
60
+ * {() => translations.common.pages.main.title}
61
+ * </Safe>
62
+ * ```
63
+ */
64
+ declare const Safe: FC<{
65
+ children: () => string;
66
+ errorComponent?: ReactNode;
67
+ errorHandler?: (error: unknown) => void;
68
+ }>;
69
+
70
+ /**
71
+ * Hook for accessing translations with automatic loading and locale change handling.
72
+ * Returns undefined if translation is not yet loaded.
73
+ *
74
+ * @template T - Type of translations object (e.g., { common: 'common', errors: 'errors' })
75
+ * @template L - Type of locales object (e.g., { en: 'en', ru: 'ru' })
76
+ * @template M - Type of translation modules mapping
77
+ * @template K - Namespace key from translations object
78
+ *
79
+ * @param namespace - Namespace key to load translations for
80
+ * @param fromCache - Whether to use cached translation if available (default: true)
81
+ * @returns Translation object for the specified namespace, or undefined if not loaded
82
+ *
83
+ * @example
84
+ * ```tsx
85
+ * const translations = useI18nTranslation('common');
86
+ * if (translations) {
87
+ * console.log(translations.greeting);
88
+ * }
89
+ * ```
90
+ */
91
+ declare const useI18nTranslation: <T extends Record<string, string>, L extends Record<string, string>, M extends { [K_1 in keyof T]: any; }, K extends keyof T>(namespace: K, fromCache?: boolean) => M[K] | undefined;
92
+
93
+ /**
94
+ * Hook for accessing translations with lazy loading and suspense support.
95
+ * Throws a promise if translation is not yet loaded (for React Suspense).
96
+ * Always returns a translation object (never undefined).
97
+ *
98
+ * @template T - Type of translations object (e.g., { common: 'common', errors: 'errors' })
99
+ * @template L - Type of locales object (e.g., { en: 'en', ru: 'ru' })
100
+ * @template M - Type of translation modules mapping
101
+ * @template K - Namespace key from translations object
102
+ *
103
+ * @param namespace - Namespace key to load translations for
104
+ * @param fromCache - Whether to use cached translation if available (default: true)
105
+ * @returns Translation object for the specified namespace (never undefined)
106
+ * @throws Promise if translation is not yet loaded (for React Suspense)
107
+ *
108
+ * @example
109
+ * ```tsx
110
+ * function MyComponent() {
111
+ * const translations = useI18nTranslationLazy('common');
112
+ * return <div>{translations.greeting}</div>;
113
+ * }
114
+ *
115
+ * function App() {
116
+ * return (
117
+ * <Suspense fallback={<Loading />}>
118
+ * <MyComponent />
119
+ * </Suspense>
120
+ * );
121
+ * }
122
+ * ```
123
+ */
124
+ declare const useI18nTranslationLazy: <T extends Record<string, string>, L extends Record<string, string>, M extends { [K_1 in keyof T]: any; }, K extends keyof T>(namespace: K, fromCache?: boolean) => M[K];
125
+
126
+ /**
127
+ * Hook for accessing the I18n typed store context.
128
+ * Must be used within an I18nTypedStoreProvider.
129
+ *
130
+ * @template T - Type of translations object (e.g., { common: 'common', errors: 'errors' })
131
+ * @template L - Type of locales object (e.g., { en: 'en', ru: 'ru' })
132
+ * @template M - Type of translation modules mapping
133
+ *
134
+ * @returns I18n typed store context value
135
+ * @throws Error if used outside of I18nTypedStoreProvider
136
+ *
137
+ * @example
138
+ * ```tsx
139
+ * function MyComponent() {
140
+ * const { store, suspenseMode } = useI18nTypedStoreContext();
141
+ * // Use store, suspenseMode
142
+ * }
143
+ * ```
144
+ */
145
+ declare const useI18nTypedStoreContext: <T extends Record<string, string>, L extends Record<string, string>, M extends { [K in keyof T]: any; }>() => II18nTypedStoreContext<T, L, M>;
146
+
147
+ /**
148
+ * Hook for accessing and managing the current locale.
149
+ * Returns the current locale and a function to change it.
150
+ * Supports SSR/SSG by using useSyncExternalStore for proper hydration.
151
+ *
152
+ * @template T - Type of translations object
153
+ * @template L - Type of locales object
154
+ * @template M - Type of translation modules mapping
155
+ *
156
+ * @returns Object with current locale and setLocale function
157
+ *
158
+ * @example
159
+ * ```tsx
160
+ * function LocaleSwitcher() {
161
+ * const { locale, setLocale } = useI18nLocale();
162
+ * return (
163
+ * <select value={locale} onChange={(e) => setLocale(e.target.value as keyof Locales)}>
164
+ * <option value="en">English</option>
165
+ * <option value="ru">Русский</option>
166
+ * </select>
167
+ * );
168
+ * }
169
+ * ```
170
+ */
171
+ declare const useI18nLocale: <T extends Record<string, string>, L extends Record<string, string>, M extends { [K in keyof T]: any; }>() => {
172
+ locale: keyof L;
173
+ setLocale: (locale: keyof L) => void;
174
+ };
175
+
176
+ /**
177
+ * Request context interface for locale detection in SSR environments.
178
+ * Compatible with various SSR frameworks (Next.js, Remix, SvelteKit, etc.)
179
+ * and standard request objects that provide query, cookies, and headers.
180
+ */
181
+ interface RequestContext {
182
+ query?: Record<string, string | string[]>;
183
+ cookies?: Record<string, string>;
184
+ headers?: Record<string, string | string[]> | Headers;
185
+ }
186
+ /**
187
+ * Options for getting locale from SSR request.
188
+ */
189
+ interface GetLocaleFromRequestOptions {
190
+ /**
191
+ * Header name to read locale from (e.g., 'accept-language', 'x-locale')
192
+ * @default 'accept-language'
193
+ */
194
+ headerName?: string;
195
+ /**
196
+ * Cookie name to read locale from
197
+ */
198
+ cookieName?: string;
199
+ /**
200
+ * Query parameter name to read locale from (e.g., 'locale', 'lang')
201
+ */
202
+ queryParamName?: string;
203
+ /**
204
+ * Default locale to use if locale cannot be determined
205
+ */
206
+ defaultLocale: string;
207
+ /**
208
+ * Available locales to validate against
209
+ */
210
+ availableLocales: readonly string[];
211
+ /**
212
+ * Whether to parse Accept-Language header and find best match
213
+ * @default true
214
+ */
215
+ parseAcceptLanguage?: boolean;
216
+ }
217
+ /**
218
+ * Gets locale from SSR request context.
219
+ * Checks query params, cookies, and headers in that order.
220
+ * Works with any SSR framework that provides these standard request properties.
221
+ *
222
+ * @template L - Type of locales object
223
+ * @param context - SSR request context with query, cookies, and headers
224
+ * @param options - Options for locale detection
225
+ * @returns Detected locale key
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * // Next.js example
230
+ * import type { GetServerSidePropsContext } from 'next';
231
+ *
232
+ * export async function getServerSideProps(context: GetServerSidePropsContext) {
233
+ * const locale = getLocaleFromRequest(context, {
234
+ * defaultLocale: 'en',
235
+ * availableLocales: ['en', 'ru'],
236
+ * cookieName: 'locale',
237
+ * queryParamName: 'locale',
238
+ * });
239
+ *
240
+ * const store = storeFactory.type<MyTranslations>();
241
+ * initializeStore(store, locale);
242
+ *
243
+ * return { props: { locale } };
244
+ * }
245
+ * ```
246
+ *
247
+ * @example
248
+ * ```ts
249
+ * // Remix example
250
+ * export async function loader({ request }: LoaderFunctionArgs) {
251
+ * const url = new URL(request.url);
252
+ * const context: RequestContext = {
253
+ * query: Object.fromEntries(url.searchParams),
254
+ * headers: Object.fromEntries(request.headers),
255
+ * };
256
+ *
257
+ * const locale = getLocaleFromRequest(context, {
258
+ * defaultLocale: 'en',
259
+ * availableLocales: ['en', 'ru'],
260
+ * });
261
+ *
262
+ * return { locale };
263
+ * }
264
+ * ```
265
+ */
266
+ declare function getLocaleFromRequest<L extends Record<string, string>>(context: RequestContext, options: GetLocaleFromRequestOptions): keyof L;
267
+ /**
268
+ * Initializes translation store with a specific locale.
269
+ * Useful for SSR/SSG where you need to set the locale before rendering.
270
+ *
271
+ * @template T - Type of translations object
272
+ * @template L - Type of locales object
273
+ * @template M - Type of translation modules mapping
274
+ * @param store - Translation store instance
275
+ * @param locale - Locale to initialize with
276
+ *
277
+ * @example
278
+ * ```ts
279
+ * // In SSR handler
280
+ * const locale = getLocaleFromRequest(context, {
281
+ * defaultLocale: 'en',
282
+ * availableLocales: ['en', 'ru'],
283
+ * });
284
+ *
285
+ * const store = storeFactory.type<MyTranslations>();
286
+ * initializeStore(store, locale);
287
+ *
288
+ * // Preload translations if needed
289
+ * await store.common.load(locale);
290
+ * ```
291
+ */
292
+ declare function initializeStore<T extends Record<string, string>, L extends Record<string, string>, M extends {
293
+ [K in keyof T]: any;
294
+ }>(store: TranslationStore<T, L, M>, locale: keyof L): void;
295
+
296
+ export { type GetLocaleFromRequestOptions, I18nTypedStoreContext, I18nTypedStoreProvider, type RequestContext, Safe, getLocaleFromRequest, initializeStore, useI18nLocale, useI18nTranslation, useI18nTranslationLazy, useI18nTypedStoreContext };