@sheet-i18n/react-client 1.5.3 → 1.6.0-canary.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 +26 -6
- package/dist/index.d.ts +26 -6
- package/dist/index.js +42 -25
- package/dist/index.mjs +40 -24
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -40,7 +40,16 @@ type SafeMessageId<TSupportedLocales extends readonly string[], TLocaleSet exten
|
|
|
40
40
|
* Resolves to valid sheet titles from the locale set
|
|
41
41
|
* or defaults to string if type safety is off or keys are not available.
|
|
42
42
|
*/
|
|
43
|
-
type
|
|
43
|
+
type SupportedLocales = readonly string[];
|
|
44
|
+
type LocaleSet = Partial<Record<SupportedLocales[number], Record<string, any>>>;
|
|
45
|
+
type TypeSafe = boolean;
|
|
46
|
+
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;
|
|
47
|
+
/**
|
|
48
|
+
* Plugin types
|
|
49
|
+
*/
|
|
50
|
+
type RuleKey = string;
|
|
51
|
+
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;
|
|
52
|
+
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>>;
|
|
44
53
|
|
|
45
54
|
type GetTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>> = {
|
|
46
55
|
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;
|
|
@@ -51,11 +60,12 @@ type GetTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSe
|
|
|
51
60
|
};
|
|
52
61
|
};
|
|
53
62
|
|
|
54
|
-
type UseTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Partial<Record<TSupportedLocales[number], Record<string, any>>>>, TTypeSafe extends boolean, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe
|
|
63
|
+
type UseTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Partial<Record<TSupportedLocales[number], Record<string, any>>>>, TTypeSafe extends boolean, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TRules extends Rules> = {
|
|
55
64
|
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;
|
|
56
65
|
} & {
|
|
57
66
|
t: {
|
|
58
67
|
dynamic: <TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>, TValues extends $TParams[0], TOpts extends $TParams[1], TDescriptor extends $TParams[2]>(id: string, values?: TValues, opts?: TOpts, _descriptor?: TDescriptor) => TTypeSafe extends true ? string : any;
|
|
68
|
+
rule: <TRuleKey extends keyof TRules, TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>>(ruleKey: TRuleKey, values?: Parameters<TRules[TRuleKey]>[0]) => TTypeSafe extends true ? TMessageId : any;
|
|
59
69
|
};
|
|
60
70
|
};
|
|
61
71
|
|
|
@@ -76,16 +86,26 @@ interface StorageBasedIntlProviderProps<TSupportedLocales extends readonly strin
|
|
|
76
86
|
children: React.ReactNode;
|
|
77
87
|
}
|
|
78
88
|
|
|
79
|
-
|
|
89
|
+
interface Plugins<TRules extends Rules> {
|
|
90
|
+
rules?: TRules;
|
|
91
|
+
}
|
|
92
|
+
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>): {
|
|
80
93
|
IntlProvider: ({ currentLocale, children, }: Omit<IntlProviderProps<TSupportedLocales, TLocaleSet>, "currentLocale" | "i18nStore"> & {
|
|
81
94
|
currentLocale?: string;
|
|
82
95
|
children?: React.ReactNode;
|
|
83
96
|
}) => react_jsx_runtime.JSX.Element;
|
|
84
97
|
StorageBasedIntlProvider: ({ storageManager, children, }: Omit<StorageBasedIntlProviderProps<TSupportedLocales, TLocaleSet, TTypeSafe>, "i18nStore">) => react_jsx_runtime.JSX.Element;
|
|
85
|
-
useTranslation: <TSheetTitle extends
|
|
86
|
-
getTranslation: <TSheetTitle extends
|
|
98
|
+
useTranslation: <TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>>(sheetTitle: TSheetTitle) => UseTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle, TRules>;
|
|
99
|
+
getTranslation: <TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>>(sheetTitle: TSheetTitle) => GetTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle>;
|
|
87
100
|
getLocaleStorageManager: (storageService?: IStorageService<string>) => LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>;
|
|
88
101
|
useLocaleStorage: typeof useLocaleStorage;
|
|
89
102
|
};
|
|
90
103
|
|
|
91
|
-
|
|
104
|
+
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>> {
|
|
105
|
+
private readonly i18nStore;
|
|
106
|
+
constructor(i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>);
|
|
107
|
+
createRule: <TValues extends $TParams[0]>(ruleFn: RuleFn<TSupportedLocales, TLocaleSet, I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, TTypeSafe, TSheetTitle, TMessageId, TValues>) => (values: TValues) => TMessageId;
|
|
108
|
+
}
|
|
109
|
+
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>>;
|
|
110
|
+
|
|
111
|
+
export { type IStorageService, createI18nContext, ruleFactory };
|
package/dist/index.d.ts
CHANGED
|
@@ -40,7 +40,16 @@ type SafeMessageId<TSupportedLocales extends readonly string[], TLocaleSet exten
|
|
|
40
40
|
* Resolves to valid sheet titles from the locale set
|
|
41
41
|
* or defaults to string if type safety is off or keys are not available.
|
|
42
42
|
*/
|
|
43
|
-
type
|
|
43
|
+
type SupportedLocales = readonly string[];
|
|
44
|
+
type LocaleSet = Partial<Record<SupportedLocales[number], Record<string, any>>>;
|
|
45
|
+
type TypeSafe = boolean;
|
|
46
|
+
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;
|
|
47
|
+
/**
|
|
48
|
+
* Plugin types
|
|
49
|
+
*/
|
|
50
|
+
type RuleKey = string;
|
|
51
|
+
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;
|
|
52
|
+
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>>;
|
|
44
53
|
|
|
45
54
|
type GetTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>> = {
|
|
46
55
|
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;
|
|
@@ -51,11 +60,12 @@ type GetTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSe
|
|
|
51
60
|
};
|
|
52
61
|
};
|
|
53
62
|
|
|
54
|
-
type UseTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Partial<Record<TSupportedLocales[number], Record<string, any>>>>, TTypeSafe extends boolean, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe
|
|
63
|
+
type UseTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Partial<Record<TSupportedLocales[number], Record<string, any>>>>, TTypeSafe extends boolean, TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>, TRules extends Rules> = {
|
|
55
64
|
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;
|
|
56
65
|
} & {
|
|
57
66
|
t: {
|
|
58
67
|
dynamic: <TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>, TValues extends $TParams[0], TOpts extends $TParams[1], TDescriptor extends $TParams[2]>(id: string, values?: TValues, opts?: TOpts, _descriptor?: TDescriptor) => TTypeSafe extends true ? string : any;
|
|
68
|
+
rule: <TRuleKey extends keyof TRules, TMessageId extends SafeMessageId<TSupportedLocales, TLocaleSet, TSheetTitle, TTypeSafe>>(ruleKey: TRuleKey, values?: Parameters<TRules[TRuleKey]>[0]) => TTypeSafe extends true ? TMessageId : any;
|
|
59
69
|
};
|
|
60
70
|
};
|
|
61
71
|
|
|
@@ -76,16 +86,26 @@ interface StorageBasedIntlProviderProps<TSupportedLocales extends readonly strin
|
|
|
76
86
|
children: React.ReactNode;
|
|
77
87
|
}
|
|
78
88
|
|
|
79
|
-
|
|
89
|
+
interface Plugins<TRules extends Rules> {
|
|
90
|
+
rules?: TRules;
|
|
91
|
+
}
|
|
92
|
+
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>): {
|
|
80
93
|
IntlProvider: ({ currentLocale, children, }: Omit<IntlProviderProps<TSupportedLocales, TLocaleSet>, "currentLocale" | "i18nStore"> & {
|
|
81
94
|
currentLocale?: string;
|
|
82
95
|
children?: React.ReactNode;
|
|
83
96
|
}) => react_jsx_runtime.JSX.Element;
|
|
84
97
|
StorageBasedIntlProvider: ({ storageManager, children, }: Omit<StorageBasedIntlProviderProps<TSupportedLocales, TLocaleSet, TTypeSafe>, "i18nStore">) => react_jsx_runtime.JSX.Element;
|
|
85
|
-
useTranslation: <TSheetTitle extends
|
|
86
|
-
getTranslation: <TSheetTitle extends
|
|
98
|
+
useTranslation: <TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>>(sheetTitle: TSheetTitle) => UseTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle, TRules>;
|
|
99
|
+
getTranslation: <TSheetTitle extends SafeSheetTitle<TSupportedLocales, TLocaleSet, TTypeSafe>>(sheetTitle: TSheetTitle) => GetTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle>;
|
|
87
100
|
getLocaleStorageManager: (storageService?: IStorageService<string>) => LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>;
|
|
88
101
|
useLocaleStorage: typeof useLocaleStorage;
|
|
89
102
|
};
|
|
90
103
|
|
|
91
|
-
|
|
104
|
+
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>> {
|
|
105
|
+
private readonly i18nStore;
|
|
106
|
+
constructor(i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>);
|
|
107
|
+
createRule: <TValues extends $TParams[0]>(ruleFn: RuleFn<TSupportedLocales, TLocaleSet, I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, TTypeSafe, TSheetTitle, TMessageId, TValues>) => (values: TValues) => TMessageId;
|
|
108
|
+
}
|
|
109
|
+
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>>;
|
|
110
|
+
|
|
111
|
+
export { type IStorageService, createI18nContext, ruleFactory };
|
package/dist/index.js
CHANGED
|
@@ -38,7 +38,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
38
38
|
// src/index.ts
|
|
39
39
|
var src_exports = {};
|
|
40
40
|
__export(src_exports, {
|
|
41
|
-
createI18nContext: () => createI18nContext
|
|
41
|
+
createI18nContext: () => createI18nContext,
|
|
42
|
+
ruleFactory: () => ruleFactory
|
|
42
43
|
});
|
|
43
44
|
module.exports = __toCommonJS(src_exports);
|
|
44
45
|
|
|
@@ -134,7 +135,7 @@ function useDynamicLocale(useDynamicLocaleProps) {
|
|
|
134
135
|
if (!dynamicLoaders) return;
|
|
135
136
|
const targetDynamicLoader = dynamicLoaders[currentLocale];
|
|
136
137
|
if (!targetDynamicLoader) return;
|
|
137
|
-
const currentLocaleSet = i18nStore == null ? void 0 : i18nStore.
|
|
138
|
+
const currentLocaleSet = i18nStore == null ? void 0 : i18nStore.localeSet;
|
|
138
139
|
const targetLocaleData = currentLocaleSet == null ? void 0 : currentLocaleSet[currentLocale];
|
|
139
140
|
if (targetLocaleData) return;
|
|
140
141
|
setIsLoading(true);
|
|
@@ -251,20 +252,26 @@ var TranslationService = class {
|
|
|
251
252
|
// src/hooks/useTranslation.ts
|
|
252
253
|
function useTranslation({
|
|
253
254
|
sheetTitle,
|
|
254
|
-
i18nStore
|
|
255
|
+
i18nStore,
|
|
256
|
+
rules
|
|
255
257
|
}) {
|
|
256
258
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
257
259
|
sheetTitle,
|
|
258
260
|
i18nStore
|
|
259
261
|
);
|
|
262
|
+
const translationService = new TranslationService(intlInstance);
|
|
260
263
|
const t = (id, values, opts, _descriptor) => {
|
|
261
|
-
const translationService = new TranslationService(intlInstance);
|
|
262
264
|
return translationService.translate(id, values, opts, _descriptor);
|
|
263
265
|
};
|
|
264
266
|
t.dynamic = (id, values, opts, _descriptor) => {
|
|
265
|
-
const translationService = new TranslationService(intlInstance);
|
|
266
267
|
return translationService.translate(id, values, opts, _descriptor);
|
|
267
268
|
};
|
|
269
|
+
t.rule = (ruleKey, values) => {
|
|
270
|
+
const ruleFn = rules == null ? void 0 : rules[ruleKey];
|
|
271
|
+
if (!ruleFn) return ruleKey;
|
|
272
|
+
const messageId = ruleFn(values, {});
|
|
273
|
+
return translationService.translate(messageId, values);
|
|
274
|
+
};
|
|
268
275
|
return { t };
|
|
269
276
|
}
|
|
270
277
|
|
|
@@ -280,39 +287,33 @@ function getTranslation({
|
|
|
280
287
|
sheetTitle,
|
|
281
288
|
i18nStore
|
|
282
289
|
}) {
|
|
290
|
+
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
291
|
+
sheetTitle,
|
|
292
|
+
i18nStore,
|
|
293
|
+
"server"
|
|
294
|
+
);
|
|
295
|
+
const translationService = new TranslationService(intlInstance);
|
|
283
296
|
const t = (id, values, opts, _descriptor) => {
|
|
284
|
-
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
285
|
-
sheetTitle,
|
|
286
|
-
i18nStore,
|
|
287
|
-
"server"
|
|
288
|
-
);
|
|
289
|
-
const translationService = new TranslationService(intlInstance);
|
|
290
297
|
return translationService.translate(id, values, opts, _descriptor);
|
|
291
298
|
};
|
|
292
299
|
t.dynamic = (id, values, opts, _descriptor) => {
|
|
293
|
-
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
294
|
-
sheetTitle,
|
|
295
|
-
i18nStore,
|
|
296
|
-
"server"
|
|
297
|
-
);
|
|
298
|
-
const translationService = new TranslationService(intlInstance);
|
|
299
300
|
return translationService.translate(id, values, opts, _descriptor);
|
|
300
301
|
};
|
|
301
302
|
t.promise = (id, values, opts, _descriptor) => {
|
|
302
303
|
const intlInitPromise = new Promise((resolve) => {
|
|
303
304
|
setTimeout(() => {
|
|
304
|
-
const
|
|
305
|
+
const intlInstance2 = intlInstanceCache.getCachedIntlInstance(
|
|
305
306
|
sheetTitle,
|
|
306
307
|
i18nStore,
|
|
307
308
|
"server"
|
|
308
309
|
);
|
|
309
|
-
resolve(
|
|
310
|
+
resolve(intlInstance2);
|
|
310
311
|
}, 1);
|
|
311
312
|
});
|
|
312
313
|
return new Promise((resolve) => {
|
|
313
|
-
intlInitPromise.then((
|
|
314
|
-
const
|
|
315
|
-
resolve(
|
|
314
|
+
intlInitPromise.then((intlInstance2) => {
|
|
315
|
+
const translationService2 = new TranslationService(intlInstance2);
|
|
316
|
+
resolve(translationService2.translate(id, values, opts, _descriptor));
|
|
316
317
|
});
|
|
317
318
|
});
|
|
318
319
|
};
|
|
@@ -497,7 +498,7 @@ function StorageBasedIntlProvider({
|
|
|
497
498
|
|
|
498
499
|
// src/createI18nContext.tsx
|
|
499
500
|
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
500
|
-
function createI18nContext(i18nStore) {
|
|
501
|
+
function createI18nContext(i18nStore, plugins) {
|
|
501
502
|
if (import_shared_utils6.validator.isNullish(i18nStore)) {
|
|
502
503
|
throw new InvalidI18nContextStateError(
|
|
503
504
|
"\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
|
|
@@ -508,6 +509,7 @@ function createI18nContext(i18nStore) {
|
|
|
508
509
|
"\u26A0\uFE0F The provided i18nStore is invalid. Please ensure a store instance you passed is an instance of I18nStore."
|
|
509
510
|
);
|
|
510
511
|
}
|
|
512
|
+
const { rules } = plugins != null ? plugins : {};
|
|
511
513
|
const IntlProviderImpl = ({
|
|
512
514
|
currentLocale,
|
|
513
515
|
children
|
|
@@ -530,7 +532,7 @@ function createI18nContext(i18nStore) {
|
|
|
530
532
|
children
|
|
531
533
|
}
|
|
532
534
|
);
|
|
533
|
-
const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore });
|
|
535
|
+
const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore, rules });
|
|
534
536
|
const getTranslationImpl = (sheetTitle) => getTranslation({ sheetTitle, i18nStore });
|
|
535
537
|
const getLocaleStorageManagerImpl = (storageService) => {
|
|
536
538
|
return getLocaleStorageManager(i18nStore, storageService);
|
|
@@ -544,7 +546,22 @@ function createI18nContext(i18nStore) {
|
|
|
544
546
|
useLocaleStorage
|
|
545
547
|
};
|
|
546
548
|
}
|
|
549
|
+
|
|
550
|
+
// src/Service/RuleService.ts
|
|
551
|
+
var RuleService = class {
|
|
552
|
+
constructor(i18nStore) {
|
|
553
|
+
this.i18nStore = i18nStore;
|
|
554
|
+
this.createRule = (ruleFn) => {
|
|
555
|
+
const currentLocaleSet = this.i18nStore.getCurrentLocaleSet();
|
|
556
|
+
return (values) => ruleFn(values, currentLocaleSet);
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
var ruleFactory = (i18nStore) => {
|
|
561
|
+
return new RuleService(i18nStore);
|
|
562
|
+
};
|
|
547
563
|
// Annotate the CommonJS export names for ESM import in node:
|
|
548
564
|
0 && (module.exports = {
|
|
549
|
-
createI18nContext
|
|
565
|
+
createI18nContext,
|
|
566
|
+
ruleFactory
|
|
550
567
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -111,7 +111,7 @@ function useDynamicLocale(useDynamicLocaleProps) {
|
|
|
111
111
|
if (!dynamicLoaders) return;
|
|
112
112
|
const targetDynamicLoader = dynamicLoaders[currentLocale];
|
|
113
113
|
if (!targetDynamicLoader) return;
|
|
114
|
-
const currentLocaleSet = i18nStore == null ? void 0 : i18nStore.
|
|
114
|
+
const currentLocaleSet = i18nStore == null ? void 0 : i18nStore.localeSet;
|
|
115
115
|
const targetLocaleData = currentLocaleSet == null ? void 0 : currentLocaleSet[currentLocale];
|
|
116
116
|
if (targetLocaleData) return;
|
|
117
117
|
setIsLoading(true);
|
|
@@ -228,20 +228,26 @@ var TranslationService = class {
|
|
|
228
228
|
// src/hooks/useTranslation.ts
|
|
229
229
|
function useTranslation({
|
|
230
230
|
sheetTitle,
|
|
231
|
-
i18nStore
|
|
231
|
+
i18nStore,
|
|
232
|
+
rules
|
|
232
233
|
}) {
|
|
233
234
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
234
235
|
sheetTitle,
|
|
235
236
|
i18nStore
|
|
236
237
|
);
|
|
238
|
+
const translationService = new TranslationService(intlInstance);
|
|
237
239
|
const t = (id, values, opts, _descriptor) => {
|
|
238
|
-
const translationService = new TranslationService(intlInstance);
|
|
239
240
|
return translationService.translate(id, values, opts, _descriptor);
|
|
240
241
|
};
|
|
241
242
|
t.dynamic = (id, values, opts, _descriptor) => {
|
|
242
|
-
const translationService = new TranslationService(intlInstance);
|
|
243
243
|
return translationService.translate(id, values, opts, _descriptor);
|
|
244
244
|
};
|
|
245
|
+
t.rule = (ruleKey, values) => {
|
|
246
|
+
const ruleFn = rules == null ? void 0 : rules[ruleKey];
|
|
247
|
+
if (!ruleFn) return ruleKey;
|
|
248
|
+
const messageId = ruleFn(values, {});
|
|
249
|
+
return translationService.translate(messageId, values);
|
|
250
|
+
};
|
|
245
251
|
return { t };
|
|
246
252
|
}
|
|
247
253
|
|
|
@@ -257,39 +263,33 @@ function getTranslation({
|
|
|
257
263
|
sheetTitle,
|
|
258
264
|
i18nStore
|
|
259
265
|
}) {
|
|
266
|
+
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
267
|
+
sheetTitle,
|
|
268
|
+
i18nStore,
|
|
269
|
+
"server"
|
|
270
|
+
);
|
|
271
|
+
const translationService = new TranslationService(intlInstance);
|
|
260
272
|
const t = (id, values, opts, _descriptor) => {
|
|
261
|
-
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
262
|
-
sheetTitle,
|
|
263
|
-
i18nStore,
|
|
264
|
-
"server"
|
|
265
|
-
);
|
|
266
|
-
const translationService = new TranslationService(intlInstance);
|
|
267
273
|
return translationService.translate(id, values, opts, _descriptor);
|
|
268
274
|
};
|
|
269
275
|
t.dynamic = (id, values, opts, _descriptor) => {
|
|
270
|
-
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
271
|
-
sheetTitle,
|
|
272
|
-
i18nStore,
|
|
273
|
-
"server"
|
|
274
|
-
);
|
|
275
|
-
const translationService = new TranslationService(intlInstance);
|
|
276
276
|
return translationService.translate(id, values, opts, _descriptor);
|
|
277
277
|
};
|
|
278
278
|
t.promise = (id, values, opts, _descriptor) => {
|
|
279
279
|
const intlInitPromise = new Promise((resolve) => {
|
|
280
280
|
setTimeout(() => {
|
|
281
|
-
const
|
|
281
|
+
const intlInstance2 = intlInstanceCache.getCachedIntlInstance(
|
|
282
282
|
sheetTitle,
|
|
283
283
|
i18nStore,
|
|
284
284
|
"server"
|
|
285
285
|
);
|
|
286
|
-
resolve(
|
|
286
|
+
resolve(intlInstance2);
|
|
287
287
|
}, 1);
|
|
288
288
|
});
|
|
289
289
|
return new Promise((resolve) => {
|
|
290
|
-
intlInitPromise.then((
|
|
291
|
-
const
|
|
292
|
-
resolve(
|
|
290
|
+
intlInitPromise.then((intlInstance2) => {
|
|
291
|
+
const translationService2 = new TranslationService(intlInstance2);
|
|
292
|
+
resolve(translationService2.translate(id, values, opts, _descriptor));
|
|
293
293
|
});
|
|
294
294
|
});
|
|
295
295
|
};
|
|
@@ -474,7 +474,7 @@ function StorageBasedIntlProvider({
|
|
|
474
474
|
|
|
475
475
|
// src/createI18nContext.tsx
|
|
476
476
|
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
477
|
-
function createI18nContext(i18nStore) {
|
|
477
|
+
function createI18nContext(i18nStore, plugins) {
|
|
478
478
|
if (validator5.isNullish(i18nStore)) {
|
|
479
479
|
throw new InvalidI18nContextStateError(
|
|
480
480
|
"\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
|
|
@@ -485,6 +485,7 @@ function createI18nContext(i18nStore) {
|
|
|
485
485
|
"\u26A0\uFE0F The provided i18nStore is invalid. Please ensure a store instance you passed is an instance of I18nStore."
|
|
486
486
|
);
|
|
487
487
|
}
|
|
488
|
+
const { rules } = plugins != null ? plugins : {};
|
|
488
489
|
const IntlProviderImpl = ({
|
|
489
490
|
currentLocale,
|
|
490
491
|
children
|
|
@@ -507,7 +508,7 @@ function createI18nContext(i18nStore) {
|
|
|
507
508
|
children
|
|
508
509
|
}
|
|
509
510
|
);
|
|
510
|
-
const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore });
|
|
511
|
+
const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore, rules });
|
|
511
512
|
const getTranslationImpl = (sheetTitle) => getTranslation({ sheetTitle, i18nStore });
|
|
512
513
|
const getLocaleStorageManagerImpl = (storageService) => {
|
|
513
514
|
return getLocaleStorageManager(i18nStore, storageService);
|
|
@@ -521,6 +522,21 @@ function createI18nContext(i18nStore) {
|
|
|
521
522
|
useLocaleStorage
|
|
522
523
|
};
|
|
523
524
|
}
|
|
525
|
+
|
|
526
|
+
// src/Service/RuleService.ts
|
|
527
|
+
var RuleService = class {
|
|
528
|
+
constructor(i18nStore) {
|
|
529
|
+
this.i18nStore = i18nStore;
|
|
530
|
+
this.createRule = (ruleFn) => {
|
|
531
|
+
const currentLocaleSet = this.i18nStore.getCurrentLocaleSet();
|
|
532
|
+
return (values) => ruleFn(values, currentLocaleSet);
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
var ruleFactory = (i18nStore) => {
|
|
537
|
+
return new RuleService(i18nStore);
|
|
538
|
+
};
|
|
524
539
|
export {
|
|
525
|
-
createI18nContext
|
|
540
|
+
createI18nContext,
|
|
541
|
+
ruleFactory
|
|
526
542
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sheet-i18n/react-client",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0-canary.0",
|
|
4
4
|
"description": "a client package for react modules used by sheet-i18n",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@sheet-i18n/shared-utils": "1.8.2",
|
|
29
29
|
"@sheet-i18n/errors": "1.8.1",
|
|
30
|
-
"@sheet-i18n/react-core": "1.
|
|
30
|
+
"@sheet-i18n/react-core": "1.6.0-canary.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/react": "^19.0.2",
|