@sheet-i18n/react 1.6.5 → 1.7.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.
package/dist/index.d.mts CHANGED
@@ -1,59 +1,37 @@
1
1
  import { I18nStore } from '@sheet-i18n/core';
2
2
  export * from '@sheet-i18n/core';
3
- import { ObserverManager } from '@sheet-i18n/shared-utils';
3
+ import * as _sheet_i18n_shared_service from '@sheet-i18n/shared-service';
4
+ import { LocaleStorageManager, IStorageService } from '@sheet-i18n/shared-service';
5
+ export { LocaleStorageManager, IStorageService as Storage, ruleFactory } from '@sheet-i18n/shared-service';
4
6
  import { MessageDescriptor, IntlShape } from 'react-intl';
7
+ import { SupportedLocales, LocaleSet, TypeSafe, SafeSheetTitle, SafeMessageId } from '@sheet-i18n/typescript';
5
8
  import * as react_jsx_runtime from 'react/jsx-runtime';
6
9
 
7
- interface IStorageService<V extends string> {
8
- getItem(key: string): V;
9
- setItem(key: string, value: V): boolean;
10
- removeItem(key: string): boolean;
11
- clear(): boolean;
12
- }
13
- /**
14
- * locale storage manager
15
- * implements ILocaleStorageManager (injected StorageService)
16
- */
17
- interface ILocaleStorageManager<TSupportedLocales extends readonly string[]> {
18
- getLocale(): TSupportedLocales[number];
19
- setLocale(locale: TSupportedLocales[number]): void;
20
- }
21
- declare class LocaleStorageManager<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean = false> implements ILocaleStorageManager<TSupportedLocales> {
22
- private readonly storageService;
23
- private readonly i18nStore;
24
- private readonly localeStorageKey;
25
- observerManager: ObserverManager<TSupportedLocales[number]>;
26
- constructor(storageService: IStorageService<TSupportedLocales[number]>, i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, localeStorageKey?: string);
27
- private initializeCurrentLocale;
28
- getLocale: () => TSupportedLocales[number];
29
- setLocale: (locale: TSupportedLocales[number]) => void;
30
- }
31
-
32
10
  type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
33
11
  type ExtendedUseIntlParams<D = MessageDescriptor> = UseIntlParams<D> extends [infer A, infer B, ...infer Rest] ? A extends Record<string, infer V> ? [Record<string, V | React.ReactNode>, B, ...Rest] : [A, B, ...Rest] : UseIntlParams<D>;
34
12
  type $TParams = Partial<ExtendedUseIntlParams>;
13
+ type RuleKey = string;
35
14
  /**
36
- * Resolves to keys of a specific sheet title in a localized set,
37
- * or falls back to `string` if not type-safe or keys resolve to `never`.
15
+ * Extended rule function type with full generic chain.
16
+ * Used internally by RuleService / ruleFactory so that createRule infers the
17
+ * correct value and message-id types from the store's locale data.
38
18
  */
39
- type SafeMessageId<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TTypeSafe extends boolean> = TTypeSafe extends true ? keyof NonNullable<TLocaleSet[TSupportedLocales[number]]>[TSheetTitle] extends never ? string : keyof NonNullable<TLocaleSet[TSupportedLocales[number]]>[TSheetTitle] : string;
19
+ type RuleFn<TSupportedLocales extends SupportedLocales, TLocaleSet extends LocaleSet, TI18nStore extends I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, TTypeSafe extends TypeSafe, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>, TValues = any> = (values: TValues, currentLocaleSet: ReturnType<TI18nStore['getCurrentLocaleSet']>) => TMessageId;
40
20
  /**
41
- * Resolves to valid sheet titles from the locale set
42
- * or defaults to string if type safety is off or keys are not available.
21
+ * Extended rules map with full generic chain.
22
+ * Consumers can annotate their rules objects with the simpler `Rules` from
23
+ * @sheet-i18n/typescript; this extended version is used for inference only.
43
24
  */
44
- type SupportedLocales = readonly string[];
45
- type LocaleSet = Partial<Record<SupportedLocales[number], Record<string, any>>>;
46
- type TypeSafe = boolean;
47
- type SafeSheetTitle<TSupportedLocales extends SupportedLocales, TLocaleSet extends LocaleSet, TTypeSafe extends TypeSafe> = TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] extends never ? string : keyof TLocaleSet[TSupportedLocales[number]] : string;
25
+ type Rules<TSupportedLocales extends SupportedLocales = SupportedLocales, TLocaleSet extends LocaleSet = LocaleSet, TTypeSafe extends TypeSafe = TypeSafe, TI18nStore extends I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe> = I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe> = SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe> = SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>> = Record<RuleKey, RuleFn<TSupportedLocales, TLocaleSet, TI18nStore, TTypeSafe, TSheetTitle, TMessageId>>;
48
26
  /**
49
- * Plugin types
27
+ * Plugin container for createI18nContext.
28
+ * Constrains TRules against React's own extended Rules (not the simplified
29
+ * base from @sheet-i18n/typescript) so that RuleFn's TMessageId return type
30
+ * is preserved through the generic chain.
50
31
  */
51
- type RuleKey = string;
52
- type RuleFn<TSupportedLocales extends SupportedLocales, TLocaleSet extends LocaleSet, TI18nStore extends I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, TTypeSafe extends TypeSafe, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>, TValues = any> = (values: TValues, currentLocaleSet: ReturnType<TI18nStore['getCurrentLocaleSet']>) => TMessageId;
53
32
  type Plugins<TRules extends Rules = Rules> = {
54
33
  rules?: TRules;
55
34
  };
56
- type Rules<TSupportedLocales extends SupportedLocales = SupportedLocales, TLocaleSet extends LocaleSet = LocaleSet, TTypeSafe extends TypeSafe = TypeSafe, TI18nStore extends I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe> = I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe> = SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe> = SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>> = Record<RuleKey, RuleFn<TSupportedLocales, TLocaleSet, TI18nStore, TTypeSafe, TSheetTitle, TMessageId>>;
57
35
 
58
36
  type GetTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>> = {
59
37
  t: <TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>, TValues extends $TParams[0], TOpts extends $TParams[1], TDescriptor extends $TParams[2]>(id: TMessageId, values?: TValues, opts?: TOpts, _descriptor?: TDescriptor) => TTypeSafe extends true ? TMessageId : any;
@@ -76,7 +54,7 @@ type UseTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSe
76
54
  type IntlProviderProps<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean = false> = {
77
55
  i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>;
78
56
  currentLocale?: TSupportedLocales[number];
79
- children: Omit<React.ReactNode, 'bigint'> | ((currentLocale?: TSupportedLocales[number]) => React.ReactNode);
57
+ children: React.ReactNode | ((currentLocale?: TSupportedLocales[number]) => React.ReactNode);
80
58
  };
81
59
 
82
60
  declare function useLocaleStorage<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean = false>(localeStorageManager: LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>): {
@@ -87,7 +65,7 @@ interface StorageBasedIntlProviderProps<TSupportedLocales extends readonly strin
87
65
  i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>;
88
66
  storageManager: LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>;
89
67
  fallbackUI?: React.ReactNode;
90
- children: Omit<React.ReactNode, 'bigint'> | ((currentLocale?: TSupportedLocales[number]) => React.ReactNode);
68
+ children: React.ReactNode | ((currentLocale?: TSupportedLocales[number]) => React.ReactNode);
91
69
  }
92
70
 
93
71
  declare function createI18nContext<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TRules extends Rules, TTypeSafe extends boolean = false>(i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, plugins?: Plugins<TRules>): {
@@ -98,15 +76,8 @@ declare function createI18nContext<TSupportedLocales extends readonly string[],
98
76
  StorageBasedIntlProvider: ({ storageManager, children, }: Omit<StorageBasedIntlProviderProps<TSupportedLocales, TLocaleSet, TTypeSafe>, "i18nStore">) => react_jsx_runtime.JSX.Element;
99
77
  useTranslation: <TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>>(sheetTitle: TSheetTitle) => UseTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle, TRules>;
100
78
  getTranslation: <TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>>(sheetTitle: TSheetTitle) => GetTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle>;
101
- getLocaleStorageManager: (storageService?: IStorageService<string>) => LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>;
79
+ getLocaleStorageManager: (storageService?: IStorageService<string>) => _sheet_i18n_shared_service.LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>;
102
80
  useLocaleStorage: typeof useLocaleStorage;
103
81
  };
104
82
 
105
- declare class RuleService<TSupportedLocales extends SupportedLocales, TLocaleSet extends LocaleSet, TTypeSafe extends TypeSafe, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>> {
106
- private readonly i18nStore;
107
- constructor(i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>);
108
- createRule: <TValues extends $TParams[0]>(ruleFn: RuleFn<TSupportedLocales, TLocaleSet, I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, TTypeSafe, TSheetTitle, TMessageId, TValues>) => (values: TValues) => TMessageId;
109
- }
110
- declare const ruleFactory: <TSupportedLocales extends SupportedLocales, TLocaleSet extends LocaleSet, TTypeSafe extends TypeSafe>(i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>) => RuleService<TSupportedLocales, TLocaleSet, TTypeSafe, SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, SafeMessageId<TSupportedLocales, TLocaleSet, SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TTypeSafe>>;
111
-
112
- export { type GetTranslationReturn as GetTranslation, LocaleStorageManager, type Plugins, type Rules, type IStorageService as Storage, type StorageBasedIntlProviderProps, type UseTranslationReturn as UseTranslation, createI18nContext, ruleFactory };
83
+ export { type GetTranslationReturn as GetTranslation, type StorageBasedIntlProviderProps, type UseTranslationReturn as UseTranslation, createI18nContext };
package/dist/index.d.ts CHANGED
@@ -1,59 +1,37 @@
1
1
  import { I18nStore } from '@sheet-i18n/core';
2
2
  export * from '@sheet-i18n/core';
3
- import { ObserverManager } from '@sheet-i18n/shared-utils';
3
+ import * as _sheet_i18n_shared_service from '@sheet-i18n/shared-service';
4
+ import { LocaleStorageManager, IStorageService } from '@sheet-i18n/shared-service';
5
+ export { LocaleStorageManager, IStorageService as Storage, ruleFactory } from '@sheet-i18n/shared-service';
4
6
  import { MessageDescriptor, IntlShape } from 'react-intl';
7
+ import { SupportedLocales, LocaleSet, TypeSafe, SafeSheetTitle, SafeMessageId } from '@sheet-i18n/typescript';
5
8
  import * as react_jsx_runtime from 'react/jsx-runtime';
6
9
 
7
- interface IStorageService<V extends string> {
8
- getItem(key: string): V;
9
- setItem(key: string, value: V): boolean;
10
- removeItem(key: string): boolean;
11
- clear(): boolean;
12
- }
13
- /**
14
- * locale storage manager
15
- * implements ILocaleStorageManager (injected StorageService)
16
- */
17
- interface ILocaleStorageManager<TSupportedLocales extends readonly string[]> {
18
- getLocale(): TSupportedLocales[number];
19
- setLocale(locale: TSupportedLocales[number]): void;
20
- }
21
- declare class LocaleStorageManager<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean = false> implements ILocaleStorageManager<TSupportedLocales> {
22
- private readonly storageService;
23
- private readonly i18nStore;
24
- private readonly localeStorageKey;
25
- observerManager: ObserverManager<TSupportedLocales[number]>;
26
- constructor(storageService: IStorageService<TSupportedLocales[number]>, i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, localeStorageKey?: string);
27
- private initializeCurrentLocale;
28
- getLocale: () => TSupportedLocales[number];
29
- setLocale: (locale: TSupportedLocales[number]) => void;
30
- }
31
-
32
10
  type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
33
11
  type ExtendedUseIntlParams<D = MessageDescriptor> = UseIntlParams<D> extends [infer A, infer B, ...infer Rest] ? A extends Record<string, infer V> ? [Record<string, V | React.ReactNode>, B, ...Rest] : [A, B, ...Rest] : UseIntlParams<D>;
34
12
  type $TParams = Partial<ExtendedUseIntlParams>;
13
+ type RuleKey = string;
35
14
  /**
36
- * Resolves to keys of a specific sheet title in a localized set,
37
- * or falls back to `string` if not type-safe or keys resolve to `never`.
15
+ * Extended rule function type with full generic chain.
16
+ * Used internally by RuleService / ruleFactory so that createRule infers the
17
+ * correct value and message-id types from the store's locale data.
38
18
  */
39
- type SafeMessageId<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TTypeSafe extends boolean> = TTypeSafe extends true ? keyof NonNullable<TLocaleSet[TSupportedLocales[number]]>[TSheetTitle] extends never ? string : keyof NonNullable<TLocaleSet[TSupportedLocales[number]]>[TSheetTitle] : string;
19
+ type RuleFn<TSupportedLocales extends SupportedLocales, TLocaleSet extends LocaleSet, TI18nStore extends I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, TTypeSafe extends TypeSafe, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>, TValues = any> = (values: TValues, currentLocaleSet: ReturnType<TI18nStore['getCurrentLocaleSet']>) => TMessageId;
40
20
  /**
41
- * Resolves to valid sheet titles from the locale set
42
- * or defaults to string if type safety is off or keys are not available.
21
+ * Extended rules map with full generic chain.
22
+ * Consumers can annotate their rules objects with the simpler `Rules` from
23
+ * @sheet-i18n/typescript; this extended version is used for inference only.
43
24
  */
44
- type SupportedLocales = readonly string[];
45
- type LocaleSet = Partial<Record<SupportedLocales[number], Record<string, any>>>;
46
- type TypeSafe = boolean;
47
- type SafeSheetTitle<TSupportedLocales extends SupportedLocales, TLocaleSet extends LocaleSet, TTypeSafe extends TypeSafe> = TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] extends never ? string : keyof TLocaleSet[TSupportedLocales[number]] : string;
25
+ type Rules<TSupportedLocales extends SupportedLocales = SupportedLocales, TLocaleSet extends LocaleSet = LocaleSet, TTypeSafe extends TypeSafe = TypeSafe, TI18nStore extends I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe> = I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe> = SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe> = SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>> = Record<RuleKey, RuleFn<TSupportedLocales, TLocaleSet, TI18nStore, TTypeSafe, TSheetTitle, TMessageId>>;
48
26
  /**
49
- * Plugin types
27
+ * Plugin container for createI18nContext.
28
+ * Constrains TRules against React's own extended Rules (not the simplified
29
+ * base from @sheet-i18n/typescript) so that RuleFn's TMessageId return type
30
+ * is preserved through the generic chain.
50
31
  */
51
- type RuleKey = string;
52
- type RuleFn<TSupportedLocales extends SupportedLocales, TLocaleSet extends LocaleSet, TI18nStore extends I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, TTypeSafe extends TypeSafe, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>, TValues = any> = (values: TValues, currentLocaleSet: ReturnType<TI18nStore['getCurrentLocaleSet']>) => TMessageId;
53
32
  type Plugins<TRules extends Rules = Rules> = {
54
33
  rules?: TRules;
55
34
  };
56
- type Rules<TSupportedLocales extends SupportedLocales = SupportedLocales, TLocaleSet extends LocaleSet = LocaleSet, TTypeSafe extends TypeSafe = TypeSafe, TI18nStore extends I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe> = I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe> = SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe> = SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>> = Record<RuleKey, RuleFn<TSupportedLocales, TLocaleSet, TI18nStore, TTypeSafe, TSheetTitle, TMessageId>>;
57
35
 
58
36
  type GetTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>> = {
59
37
  t: <TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>, TValues extends $TParams[0], TOpts extends $TParams[1], TDescriptor extends $TParams[2]>(id: TMessageId, values?: TValues, opts?: TOpts, _descriptor?: TDescriptor) => TTypeSafe extends true ? TMessageId : any;
@@ -76,7 +54,7 @@ type UseTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSe
76
54
  type IntlProviderProps<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean = false> = {
77
55
  i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>;
78
56
  currentLocale?: TSupportedLocales[number];
79
- children: Omit<React.ReactNode, 'bigint'> | ((currentLocale?: TSupportedLocales[number]) => React.ReactNode);
57
+ children: React.ReactNode | ((currentLocale?: TSupportedLocales[number]) => React.ReactNode);
80
58
  };
81
59
 
82
60
  declare function useLocaleStorage<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean = false>(localeStorageManager: LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>): {
@@ -87,7 +65,7 @@ interface StorageBasedIntlProviderProps<TSupportedLocales extends readonly strin
87
65
  i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>;
88
66
  storageManager: LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>;
89
67
  fallbackUI?: React.ReactNode;
90
- children: Omit<React.ReactNode, 'bigint'> | ((currentLocale?: TSupportedLocales[number]) => React.ReactNode);
68
+ children: React.ReactNode | ((currentLocale?: TSupportedLocales[number]) => React.ReactNode);
91
69
  }
92
70
 
93
71
  declare function createI18nContext<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TRules extends Rules, TTypeSafe extends boolean = false>(i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, plugins?: Plugins<TRules>): {
@@ -98,15 +76,8 @@ declare function createI18nContext<TSupportedLocales extends readonly string[],
98
76
  StorageBasedIntlProvider: ({ storageManager, children, }: Omit<StorageBasedIntlProviderProps<TSupportedLocales, TLocaleSet, TTypeSafe>, "i18nStore">) => react_jsx_runtime.JSX.Element;
99
77
  useTranslation: <TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>>(sheetTitle: TSheetTitle) => UseTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle, TRules>;
100
78
  getTranslation: <TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>>(sheetTitle: TSheetTitle) => GetTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle>;
101
- getLocaleStorageManager: (storageService?: IStorageService<string>) => LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>;
79
+ getLocaleStorageManager: (storageService?: IStorageService<string>) => _sheet_i18n_shared_service.LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>;
102
80
  useLocaleStorage: typeof useLocaleStorage;
103
81
  };
104
82
 
105
- declare class RuleService<TSupportedLocales extends SupportedLocales, TLocaleSet extends LocaleSet, TTypeSafe extends TypeSafe, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>> {
106
- private readonly i18nStore;
107
- constructor(i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>);
108
- createRule: <TValues extends $TParams[0]>(ruleFn: RuleFn<TSupportedLocales, TLocaleSet, I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, TTypeSafe, TSheetTitle, TMessageId, TValues>) => (values: TValues) => TMessageId;
109
- }
110
- declare const ruleFactory: <TSupportedLocales extends SupportedLocales, TLocaleSet extends LocaleSet, TTypeSafe extends TypeSafe>(i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>) => RuleService<TSupportedLocales, TLocaleSet, TTypeSafe, SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, SafeMessageId<TSupportedLocales, TLocaleSet, SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TTypeSafe>>;
111
-
112
- export { type GetTranslationReturn as GetTranslation, LocaleStorageManager, type Plugins, type Rules, type IStorageService as Storage, type StorageBasedIntlProviderProps, type UseTranslationReturn as UseTranslation, createI18nContext, ruleFactory };
83
+ export { type GetTranslationReturn as GetTranslation, type StorageBasedIntlProviderProps, type UseTranslationReturn as UseTranslation, createI18nContext };
package/dist/index.js CHANGED
@@ -39,16 +39,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
39
39
  // src/index.ts
40
40
  var src_exports = {};
41
41
  __export(src_exports, {
42
- LocaleStorageManager: () => LocaleStorageManager,
42
+ LocaleStorageManager: () => import_shared_service3.LocaleStorageManager,
43
43
  createI18nContext: () => createI18nContext,
44
- ruleFactory: () => ruleFactory
44
+ ruleFactory: () => import_shared_service3.ruleFactory
45
45
  });
46
46
  module.exports = __toCommonJS(src_exports);
47
47
  __reExport(src_exports, require("@sheet-i18n/core"), module.exports);
48
48
 
49
49
  // src/createI18nContext.tsx
50
- var import_shared_utils6 = require("@sheet-i18n/shared-utils");
50
+ var import_shared_utils5 = require("@sheet-i18n/shared-utils");
51
51
  var import_core = require("@sheet-i18n/core");
52
+ var import_shared_service2 = require("@sheet-i18n/shared-service");
52
53
 
53
54
  // src/Provider/IntlProvider.tsx
54
55
  var import_react_intl2 = require("react-intl");
@@ -211,6 +212,7 @@ function detectClientLanguage(i18nStore) {
211
212
 
212
213
  // src/Service/TranslationService.ts
213
214
  var import_shared_utils3 = require("@sheet-i18n/shared-utils");
215
+ var import_shared_service = require("@sheet-i18n/shared-service");
214
216
  var TranslationService = class {
215
217
  constructor(intlInstance) {
216
218
  this.intlInstance = intlInstance;
@@ -224,25 +226,8 @@ var TranslationService = class {
224
226
  });
225
227
  return targetTranslation;
226
228
  }
227
- /** helpers */
228
229
  interpolatePlaceholders(translationText, values) {
229
- if (!translationText.includes("{")) {
230
- return translationText;
231
- }
232
- if (!values || Object.keys(values).length === 0) {
233
- return translationText;
234
- }
235
- return translationText.replace(/\{([^{}]+)\}/g, (match, rawKey) => {
236
- const key = String(rawKey).trim();
237
- const value = values[key];
238
- if (value === null || value === void 0) {
239
- return match;
240
- }
241
- if (typeof value === "object" || typeof value === "function" || typeof value === "symbol") {
242
- return match;
243
- }
244
- return String(value);
245
- });
230
+ return (0, import_shared_service.interpolatePlaceholders)(translationText, values);
246
231
  }
247
232
  createDescriptor(id, _descriptor) {
248
233
  var _a;
@@ -300,13 +285,6 @@ function useTranslation({
300
285
  return { t };
301
286
  }
302
287
 
303
- // src/Errors.ts
304
- var import_errors = require("@sheet-i18n/errors");
305
- var InvalidI18nContextStateError = class extends import_errors.CustomError {
306
- };
307
- var IsNotInstanceOfI18nStoreError = class extends import_errors.CustomError {
308
- };
309
-
310
288
  // src/hooks/getTranslation.ts
311
289
  function getTranslation({
312
290
  sheetTitle,
@@ -351,148 +329,13 @@ function getTranslation({
351
329
  return { t };
352
330
  }
353
331
 
354
- // src/Service/StorageService.ts
355
- var import_shared_utils4 = require("@sheet-i18n/shared-utils");
356
- var StorageService = class {
357
- constructor(storage) {
358
- this.storage = null;
359
- this.isClientSide = typeof window !== "undefined";
360
- if (storage) {
361
- this.storage = this.validateStorage(storage);
362
- } else if (this.isClientSide) {
363
- this.storage = this.initializeWindowLocalStorage();
364
- }
365
- }
366
- validateStorage(storage) {
367
- const requiredMethods = [
368
- "getItem",
369
- "setItem",
370
- "removeItem",
371
- "clear"
372
- ];
373
- try {
374
- for (const method of requiredMethods) {
375
- if (typeof storage[method] !== "function") {
376
- throw new Error(
377
- `Invalid storage object: missing required method '${method}'`
378
- );
379
- }
380
- }
381
- return storage;
382
- } catch (error) {
383
- console.error(error);
384
- return null;
385
- }
386
- }
387
- initializeWindowLocalStorage() {
388
- try {
389
- const testKey = "__storage_test__";
390
- localStorage.setItem(testKey, "test");
391
- localStorage.removeItem(testKey);
392
- return localStorage;
393
- } catch (error) {
394
- console.warn("Window LocalStorage is not available:", error);
395
- return null;
396
- }
397
- }
398
- getItem(key) {
399
- if (!this.storage || !key) {
400
- return "";
401
- }
402
- try {
403
- return this.storage.getItem(key);
404
- } catch (error) {
405
- console.error(`Failed to get item with key "${key}":`, error);
406
- return "";
407
- }
408
- }
409
- setItem(key, value) {
410
- if (!this.storage || !key) {
411
- return false;
412
- }
413
- try {
414
- this.storage.setItem(key, value);
415
- return true;
416
- } catch (error) {
417
- console.error(`Failed to set item with key "${key}":`, error);
418
- return false;
419
- }
420
- }
421
- removeItem(key) {
422
- if (!this.storage || !key) {
423
- return false;
424
- }
425
- try {
426
- this.storage.removeItem(key);
427
- return true;
428
- } catch (error) {
429
- console.error(`Failed to remove item with key "${key}":`, error);
430
- return false;
431
- }
432
- }
433
- clear() {
434
- if (!this.storage) {
435
- return false;
436
- }
437
- try {
438
- this.storage.clear();
439
- return true;
440
- } catch (error) {
441
- console.error("Failed to clear storage:", error);
442
- return false;
443
- }
444
- }
445
- };
446
- var LocaleStorageManager = class {
447
- constructor(storageService, i18nStore, localeStorageKey = "sheet-i18n-locale") {
448
- this.storageService = storageService;
449
- this.i18nStore = i18nStore;
450
- this.localeStorageKey = localeStorageKey;
451
- this.observerManager = new import_shared_utils4.ObserverManager();
452
- this.initializeCurrentLocale = () => {
453
- var _a, _b;
454
- const storedLocale = this.storageService.getItem(this.localeStorageKey);
455
- if (!storedLocale || storedLocale === "") {
456
- const defaultLocale = this.i18nStore.currentLocale || this.i18nStore.defaultLocale;
457
- this.storageService.setItem(this.localeStorageKey, defaultLocale);
458
- }
459
- (_b = (_a = this.i18nStore) == null ? void 0 : _a.observerManager) == null ? void 0 : _b.addListener({
460
- listenerId: this.localeStorageKey,
461
- listener: (newLocale) => {
462
- this.storageService.setItem(this.localeStorageKey, newLocale);
463
- }
464
- });
465
- };
466
- this.getLocale = () => {
467
- const stored = this.storageService.getItem(this.localeStorageKey);
468
- if (!stored || stored === "") {
469
- return this.i18nStore.defaultLocale;
470
- }
471
- return stored;
472
- };
473
- this.setLocale = (locale) => {
474
- this.storageService.setItem(this.localeStorageKey, locale);
475
- this.observerManager.notify(locale);
476
- };
477
- this.initializeCurrentLocale();
478
- }
479
- };
480
- var getLocaleStorageManager = (i18nStore, storage) => {
481
- const localeStorageService = new StorageService(storage);
482
- const localeStorageManager = new LocaleStorageManager(
483
- localeStorageService,
484
- i18nStore
485
- );
486
- return localeStorageManager;
487
- };
488
-
489
332
  // src/hooks/useLocaleStorage.ts
490
333
  var import_react3 = require("react");
491
334
  var LISTENER_ID = "LOCALE_STORAGE_LISTENER_ID";
492
335
  function useLocaleStorage(localeStorageManager) {
493
336
  const [locale, setLocale] = (0, import_react3.useState)();
494
337
  (0, import_react3.useEffect)(() => {
495
- const storedLocale = localeStorageManager.getLocale();
338
+ const storedLocale = localeStorageManager.currentLocale;
496
339
  setLocale(storedLocale);
497
340
  }, []);
498
341
  (0, import_react3.useEffect)(() => {
@@ -512,7 +355,7 @@ function useLocaleStorage(localeStorageManager) {
512
355
  }
513
356
 
514
357
  // src/Provider/StorageBasedIntlProvider.tsx
515
- var import_shared_utils5 = require("@sheet-i18n/shared-utils");
358
+ var import_shared_utils4 = require("@sheet-i18n/shared-utils");
516
359
  var import_jsx_runtime2 = require("react/jsx-runtime");
517
360
  function StorageBasedIntlProvider({
518
361
  i18nStore,
@@ -521,7 +364,7 @@ function StorageBasedIntlProvider({
521
364
  children: childrenFromProps
522
365
  }) {
523
366
  const { locale } = useLocaleStorage(storageManager);
524
- if (import_shared_utils5.validator.isNullish(locale)) {
367
+ if (import_shared_utils4.validator.isNullish(locale)) {
525
368
  return fallbackUI;
526
369
  }
527
370
  const children = typeof childrenFromProps === "function" ? childrenFromProps(locale) : childrenFromProps;
@@ -531,13 +374,13 @@ function StorageBasedIntlProvider({
531
374
  // src/createI18nContext.tsx
532
375
  var import_jsx_runtime3 = require("react/jsx-runtime");
533
376
  function createI18nContext(i18nStore, plugins) {
534
- if (import_shared_utils6.validator.isNullish(i18nStore)) {
535
- throw new InvalidI18nContextStateError(
377
+ if (import_shared_utils5.validator.isNullish(i18nStore)) {
378
+ throw new import_shared_service2.InvalidI18nContextStateError(
536
379
  "\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
537
380
  );
538
381
  }
539
382
  if (i18nStore instanceof import_core.I18nStore !== true) {
540
- throw new IsNotInstanceOfI18nStoreError(
383
+ throw new import_shared_service2.IsNotInstanceOfI18nStoreError(
541
384
  "\u26A0\uFE0F The provided i18nStore is invalid. Please ensure a store instance you passed is an instance of I18nStore."
542
385
  );
543
386
  }
@@ -567,7 +410,7 @@ function createI18nContext(i18nStore, plugins) {
567
410
  const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore, rules });
568
411
  const getTranslationImpl = (sheetTitle) => getTranslation({ sheetTitle, i18nStore });
569
412
  const getLocaleStorageManagerImpl = (storageService) => {
570
- return getLocaleStorageManager(i18nStore, storageService);
413
+ return (0, import_shared_service2.getLocaleStorageManager)(i18nStore, storageService);
571
414
  };
572
415
  return {
573
416
  IntlProvider: IntlProviderImpl,
@@ -579,21 +422,8 @@ function createI18nContext(i18nStore, plugins) {
579
422
  };
580
423
  }
581
424
 
582
- // src/Service/RuleService.ts
583
- var RuleService = class {
584
- constructor(i18nStore) {
585
- this.i18nStore = i18nStore;
586
- this.createRule = (ruleFn) => {
587
- return (values) => {
588
- const currentLocaleSet = this.i18nStore.getCurrentLocaleSet();
589
- return ruleFn(values, currentLocaleSet);
590
- };
591
- };
592
- }
593
- };
594
- var ruleFactory = (i18nStore) => {
595
- return new RuleService(i18nStore);
596
- };
425
+ // src/index.ts
426
+ var import_shared_service3 = require("@sheet-i18n/shared-service");
597
427
  // Annotate the CommonJS export names for ESM import in node:
598
428
  0 && (module.exports = {
599
429
  LocaleStorageManager,
package/dist/index.mjs CHANGED
@@ -25,6 +25,11 @@ export * from "@sheet-i18n/core";
25
25
  // src/createI18nContext.tsx
26
26
  import { validator as validator5 } from "@sheet-i18n/shared-utils";
27
27
  import { I18nStore } from "@sheet-i18n/core";
28
+ import {
29
+ InvalidI18nContextStateError,
30
+ IsNotInstanceOfI18nStoreError,
31
+ getLocaleStorageManager
32
+ } from "@sheet-i18n/shared-service";
28
33
 
29
34
  // src/Provider/IntlProvider.tsx
30
35
  import { IntlProvider as ReactIntlProvider } from "react-intl";
@@ -187,6 +192,7 @@ function detectClientLanguage(i18nStore) {
187
192
 
188
193
  // src/Service/TranslationService.ts
189
194
  import { validator as validator3 } from "@sheet-i18n/shared-utils";
195
+ import { interpolatePlaceholders } from "@sheet-i18n/shared-service";
190
196
  var TranslationService = class {
191
197
  constructor(intlInstance) {
192
198
  this.intlInstance = intlInstance;
@@ -200,25 +206,8 @@ var TranslationService = class {
200
206
  });
201
207
  return targetTranslation;
202
208
  }
203
- /** helpers */
204
209
  interpolatePlaceholders(translationText, values) {
205
- if (!translationText.includes("{")) {
206
- return translationText;
207
- }
208
- if (!values || Object.keys(values).length === 0) {
209
- return translationText;
210
- }
211
- return translationText.replace(/\{([^{}]+)\}/g, (match, rawKey) => {
212
- const key = String(rawKey).trim();
213
- const value = values[key];
214
- if (value === null || value === void 0) {
215
- return match;
216
- }
217
- if (typeof value === "object" || typeof value === "function" || typeof value === "symbol") {
218
- return match;
219
- }
220
- return String(value);
221
- });
210
+ return interpolatePlaceholders(translationText, values);
222
211
  }
223
212
  createDescriptor(id, _descriptor) {
224
213
  var _a;
@@ -276,13 +265,6 @@ function useTranslation({
276
265
  return { t };
277
266
  }
278
267
 
279
- // src/Errors.ts
280
- import { CustomError } from "@sheet-i18n/errors";
281
- var InvalidI18nContextStateError = class extends CustomError {
282
- };
283
- var IsNotInstanceOfI18nStoreError = class extends CustomError {
284
- };
285
-
286
268
  // src/hooks/getTranslation.ts
287
269
  function getTranslation({
288
270
  sheetTitle,
@@ -327,148 +309,13 @@ function getTranslation({
327
309
  return { t };
328
310
  }
329
311
 
330
- // src/Service/StorageService.ts
331
- import { ObserverManager } from "@sheet-i18n/shared-utils";
332
- var StorageService = class {
333
- constructor(storage) {
334
- this.storage = null;
335
- this.isClientSide = typeof window !== "undefined";
336
- if (storage) {
337
- this.storage = this.validateStorage(storage);
338
- } else if (this.isClientSide) {
339
- this.storage = this.initializeWindowLocalStorage();
340
- }
341
- }
342
- validateStorage(storage) {
343
- const requiredMethods = [
344
- "getItem",
345
- "setItem",
346
- "removeItem",
347
- "clear"
348
- ];
349
- try {
350
- for (const method of requiredMethods) {
351
- if (typeof storage[method] !== "function") {
352
- throw new Error(
353
- `Invalid storage object: missing required method '${method}'`
354
- );
355
- }
356
- }
357
- return storage;
358
- } catch (error) {
359
- console.error(error);
360
- return null;
361
- }
362
- }
363
- initializeWindowLocalStorage() {
364
- try {
365
- const testKey = "__storage_test__";
366
- localStorage.setItem(testKey, "test");
367
- localStorage.removeItem(testKey);
368
- return localStorage;
369
- } catch (error) {
370
- console.warn("Window LocalStorage is not available:", error);
371
- return null;
372
- }
373
- }
374
- getItem(key) {
375
- if (!this.storage || !key) {
376
- return "";
377
- }
378
- try {
379
- return this.storage.getItem(key);
380
- } catch (error) {
381
- console.error(`Failed to get item with key "${key}":`, error);
382
- return "";
383
- }
384
- }
385
- setItem(key, value) {
386
- if (!this.storage || !key) {
387
- return false;
388
- }
389
- try {
390
- this.storage.setItem(key, value);
391
- return true;
392
- } catch (error) {
393
- console.error(`Failed to set item with key "${key}":`, error);
394
- return false;
395
- }
396
- }
397
- removeItem(key) {
398
- if (!this.storage || !key) {
399
- return false;
400
- }
401
- try {
402
- this.storage.removeItem(key);
403
- return true;
404
- } catch (error) {
405
- console.error(`Failed to remove item with key "${key}":`, error);
406
- return false;
407
- }
408
- }
409
- clear() {
410
- if (!this.storage) {
411
- return false;
412
- }
413
- try {
414
- this.storage.clear();
415
- return true;
416
- } catch (error) {
417
- console.error("Failed to clear storage:", error);
418
- return false;
419
- }
420
- }
421
- };
422
- var LocaleStorageManager = class {
423
- constructor(storageService, i18nStore, localeStorageKey = "sheet-i18n-locale") {
424
- this.storageService = storageService;
425
- this.i18nStore = i18nStore;
426
- this.localeStorageKey = localeStorageKey;
427
- this.observerManager = new ObserverManager();
428
- this.initializeCurrentLocale = () => {
429
- var _a, _b;
430
- const storedLocale = this.storageService.getItem(this.localeStorageKey);
431
- if (!storedLocale || storedLocale === "") {
432
- const defaultLocale = this.i18nStore.currentLocale || this.i18nStore.defaultLocale;
433
- this.storageService.setItem(this.localeStorageKey, defaultLocale);
434
- }
435
- (_b = (_a = this.i18nStore) == null ? void 0 : _a.observerManager) == null ? void 0 : _b.addListener({
436
- listenerId: this.localeStorageKey,
437
- listener: (newLocale) => {
438
- this.storageService.setItem(this.localeStorageKey, newLocale);
439
- }
440
- });
441
- };
442
- this.getLocale = () => {
443
- const stored = this.storageService.getItem(this.localeStorageKey);
444
- if (!stored || stored === "") {
445
- return this.i18nStore.defaultLocale;
446
- }
447
- return stored;
448
- };
449
- this.setLocale = (locale) => {
450
- this.storageService.setItem(this.localeStorageKey, locale);
451
- this.observerManager.notify(locale);
452
- };
453
- this.initializeCurrentLocale();
454
- }
455
- };
456
- var getLocaleStorageManager = (i18nStore, storage) => {
457
- const localeStorageService = new StorageService(storage);
458
- const localeStorageManager = new LocaleStorageManager(
459
- localeStorageService,
460
- i18nStore
461
- );
462
- return localeStorageManager;
463
- };
464
-
465
312
  // src/hooks/useLocaleStorage.ts
466
313
  import { useEffect as useEffect2, useState as useState2 } from "react";
467
314
  var LISTENER_ID = "LOCALE_STORAGE_LISTENER_ID";
468
315
  function useLocaleStorage(localeStorageManager) {
469
316
  const [locale, setLocale] = useState2();
470
317
  useEffect2(() => {
471
- const storedLocale = localeStorageManager.getLocale();
318
+ const storedLocale = localeStorageManager.currentLocale;
472
319
  setLocale(storedLocale);
473
320
  }, []);
474
321
  useEffect2(() => {
@@ -555,21 +402,8 @@ function createI18nContext(i18nStore, plugins) {
555
402
  };
556
403
  }
557
404
 
558
- // src/Service/RuleService.ts
559
- var RuleService = class {
560
- constructor(i18nStore) {
561
- this.i18nStore = i18nStore;
562
- this.createRule = (ruleFn) => {
563
- return (values) => {
564
- const currentLocaleSet = this.i18nStore.getCurrentLocaleSet();
565
- return ruleFn(values, currentLocaleSet);
566
- };
567
- };
568
- }
569
- };
570
- var ruleFactory = (i18nStore) => {
571
- return new RuleService(i18nStore);
572
- };
405
+ // src/index.ts
406
+ import { ruleFactory, LocaleStorageManager } from "@sheet-i18n/shared-service";
573
407
  export {
574
408
  LocaleStorageManager,
575
409
  createI18nContext,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sheet-i18n/react",
3
- "version": "1.6.5",
3
+ "version": "1.7.1",
4
4
  "description": "i18n client logic based on react",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -36,9 +36,11 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "react-intl": "^7.0.4",
39
+ "@sheet-i18n/core": "1.7.0",
40
+ "@sheet-i18n/shared-service": "0.2.0",
39
41
  "@sheet-i18n/errors": "1.8.2",
40
- "@sheet-i18n/core": "1.6.3",
41
- "@sheet-i18n/shared-utils": "1.8.3"
42
+ "@sheet-i18n/shared-utils": "1.8.3",
43
+ "@sheet-i18n/typescript": "0.4.0"
42
44
  },
43
45
  "peerDependencies": {
44
46
  "react": "^18 || ^19 || ^20 || ^21",