lokal-react 1.3.2 → 1.3.6
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.mts +144 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.js +357 -44
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +369 -36
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/context/LokalContext.tsx +475 -37
- package/src/hooks/useStandaloneTranslate.ts +117 -0
- package/src/index.ts +30 -4
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import React, { ReactNode } from 'react';
|
|
3
|
+
import { LocaleData } from 'lokal-core';
|
|
4
|
+
export { LocaleData } from 'lokal-core';
|
|
5
|
+
|
|
6
|
+
type TranslateFunction = <K extends string>(key: K, params?: Record<string, string | number>) => string;
|
|
7
|
+
type PluralCategory = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other';
|
|
8
|
+
interface PluralParams {
|
|
9
|
+
count: number;
|
|
10
|
+
[key: string]: string | number;
|
|
11
|
+
}
|
|
12
|
+
type GenderCategory = 'male' | 'female' | 'other';
|
|
13
|
+
interface GenderParams {
|
|
14
|
+
gender: GenderCategory;
|
|
15
|
+
[key: string]: string | number;
|
|
16
|
+
}
|
|
17
|
+
interface ExtendedTranslateFunction {
|
|
18
|
+
<K extends string>(key: K, params?: Record<string, string | number>): string;
|
|
19
|
+
plural: <K extends string>(key: K, params: PluralParams) => string;
|
|
20
|
+
gender: <K extends string>(key: K, params: GenderParams) => string;
|
|
21
|
+
choice: <K extends string>(key: K, params: {
|
|
22
|
+
value: string | number;
|
|
23
|
+
}) => string;
|
|
24
|
+
}
|
|
25
|
+
interface StorageInterface {
|
|
26
|
+
getItem(key: string): Promise<string | null>;
|
|
27
|
+
setItem(key: string, value: string): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
interface DateFormatOptions {
|
|
30
|
+
year?: 'numeric' | '2-digit';
|
|
31
|
+
month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';
|
|
32
|
+
day?: 'numeric' | '2-digit';
|
|
33
|
+
hour?: 'numeric' | '2-digit';
|
|
34
|
+
minute?: 'numeric' | '2-digit';
|
|
35
|
+
second?: 'numeric' | '2-digit';
|
|
36
|
+
timeZoneName?: 'short' | 'long';
|
|
37
|
+
}
|
|
38
|
+
interface NumberFormatOptions {
|
|
39
|
+
style?: 'decimal' | 'currency' | 'percent' | 'unit';
|
|
40
|
+
currency?: string;
|
|
41
|
+
currencyDisplay?: 'symbol' | 'code' | 'name';
|
|
42
|
+
minimumFractionDigits?: number;
|
|
43
|
+
maximumFractionDigits?: number;
|
|
44
|
+
useGrouping?: boolean;
|
|
45
|
+
unit?: string;
|
|
46
|
+
unitDisplay?: 'long' | 'short' | 'narrow';
|
|
47
|
+
}
|
|
48
|
+
interface LokalContextValue {
|
|
49
|
+
locale: string;
|
|
50
|
+
setLocale: (locale: string) => void;
|
|
51
|
+
locales: string[];
|
|
52
|
+
t: ExtendedTranslateFunction;
|
|
53
|
+
translations: LocaleData;
|
|
54
|
+
isLoading: boolean;
|
|
55
|
+
formatDate: (date: Date | string, options?: DateFormatOptions) => string;
|
|
56
|
+
formatNumber: (num: number, options?: NumberFormatOptions) => string;
|
|
57
|
+
}
|
|
58
|
+
interface LokalProviderProps {
|
|
59
|
+
children: ReactNode;
|
|
60
|
+
locale?: string;
|
|
61
|
+
locales?: string[];
|
|
62
|
+
translations?: LocaleData;
|
|
63
|
+
storage?: StorageInterface;
|
|
64
|
+
namespace?: string;
|
|
65
|
+
defaultLocale?: string;
|
|
66
|
+
onLocaleChange?: (locale: string) => void;
|
|
67
|
+
initialLocale?: string;
|
|
68
|
+
initialTranslations?: LocaleData;
|
|
69
|
+
fallbackLocale?: string;
|
|
70
|
+
}
|
|
71
|
+
declare function LokalProvider({ children, locale: initialLocale, locales, translations: initialTranslations, storage, namespace, defaultLocale, onLocaleChange, initialLocale: ssrInitialLocale, initialTranslations: ssrInitialTranslations, fallbackLocale, }: LokalProviderProps): react_jsx_runtime.JSX.Element;
|
|
72
|
+
/**
|
|
73
|
+
* Hook to access the full Lokal context
|
|
74
|
+
*/
|
|
75
|
+
declare function useLokal(): LokalContextValue;
|
|
76
|
+
/**
|
|
77
|
+
* Hook to access only the translation function
|
|
78
|
+
*/
|
|
79
|
+
declare function useTranslate(): ExtendedTranslateFunction;
|
|
80
|
+
/**
|
|
81
|
+
* Hook to access locale and setter
|
|
82
|
+
*/
|
|
83
|
+
declare function useLocale(): {
|
|
84
|
+
locale: string;
|
|
85
|
+
setLocale: (locale: string) => void;
|
|
86
|
+
locales: string[];
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Hook to access date/number formatters
|
|
90
|
+
*/
|
|
91
|
+
declare function useFormatters(): {
|
|
92
|
+
formatDate: (date: Date | string, options?: DateFormatOptions) => string;
|
|
93
|
+
formatNumber: (num: number, options?: NumberFormatOptions) => string;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Hook to check if translations are loading
|
|
97
|
+
*/
|
|
98
|
+
declare function useIsLoading(): boolean;
|
|
99
|
+
interface TComponentProps {
|
|
100
|
+
children: string;
|
|
101
|
+
params?: Record<string, string | number>;
|
|
102
|
+
plural?: number;
|
|
103
|
+
gender?: GenderCategory;
|
|
104
|
+
className?: string;
|
|
105
|
+
as?: React.ElementType;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* T Component - Translate content directly in JSX
|
|
109
|
+
* Usage: <T>hello_world</T> or <T params={{name: 'John'}}>greeting</T>
|
|
110
|
+
*/
|
|
111
|
+
declare function T({ children, params, plural, gender, className, as: Component }: TComponentProps): react_jsx_runtime.JSX.Element;
|
|
112
|
+
/**
|
|
113
|
+
* Hook for lazy-loaded translations (useful for code splitting)
|
|
114
|
+
*/
|
|
115
|
+
declare function useLazyTranslations(namespace: string): {
|
|
116
|
+
t: (key: string, params?: Record<string, string | number>) => string;
|
|
117
|
+
isLoading: boolean;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Hook to handle SSR hydration
|
|
121
|
+
* Returns true until client-side hydration is complete
|
|
122
|
+
*/
|
|
123
|
+
declare function useHydrated(): boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Hook to get translations only after hydration
|
|
126
|
+
* Prevents hydration mismatches
|
|
127
|
+
*/
|
|
128
|
+
declare function useSafeTranslations(): {
|
|
129
|
+
t: ExtendedTranslateFunction;
|
|
130
|
+
isHydrated: boolean;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Create a standalone translation function
|
|
135
|
+
* Useful for non-React contexts or when you want to avoid hooks
|
|
136
|
+
*/
|
|
137
|
+
declare function createTranslate(translations: LocaleData, locale?: string): ExtendedTranslateFunction;
|
|
138
|
+
/**
|
|
139
|
+
* Hook to create a standalone translate function from context
|
|
140
|
+
* This is useful when you want to pass translation function to non-React code
|
|
141
|
+
*/
|
|
142
|
+
declare function useStandaloneTranslate(): ExtendedTranslateFunction;
|
|
143
|
+
|
|
144
|
+
export { type DateFormatOptions, type ExtendedTranslateFunction, type GenderCategory, type GenderParams, type LokalContextValue, LokalProvider, type LokalProviderProps, type NumberFormatOptions, type PluralCategory, type PluralParams, type StorageInterface, T, type TranslateFunction, createTranslate, useFormatters, useHydrated, useIsLoading, useLazyTranslations, useLocale, useLokal, useSafeTranslations, useStandaloneTranslate, useTranslate };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import React, { ReactNode } from 'react';
|
|
3
|
+
import { LocaleData } from 'lokal-core';
|
|
4
|
+
export { LocaleData } from 'lokal-core';
|
|
5
|
+
|
|
6
|
+
type TranslateFunction = <K extends string>(key: K, params?: Record<string, string | number>) => string;
|
|
7
|
+
type PluralCategory = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other';
|
|
8
|
+
interface PluralParams {
|
|
9
|
+
count: number;
|
|
10
|
+
[key: string]: string | number;
|
|
11
|
+
}
|
|
12
|
+
type GenderCategory = 'male' | 'female' | 'other';
|
|
13
|
+
interface GenderParams {
|
|
14
|
+
gender: GenderCategory;
|
|
15
|
+
[key: string]: string | number;
|
|
16
|
+
}
|
|
17
|
+
interface ExtendedTranslateFunction {
|
|
18
|
+
<K extends string>(key: K, params?: Record<string, string | number>): string;
|
|
19
|
+
plural: <K extends string>(key: K, params: PluralParams) => string;
|
|
20
|
+
gender: <K extends string>(key: K, params: GenderParams) => string;
|
|
21
|
+
choice: <K extends string>(key: K, params: {
|
|
22
|
+
value: string | number;
|
|
23
|
+
}) => string;
|
|
24
|
+
}
|
|
25
|
+
interface StorageInterface {
|
|
26
|
+
getItem(key: string): Promise<string | null>;
|
|
27
|
+
setItem(key: string, value: string): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
interface DateFormatOptions {
|
|
30
|
+
year?: 'numeric' | '2-digit';
|
|
31
|
+
month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';
|
|
32
|
+
day?: 'numeric' | '2-digit';
|
|
33
|
+
hour?: 'numeric' | '2-digit';
|
|
34
|
+
minute?: 'numeric' | '2-digit';
|
|
35
|
+
second?: 'numeric' | '2-digit';
|
|
36
|
+
timeZoneName?: 'short' | 'long';
|
|
37
|
+
}
|
|
38
|
+
interface NumberFormatOptions {
|
|
39
|
+
style?: 'decimal' | 'currency' | 'percent' | 'unit';
|
|
40
|
+
currency?: string;
|
|
41
|
+
currencyDisplay?: 'symbol' | 'code' | 'name';
|
|
42
|
+
minimumFractionDigits?: number;
|
|
43
|
+
maximumFractionDigits?: number;
|
|
44
|
+
useGrouping?: boolean;
|
|
45
|
+
unit?: string;
|
|
46
|
+
unitDisplay?: 'long' | 'short' | 'narrow';
|
|
47
|
+
}
|
|
48
|
+
interface LokalContextValue {
|
|
49
|
+
locale: string;
|
|
50
|
+
setLocale: (locale: string) => void;
|
|
51
|
+
locales: string[];
|
|
52
|
+
t: ExtendedTranslateFunction;
|
|
53
|
+
translations: LocaleData;
|
|
54
|
+
isLoading: boolean;
|
|
55
|
+
formatDate: (date: Date | string, options?: DateFormatOptions) => string;
|
|
56
|
+
formatNumber: (num: number, options?: NumberFormatOptions) => string;
|
|
57
|
+
}
|
|
58
|
+
interface LokalProviderProps {
|
|
59
|
+
children: ReactNode;
|
|
60
|
+
locale?: string;
|
|
61
|
+
locales?: string[];
|
|
62
|
+
translations?: LocaleData;
|
|
63
|
+
storage?: StorageInterface;
|
|
64
|
+
namespace?: string;
|
|
65
|
+
defaultLocale?: string;
|
|
66
|
+
onLocaleChange?: (locale: string) => void;
|
|
67
|
+
initialLocale?: string;
|
|
68
|
+
initialTranslations?: LocaleData;
|
|
69
|
+
fallbackLocale?: string;
|
|
70
|
+
}
|
|
71
|
+
declare function LokalProvider({ children, locale: initialLocale, locales, translations: initialTranslations, storage, namespace, defaultLocale, onLocaleChange, initialLocale: ssrInitialLocale, initialTranslations: ssrInitialTranslations, fallbackLocale, }: LokalProviderProps): react_jsx_runtime.JSX.Element;
|
|
72
|
+
/**
|
|
73
|
+
* Hook to access the full Lokal context
|
|
74
|
+
*/
|
|
75
|
+
declare function useLokal(): LokalContextValue;
|
|
76
|
+
/**
|
|
77
|
+
* Hook to access only the translation function
|
|
78
|
+
*/
|
|
79
|
+
declare function useTranslate(): ExtendedTranslateFunction;
|
|
80
|
+
/**
|
|
81
|
+
* Hook to access locale and setter
|
|
82
|
+
*/
|
|
83
|
+
declare function useLocale(): {
|
|
84
|
+
locale: string;
|
|
85
|
+
setLocale: (locale: string) => void;
|
|
86
|
+
locales: string[];
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Hook to access date/number formatters
|
|
90
|
+
*/
|
|
91
|
+
declare function useFormatters(): {
|
|
92
|
+
formatDate: (date: Date | string, options?: DateFormatOptions) => string;
|
|
93
|
+
formatNumber: (num: number, options?: NumberFormatOptions) => string;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Hook to check if translations are loading
|
|
97
|
+
*/
|
|
98
|
+
declare function useIsLoading(): boolean;
|
|
99
|
+
interface TComponentProps {
|
|
100
|
+
children: string;
|
|
101
|
+
params?: Record<string, string | number>;
|
|
102
|
+
plural?: number;
|
|
103
|
+
gender?: GenderCategory;
|
|
104
|
+
className?: string;
|
|
105
|
+
as?: React.ElementType;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* T Component - Translate content directly in JSX
|
|
109
|
+
* Usage: <T>hello_world</T> or <T params={{name: 'John'}}>greeting</T>
|
|
110
|
+
*/
|
|
111
|
+
declare function T({ children, params, plural, gender, className, as: Component }: TComponentProps): react_jsx_runtime.JSX.Element;
|
|
112
|
+
/**
|
|
113
|
+
* Hook for lazy-loaded translations (useful for code splitting)
|
|
114
|
+
*/
|
|
115
|
+
declare function useLazyTranslations(namespace: string): {
|
|
116
|
+
t: (key: string, params?: Record<string, string | number>) => string;
|
|
117
|
+
isLoading: boolean;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Hook to handle SSR hydration
|
|
121
|
+
* Returns true until client-side hydration is complete
|
|
122
|
+
*/
|
|
123
|
+
declare function useHydrated(): boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Hook to get translations only after hydration
|
|
126
|
+
* Prevents hydration mismatches
|
|
127
|
+
*/
|
|
128
|
+
declare function useSafeTranslations(): {
|
|
129
|
+
t: ExtendedTranslateFunction;
|
|
130
|
+
isHydrated: boolean;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Create a standalone translation function
|
|
135
|
+
* Useful for non-React contexts or when you want to avoid hooks
|
|
136
|
+
*/
|
|
137
|
+
declare function createTranslate(translations: LocaleData, locale?: string): ExtendedTranslateFunction;
|
|
138
|
+
/**
|
|
139
|
+
* Hook to create a standalone translate function from context
|
|
140
|
+
* This is useful when you want to pass translation function to non-React code
|
|
141
|
+
*/
|
|
142
|
+
declare function useStandaloneTranslate(): ExtendedTranslateFunction;
|
|
143
|
+
|
|
144
|
+
export { type DateFormatOptions, type ExtendedTranslateFunction, type GenderCategory, type GenderParams, type LokalContextValue, LokalProvider, type LokalProviderProps, type NumberFormatOptions, type PluralCategory, type PluralParams, type StorageInterface, T, type TranslateFunction, createTranslate, useFormatters, useHydrated, useIsLoading, useLazyTranslations, useLocale, useLokal, useSafeTranslations, useStandaloneTranslate, useTranslate };
|