@sheet-i18n/react 1.6.4 → 1.7.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.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;
@@ -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;
@@ -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,7 +226,9 @@ var TranslationService = class {
224
226
  });
225
227
  return targetTranslation;
226
228
  }
227
- /** helpers */
229
+ interpolatePlaceholders(translationText, values) {
230
+ return (0, import_shared_service.interpolatePlaceholders)(translationText, values);
231
+ }
228
232
  createDescriptor(id, _descriptor) {
229
233
  var _a;
230
234
  return __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
@@ -274,18 +278,13 @@ function useTranslation({
274
278
  const ruleFn = rules == null ? void 0 : rules[ruleKey];
275
279
  if (!ruleFn) return ruleKey;
276
280
  const messageId = ruleFn(values, {});
277
- return translationService.translate(messageId, values);
281
+ const translationValue = translationService.translate(messageId, values);
282
+ const interpolatedTranslationValue = translationService.interpolatePlaceholders(translationValue, values);
283
+ return interpolatedTranslationValue;
278
284
  };
279
285
  return { t };
280
286
  }
281
287
 
282
- // src/Errors.ts
283
- var import_errors = require("@sheet-i18n/errors");
284
- var InvalidI18nContextStateError = class extends import_errors.CustomError {
285
- };
286
- var IsNotInstanceOfI18nStoreError = class extends import_errors.CustomError {
287
- };
288
-
289
288
  // src/hooks/getTranslation.ts
290
289
  function getTranslation({
291
290
  sheetTitle,
@@ -330,148 +329,13 @@ function getTranslation({
330
329
  return { t };
331
330
  }
332
331
 
333
- // src/Service/StorageService.ts
334
- var import_shared_utils4 = require("@sheet-i18n/shared-utils");
335
- var StorageService = class {
336
- constructor(storage) {
337
- this.storage = null;
338
- this.isClientSide = typeof window !== "undefined";
339
- if (storage) {
340
- this.storage = this.validateStorage(storage);
341
- } else if (this.isClientSide) {
342
- this.storage = this.initializeWindowLocalStorage();
343
- }
344
- }
345
- validateStorage(storage) {
346
- const requiredMethods = [
347
- "getItem",
348
- "setItem",
349
- "removeItem",
350
- "clear"
351
- ];
352
- try {
353
- for (const method of requiredMethods) {
354
- if (typeof storage[method] !== "function") {
355
- throw new Error(
356
- `Invalid storage object: missing required method '${method}'`
357
- );
358
- }
359
- }
360
- return storage;
361
- } catch (error) {
362
- console.error(error);
363
- return null;
364
- }
365
- }
366
- initializeWindowLocalStorage() {
367
- try {
368
- const testKey = "__storage_test__";
369
- localStorage.setItem(testKey, "test");
370
- localStorage.removeItem(testKey);
371
- return localStorage;
372
- } catch (error) {
373
- console.warn("Window LocalStorage is not available:", error);
374
- return null;
375
- }
376
- }
377
- getItem(key) {
378
- if (!this.storage || !key) {
379
- return "";
380
- }
381
- try {
382
- return this.storage.getItem(key);
383
- } catch (error) {
384
- console.error(`Failed to get item with key "${key}":`, error);
385
- return "";
386
- }
387
- }
388
- setItem(key, value) {
389
- if (!this.storage || !key) {
390
- return false;
391
- }
392
- try {
393
- this.storage.setItem(key, value);
394
- return true;
395
- } catch (error) {
396
- console.error(`Failed to set item with key "${key}":`, error);
397
- return false;
398
- }
399
- }
400
- removeItem(key) {
401
- if (!this.storage || !key) {
402
- return false;
403
- }
404
- try {
405
- this.storage.removeItem(key);
406
- return true;
407
- } catch (error) {
408
- console.error(`Failed to remove item with key "${key}":`, error);
409
- return false;
410
- }
411
- }
412
- clear() {
413
- if (!this.storage) {
414
- return false;
415
- }
416
- try {
417
- this.storage.clear();
418
- return true;
419
- } catch (error) {
420
- console.error("Failed to clear storage:", error);
421
- return false;
422
- }
423
- }
424
- };
425
- var LocaleStorageManager = class {
426
- constructor(storageService, i18nStore, localeStorageKey = "sheet-i18n-locale") {
427
- this.storageService = storageService;
428
- this.i18nStore = i18nStore;
429
- this.localeStorageKey = localeStorageKey;
430
- this.observerManager = new import_shared_utils4.ObserverManager();
431
- this.initializeCurrentLocale = () => {
432
- var _a, _b;
433
- const storedLocale = this.storageService.getItem(this.localeStorageKey);
434
- if (!storedLocale || storedLocale === "") {
435
- const defaultLocale = this.i18nStore.currentLocale || this.i18nStore.defaultLocale;
436
- this.storageService.setItem(this.localeStorageKey, defaultLocale);
437
- }
438
- (_b = (_a = this.i18nStore) == null ? void 0 : _a.observerManager) == null ? void 0 : _b.addListener({
439
- listenerId: this.localeStorageKey,
440
- listener: (newLocale) => {
441
- this.storageService.setItem(this.localeStorageKey, newLocale);
442
- }
443
- });
444
- };
445
- this.getLocale = () => {
446
- const stored = this.storageService.getItem(this.localeStorageKey);
447
- if (!stored || stored === "") {
448
- return this.i18nStore.defaultLocale;
449
- }
450
- return stored;
451
- };
452
- this.setLocale = (locale) => {
453
- this.storageService.setItem(this.localeStorageKey, locale);
454
- this.observerManager.notify(locale);
455
- };
456
- this.initializeCurrentLocale();
457
- }
458
- };
459
- var getLocaleStorageManager = (i18nStore, storage) => {
460
- const localeStorageService = new StorageService(storage);
461
- const localeStorageManager = new LocaleStorageManager(
462
- localeStorageService,
463
- i18nStore
464
- );
465
- return localeStorageManager;
466
- };
467
-
468
332
  // src/hooks/useLocaleStorage.ts
469
333
  var import_react3 = require("react");
470
334
  var LISTENER_ID = "LOCALE_STORAGE_LISTENER_ID";
471
335
  function useLocaleStorage(localeStorageManager) {
472
336
  const [locale, setLocale] = (0, import_react3.useState)();
473
337
  (0, import_react3.useEffect)(() => {
474
- const storedLocale = localeStorageManager.getLocale();
338
+ const storedLocale = localeStorageManager.currentLocale;
475
339
  setLocale(storedLocale);
476
340
  }, []);
477
341
  (0, import_react3.useEffect)(() => {
@@ -491,7 +355,7 @@ function useLocaleStorage(localeStorageManager) {
491
355
  }
492
356
 
493
357
  // src/Provider/StorageBasedIntlProvider.tsx
494
- var import_shared_utils5 = require("@sheet-i18n/shared-utils");
358
+ var import_shared_utils4 = require("@sheet-i18n/shared-utils");
495
359
  var import_jsx_runtime2 = require("react/jsx-runtime");
496
360
  function StorageBasedIntlProvider({
497
361
  i18nStore,
@@ -500,7 +364,7 @@ function StorageBasedIntlProvider({
500
364
  children: childrenFromProps
501
365
  }) {
502
366
  const { locale } = useLocaleStorage(storageManager);
503
- if (import_shared_utils5.validator.isNullish(locale)) {
367
+ if (import_shared_utils4.validator.isNullish(locale)) {
504
368
  return fallbackUI;
505
369
  }
506
370
  const children = typeof childrenFromProps === "function" ? childrenFromProps(locale) : childrenFromProps;
@@ -510,13 +374,13 @@ function StorageBasedIntlProvider({
510
374
  // src/createI18nContext.tsx
511
375
  var import_jsx_runtime3 = require("react/jsx-runtime");
512
376
  function createI18nContext(i18nStore, plugins) {
513
- if (import_shared_utils6.validator.isNullish(i18nStore)) {
514
- throw new InvalidI18nContextStateError(
377
+ if (import_shared_utils5.validator.isNullish(i18nStore)) {
378
+ throw new import_shared_service2.InvalidI18nContextStateError(
515
379
  "\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
516
380
  );
517
381
  }
518
382
  if (i18nStore instanceof import_core.I18nStore !== true) {
519
- throw new IsNotInstanceOfI18nStoreError(
383
+ throw new import_shared_service2.IsNotInstanceOfI18nStoreError(
520
384
  "\u26A0\uFE0F The provided i18nStore is invalid. Please ensure a store instance you passed is an instance of I18nStore."
521
385
  );
522
386
  }
@@ -546,7 +410,7 @@ function createI18nContext(i18nStore, plugins) {
546
410
  const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore, rules });
547
411
  const getTranslationImpl = (sheetTitle) => getTranslation({ sheetTitle, i18nStore });
548
412
  const getLocaleStorageManagerImpl = (storageService) => {
549
- return getLocaleStorageManager(i18nStore, storageService);
413
+ return (0, import_shared_service2.getLocaleStorageManager)(i18nStore, storageService);
550
414
  };
551
415
  return {
552
416
  IntlProvider: IntlProviderImpl,
@@ -558,19 +422,8 @@ function createI18nContext(i18nStore, plugins) {
558
422
  };
559
423
  }
560
424
 
561
- // src/Service/RuleService.ts
562
- var RuleService = class {
563
- constructor(i18nStore) {
564
- this.i18nStore = i18nStore;
565
- this.createRule = (ruleFn) => {
566
- const currentLocaleSet = this.i18nStore.getCurrentLocaleSet();
567
- return (values) => ruleFn(values, currentLocaleSet);
568
- };
569
- }
570
- };
571
- var ruleFactory = (i18nStore) => {
572
- return new RuleService(i18nStore);
573
- };
425
+ // src/index.ts
426
+ var import_shared_service3 = require("@sheet-i18n/shared-service");
574
427
  // Annotate the CommonJS export names for ESM import in node:
575
428
  0 && (module.exports = {
576
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,7 +206,9 @@ var TranslationService = class {
200
206
  });
201
207
  return targetTranslation;
202
208
  }
203
- /** helpers */
209
+ interpolatePlaceholders(translationText, values) {
210
+ return interpolatePlaceholders(translationText, values);
211
+ }
204
212
  createDescriptor(id, _descriptor) {
205
213
  var _a;
206
214
  return __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
@@ -250,18 +258,13 @@ function useTranslation({
250
258
  const ruleFn = rules == null ? void 0 : rules[ruleKey];
251
259
  if (!ruleFn) return ruleKey;
252
260
  const messageId = ruleFn(values, {});
253
- return translationService.translate(messageId, values);
261
+ const translationValue = translationService.translate(messageId, values);
262
+ const interpolatedTranslationValue = translationService.interpolatePlaceholders(translationValue, values);
263
+ return interpolatedTranslationValue;
254
264
  };
255
265
  return { t };
256
266
  }
257
267
 
258
- // src/Errors.ts
259
- import { CustomError } from "@sheet-i18n/errors";
260
- var InvalidI18nContextStateError = class extends CustomError {
261
- };
262
- var IsNotInstanceOfI18nStoreError = class extends CustomError {
263
- };
264
-
265
268
  // src/hooks/getTranslation.ts
266
269
  function getTranslation({
267
270
  sheetTitle,
@@ -306,148 +309,13 @@ function getTranslation({
306
309
  return { t };
307
310
  }
308
311
 
309
- // src/Service/StorageService.ts
310
- import { ObserverManager } from "@sheet-i18n/shared-utils";
311
- var StorageService = class {
312
- constructor(storage) {
313
- this.storage = null;
314
- this.isClientSide = typeof window !== "undefined";
315
- if (storage) {
316
- this.storage = this.validateStorage(storage);
317
- } else if (this.isClientSide) {
318
- this.storage = this.initializeWindowLocalStorage();
319
- }
320
- }
321
- validateStorage(storage) {
322
- const requiredMethods = [
323
- "getItem",
324
- "setItem",
325
- "removeItem",
326
- "clear"
327
- ];
328
- try {
329
- for (const method of requiredMethods) {
330
- if (typeof storage[method] !== "function") {
331
- throw new Error(
332
- `Invalid storage object: missing required method '${method}'`
333
- );
334
- }
335
- }
336
- return storage;
337
- } catch (error) {
338
- console.error(error);
339
- return null;
340
- }
341
- }
342
- initializeWindowLocalStorage() {
343
- try {
344
- const testKey = "__storage_test__";
345
- localStorage.setItem(testKey, "test");
346
- localStorage.removeItem(testKey);
347
- return localStorage;
348
- } catch (error) {
349
- console.warn("Window LocalStorage is not available:", error);
350
- return null;
351
- }
352
- }
353
- getItem(key) {
354
- if (!this.storage || !key) {
355
- return "";
356
- }
357
- try {
358
- return this.storage.getItem(key);
359
- } catch (error) {
360
- console.error(`Failed to get item with key "${key}":`, error);
361
- return "";
362
- }
363
- }
364
- setItem(key, value) {
365
- if (!this.storage || !key) {
366
- return false;
367
- }
368
- try {
369
- this.storage.setItem(key, value);
370
- return true;
371
- } catch (error) {
372
- console.error(`Failed to set item with key "${key}":`, error);
373
- return false;
374
- }
375
- }
376
- removeItem(key) {
377
- if (!this.storage || !key) {
378
- return false;
379
- }
380
- try {
381
- this.storage.removeItem(key);
382
- return true;
383
- } catch (error) {
384
- console.error(`Failed to remove item with key "${key}":`, error);
385
- return false;
386
- }
387
- }
388
- clear() {
389
- if (!this.storage) {
390
- return false;
391
- }
392
- try {
393
- this.storage.clear();
394
- return true;
395
- } catch (error) {
396
- console.error("Failed to clear storage:", error);
397
- return false;
398
- }
399
- }
400
- };
401
- var LocaleStorageManager = class {
402
- constructor(storageService, i18nStore, localeStorageKey = "sheet-i18n-locale") {
403
- this.storageService = storageService;
404
- this.i18nStore = i18nStore;
405
- this.localeStorageKey = localeStorageKey;
406
- this.observerManager = new ObserverManager();
407
- this.initializeCurrentLocale = () => {
408
- var _a, _b;
409
- const storedLocale = this.storageService.getItem(this.localeStorageKey);
410
- if (!storedLocale || storedLocale === "") {
411
- const defaultLocale = this.i18nStore.currentLocale || this.i18nStore.defaultLocale;
412
- this.storageService.setItem(this.localeStorageKey, defaultLocale);
413
- }
414
- (_b = (_a = this.i18nStore) == null ? void 0 : _a.observerManager) == null ? void 0 : _b.addListener({
415
- listenerId: this.localeStorageKey,
416
- listener: (newLocale) => {
417
- this.storageService.setItem(this.localeStorageKey, newLocale);
418
- }
419
- });
420
- };
421
- this.getLocale = () => {
422
- const stored = this.storageService.getItem(this.localeStorageKey);
423
- if (!stored || stored === "") {
424
- return this.i18nStore.defaultLocale;
425
- }
426
- return stored;
427
- };
428
- this.setLocale = (locale) => {
429
- this.storageService.setItem(this.localeStorageKey, locale);
430
- this.observerManager.notify(locale);
431
- };
432
- this.initializeCurrentLocale();
433
- }
434
- };
435
- var getLocaleStorageManager = (i18nStore, storage) => {
436
- const localeStorageService = new StorageService(storage);
437
- const localeStorageManager = new LocaleStorageManager(
438
- localeStorageService,
439
- i18nStore
440
- );
441
- return localeStorageManager;
442
- };
443
-
444
312
  // src/hooks/useLocaleStorage.ts
445
313
  import { useEffect as useEffect2, useState as useState2 } from "react";
446
314
  var LISTENER_ID = "LOCALE_STORAGE_LISTENER_ID";
447
315
  function useLocaleStorage(localeStorageManager) {
448
316
  const [locale, setLocale] = useState2();
449
317
  useEffect2(() => {
450
- const storedLocale = localeStorageManager.getLocale();
318
+ const storedLocale = localeStorageManager.currentLocale;
451
319
  setLocale(storedLocale);
452
320
  }, []);
453
321
  useEffect2(() => {
@@ -534,19 +402,8 @@ function createI18nContext(i18nStore, plugins) {
534
402
  };
535
403
  }
536
404
 
537
- // src/Service/RuleService.ts
538
- var RuleService = class {
539
- constructor(i18nStore) {
540
- this.i18nStore = i18nStore;
541
- this.createRule = (ruleFn) => {
542
- const currentLocaleSet = this.i18nStore.getCurrentLocaleSet();
543
- return (values) => ruleFn(values, currentLocaleSet);
544
- };
545
- }
546
- };
547
- var ruleFactory = (i18nStore) => {
548
- return new RuleService(i18nStore);
549
- };
405
+ // src/index.ts
406
+ import { ruleFactory, LocaleStorageManager } from "@sheet-i18n/shared-service";
550
407
  export {
551
408
  LocaleStorageManager,
552
409
  createI18nContext,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sheet-i18n/react",
3
- "version": "1.6.4",
3
+ "version": "1.7.0",
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.6.3",
40
- "@sheet-i18n/errors": "1.8.2",
41
- "@sheet-i18n/shared-utils": "1.8.3"
39
+ "@sheet-i18n/core": "1.7.0",
40
+ "@sheet-i18n/shared-service": "0.2.0",
41
+ "@sheet-i18n/typescript": "0.4.0",
42
+ "@sheet-i18n/shared-utils": "1.8.3",
43
+ "@sheet-i18n/errors": "1.8.2"
42
44
  },
43
45
  "peerDependencies": {
44
46
  "react": "^18 || ^19 || ^20 || ^21",