@sheet-i18n/react-client 1.0.8 → 1.0.10
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 +6 -7
- package/dist/index.d.ts +6 -7
- package/dist/index.js +76 -58
- package/dist/index.mjs +76 -58
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -2,19 +2,18 @@ import { MessageDescriptor, IntlShape } from 'react-intl';
|
|
|
2
2
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
3
|
import { I18nStore } from '@sheet-i18n/react-core';
|
|
4
4
|
|
|
5
|
-
type UseIntlParams
|
|
6
|
-
type $TParams
|
|
5
|
+
type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
6
|
+
type $TParams = Partial<UseIntlParams>;
|
|
7
|
+
|
|
7
8
|
type GetTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>, TTypeSafe extends boolean, TSheetTitle extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] : string> = {
|
|
8
|
-
t: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, TValues extends $TParams
|
|
9
|
+
t: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, 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;
|
|
9
10
|
} & {
|
|
10
11
|
t: {
|
|
11
|
-
dynamic: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, TValues extends $TParams
|
|
12
|
-
promise: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, TValues extends $TParams
|
|
12
|
+
dynamic: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, 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;
|
|
13
|
+
promise: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, TValues extends $TParams[0], TOpts extends $TParams[1], TDescriptor extends $TParams[2]>(id: TMessageId, values?: TValues, opts?: TOpts, _descriptor?: TDescriptor) => TTypeSafe extends true ? Promise<TMessageId> : any;
|
|
13
14
|
};
|
|
14
15
|
};
|
|
15
16
|
|
|
16
|
-
type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
17
|
-
type $TParams = Partial<UseIntlParams>;
|
|
18
17
|
type UseTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>, TTypeSafe extends boolean, TSheetTitle extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] : string> = {
|
|
19
18
|
t: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, 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;
|
|
20
19
|
} & {
|
package/dist/index.d.ts
CHANGED
|
@@ -2,19 +2,18 @@ import { MessageDescriptor, IntlShape } from 'react-intl';
|
|
|
2
2
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
3
|
import { I18nStore } from '@sheet-i18n/react-core';
|
|
4
4
|
|
|
5
|
-
type UseIntlParams
|
|
6
|
-
type $TParams
|
|
5
|
+
type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
6
|
+
type $TParams = Partial<UseIntlParams>;
|
|
7
|
+
|
|
7
8
|
type GetTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>, TTypeSafe extends boolean, TSheetTitle extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] : string> = {
|
|
8
|
-
t: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, TValues extends $TParams
|
|
9
|
+
t: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, 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;
|
|
9
10
|
} & {
|
|
10
11
|
t: {
|
|
11
|
-
dynamic: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, TValues extends $TParams
|
|
12
|
-
promise: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, TValues extends $TParams
|
|
12
|
+
dynamic: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, 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;
|
|
13
|
+
promise: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, TValues extends $TParams[0], TOpts extends $TParams[1], TDescriptor extends $TParams[2]>(id: TMessageId, values?: TValues, opts?: TOpts, _descriptor?: TDescriptor) => TTypeSafe extends true ? Promise<TMessageId> : any;
|
|
13
14
|
};
|
|
14
15
|
};
|
|
15
16
|
|
|
16
|
-
type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
17
|
-
type $TParams = Partial<UseIntlParams>;
|
|
18
17
|
type UseTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>, TTypeSafe extends boolean, TSheetTitle extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] : string> = {
|
|
19
18
|
t: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, 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;
|
|
20
19
|
} & {
|
package/dist/index.js
CHANGED
|
@@ -43,7 +43,7 @@ __export(src_exports, {
|
|
|
43
43
|
module.exports = __toCommonJS(src_exports);
|
|
44
44
|
|
|
45
45
|
// src/createI18nContext.tsx
|
|
46
|
-
var
|
|
46
|
+
var import_shared_utils3 = require("@sheet-i18n/shared-utils");
|
|
47
47
|
var import_react_core = require("@sheet-i18n/react-core");
|
|
48
48
|
|
|
49
49
|
// src/IntlProvider.tsx
|
|
@@ -55,13 +55,8 @@ function IntlProvider({
|
|
|
55
55
|
currentLocale,
|
|
56
56
|
children
|
|
57
57
|
}) {
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
i18nStore.setCurrentLocale(locale);
|
|
61
|
-
(0, import_react.useEffect)(() => {
|
|
62
|
-
i18nStore.setCurrentLocale(locale);
|
|
63
|
-
}, [locale]);
|
|
64
|
-
const onError = (err) => {
|
|
58
|
+
const { locale } = useIntlLocale({ i18nStore, currentLocale });
|
|
59
|
+
const onIntlError = (err) => {
|
|
65
60
|
const typeSafe = i18nStore.typeSafe;
|
|
66
61
|
if (typeSafe) {
|
|
67
62
|
console.error(err);
|
|
@@ -72,11 +67,25 @@ function IntlProvider({
|
|
|
72
67
|
{
|
|
73
68
|
locale,
|
|
74
69
|
messages: i18nStore == null ? void 0 : i18nStore.localeSet[locale],
|
|
75
|
-
onError,
|
|
70
|
+
onError: onIntlError,
|
|
76
71
|
children
|
|
77
72
|
}
|
|
78
73
|
);
|
|
79
74
|
}
|
|
75
|
+
function useIntlLocale({
|
|
76
|
+
i18nStore,
|
|
77
|
+
currentLocale
|
|
78
|
+
}) {
|
|
79
|
+
var _a;
|
|
80
|
+
const locale = (_a = currentLocale != null ? currentLocale : detectClientLanguage(i18nStore)) != null ? _a : i18nStore.defaultLocale;
|
|
81
|
+
i18nStore.setCurrentLocale(locale);
|
|
82
|
+
(0, import_react.useEffect)(() => {
|
|
83
|
+
i18nStore.setCurrentLocale(locale);
|
|
84
|
+
}, [locale]);
|
|
85
|
+
return {
|
|
86
|
+
locale
|
|
87
|
+
};
|
|
88
|
+
}
|
|
80
89
|
function detectClientLanguage(i18nStore) {
|
|
81
90
|
const { defaultLocale, supportedLocales } = i18nStore;
|
|
82
91
|
if (typeof navigator !== "undefined" && (navigator == null ? void 0 : navigator.languages)) {
|
|
@@ -123,7 +132,9 @@ var IntlInstanceCache = class _IntlInstanceCache {
|
|
|
123
132
|
this.createServerIntlInstance = (sheetTitle, i18nStore) => {
|
|
124
133
|
const currentLocale = i18nStore.getCurrentLocale();
|
|
125
134
|
const targetMessages = i18nStore.getTargetMessages(sheetTitle);
|
|
126
|
-
if (!i18nStore || !currentLocale)
|
|
135
|
+
if (!i18nStore || !currentLocale) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
127
138
|
const newIntl = (0, import_react_intl2.createIntl)(
|
|
128
139
|
{ locale: currentLocale, messages: targetMessages },
|
|
129
140
|
intlCache
|
|
@@ -151,6 +162,50 @@ var IntlInstanceCache = class _IntlInstanceCache {
|
|
|
151
162
|
};
|
|
152
163
|
var intlInstanceCache = IntlInstanceCache.init();
|
|
153
164
|
|
|
165
|
+
// src/Service/TranslationService.ts
|
|
166
|
+
var import_shared_utils2 = require("@sheet-i18n/shared-utils");
|
|
167
|
+
var TranslationService = class {
|
|
168
|
+
constructor(intlInstance) {
|
|
169
|
+
this.intlInstance = intlInstance;
|
|
170
|
+
}
|
|
171
|
+
translate(id, values, opts, _descriptor) {
|
|
172
|
+
const descriptor = this.createDescriptor(id, _descriptor);
|
|
173
|
+
const targetTranslation = this.findTargetTranslation(id, {
|
|
174
|
+
descriptor,
|
|
175
|
+
values,
|
|
176
|
+
opts
|
|
177
|
+
});
|
|
178
|
+
return targetTranslation;
|
|
179
|
+
}
|
|
180
|
+
/** helpers */
|
|
181
|
+
createDescriptor(id, _descriptor) {
|
|
182
|
+
var _a;
|
|
183
|
+
return __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
184
|
+
id,
|
|
185
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
findTargetTranslation(id, $tParams) {
|
|
189
|
+
var _a, _b, _c, _d, _e;
|
|
190
|
+
const targetTranslation = (_b = (_a = this == null ? void 0 : this.intlInstance) == null ? void 0 : _a.messages) == null ? void 0 : _b[id];
|
|
191
|
+
if (import_shared_utils2.validator.isNullish(targetTranslation)) {
|
|
192
|
+
return id;
|
|
193
|
+
}
|
|
194
|
+
if (typeof targetTranslation === "string" && targetTranslation.trim() === "") {
|
|
195
|
+
return "";
|
|
196
|
+
}
|
|
197
|
+
if (!$tParams || !($tParams == null ? void 0 : $tParams.descriptor)) {
|
|
198
|
+
return "";
|
|
199
|
+
}
|
|
200
|
+
return (_e = (_d = (_c = this.intlInstance) == null ? void 0 : _c.$t) == null ? void 0 : _d.call(
|
|
201
|
+
_c,
|
|
202
|
+
$tParams.descriptor,
|
|
203
|
+
$tParams.values,
|
|
204
|
+
$tParams.opts
|
|
205
|
+
)) != null ? _e : "";
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
154
209
|
// src/useTranslation.ts
|
|
155
210
|
function useTranslation({
|
|
156
211
|
sheetTitle,
|
|
@@ -161,20 +216,12 @@ function useTranslation({
|
|
|
161
216
|
i18nStore
|
|
162
217
|
);
|
|
163
218
|
const t = (id, values, opts, _descriptor) => {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
id,
|
|
167
|
-
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
168
|
-
});
|
|
169
|
-
return (_d = (_c = (_b = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _b.call(intlInstance, descriptor, values, opts)) != null ? _c : id) != null ? _d : "";
|
|
219
|
+
const translationService = new TranslationService(intlInstance);
|
|
220
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
170
221
|
};
|
|
171
222
|
t.dynamic = (id, values, opts, _descriptor) => {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
id,
|
|
175
|
-
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
176
|
-
});
|
|
177
|
-
return (_d = (_c = (_b = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _b.call(intlInstance, descriptor, values, opts)) != null ? _c : id) != null ? _d : "";
|
|
223
|
+
const translationService = new TranslationService(intlInstance);
|
|
224
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
178
225
|
};
|
|
179
226
|
return { t };
|
|
180
227
|
}
|
|
@@ -192,37 +239,24 @@ function getTranslation({
|
|
|
192
239
|
i18nStore
|
|
193
240
|
}) {
|
|
194
241
|
const t = (id, values, opts, _descriptor) => {
|
|
195
|
-
var _a, _b, _c, _d;
|
|
196
|
-
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
197
|
-
id,
|
|
198
|
-
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
199
|
-
});
|
|
200
242
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
201
243
|
sheetTitle,
|
|
202
244
|
i18nStore,
|
|
203
245
|
"server"
|
|
204
246
|
);
|
|
205
|
-
|
|
247
|
+
const translationService = new TranslationService(intlInstance);
|
|
248
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
206
249
|
};
|
|
207
250
|
t.dynamic = (id, values, opts, _descriptor) => {
|
|
208
|
-
var _a, _b, _c, _d;
|
|
209
|
-
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
210
|
-
id,
|
|
211
|
-
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
212
|
-
});
|
|
213
251
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
214
252
|
sheetTitle,
|
|
215
253
|
i18nStore,
|
|
216
254
|
"server"
|
|
217
255
|
);
|
|
218
|
-
|
|
256
|
+
const translationService = new TranslationService(intlInstance);
|
|
257
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
219
258
|
};
|
|
220
259
|
t.promise = (id, values, opts, _descriptor) => {
|
|
221
|
-
var _a;
|
|
222
|
-
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
223
|
-
id,
|
|
224
|
-
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
225
|
-
});
|
|
226
260
|
const intlInitPromise = new Promise((resolve) => {
|
|
227
261
|
setTimeout(() => {
|
|
228
262
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
@@ -235,34 +269,18 @@ function getTranslation({
|
|
|
235
269
|
});
|
|
236
270
|
return new Promise((resolve) => {
|
|
237
271
|
intlInitPromise.then((intlInstance) => {
|
|
238
|
-
|
|
239
|
-
resolve(
|
|
272
|
+
const translationService = new TranslationService(intlInstance);
|
|
273
|
+
resolve(translationService.translate(id, values, opts, _descriptor));
|
|
240
274
|
});
|
|
241
275
|
});
|
|
242
276
|
};
|
|
243
|
-
t.lazy = (id, values, opts, _descriptor) => {
|
|
244
|
-
var _a;
|
|
245
|
-
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
246
|
-
id,
|
|
247
|
-
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
248
|
-
});
|
|
249
|
-
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
250
|
-
sheetTitle,
|
|
251
|
-
i18nStore,
|
|
252
|
-
"server"
|
|
253
|
-
);
|
|
254
|
-
return () => {
|
|
255
|
-
var _a2, _b, _c;
|
|
256
|
-
return (_c = (_b = (_a2 = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _a2.call(intlInstance, descriptor, values, opts)) != null ? _b : id) != null ? _c : "";
|
|
257
|
-
};
|
|
258
|
-
};
|
|
259
277
|
return { t };
|
|
260
278
|
}
|
|
261
279
|
|
|
262
280
|
// src/createI18nContext.tsx
|
|
263
281
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
264
282
|
function createI18nContext(i18nStore) {
|
|
265
|
-
if (
|
|
283
|
+
if (import_shared_utils3.validator.isNullish(i18nStore)) {
|
|
266
284
|
throw new InvalidI18nContextStateError(
|
|
267
285
|
"\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
|
|
268
286
|
);
|
package/dist/index.mjs
CHANGED
|
@@ -20,7 +20,7 @@ var __spreadValues = (a, b) => {
|
|
|
20
20
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
21
21
|
|
|
22
22
|
// src/createI18nContext.tsx
|
|
23
|
-
import { validator as
|
|
23
|
+
import { validator as validator3 } from "@sheet-i18n/shared-utils";
|
|
24
24
|
import { I18nStore } from "@sheet-i18n/react-core";
|
|
25
25
|
|
|
26
26
|
// src/IntlProvider.tsx
|
|
@@ -32,13 +32,8 @@ function IntlProvider({
|
|
|
32
32
|
currentLocale,
|
|
33
33
|
children
|
|
34
34
|
}) {
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
i18nStore.setCurrentLocale(locale);
|
|
38
|
-
useEffect(() => {
|
|
39
|
-
i18nStore.setCurrentLocale(locale);
|
|
40
|
-
}, [locale]);
|
|
41
|
-
const onError = (err) => {
|
|
35
|
+
const { locale } = useIntlLocale({ i18nStore, currentLocale });
|
|
36
|
+
const onIntlError = (err) => {
|
|
42
37
|
const typeSafe = i18nStore.typeSafe;
|
|
43
38
|
if (typeSafe) {
|
|
44
39
|
console.error(err);
|
|
@@ -49,11 +44,25 @@ function IntlProvider({
|
|
|
49
44
|
{
|
|
50
45
|
locale,
|
|
51
46
|
messages: i18nStore == null ? void 0 : i18nStore.localeSet[locale],
|
|
52
|
-
onError,
|
|
47
|
+
onError: onIntlError,
|
|
53
48
|
children
|
|
54
49
|
}
|
|
55
50
|
);
|
|
56
51
|
}
|
|
52
|
+
function useIntlLocale({
|
|
53
|
+
i18nStore,
|
|
54
|
+
currentLocale
|
|
55
|
+
}) {
|
|
56
|
+
var _a;
|
|
57
|
+
const locale = (_a = currentLocale != null ? currentLocale : detectClientLanguage(i18nStore)) != null ? _a : i18nStore.defaultLocale;
|
|
58
|
+
i18nStore.setCurrentLocale(locale);
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
i18nStore.setCurrentLocale(locale);
|
|
61
|
+
}, [locale]);
|
|
62
|
+
return {
|
|
63
|
+
locale
|
|
64
|
+
};
|
|
65
|
+
}
|
|
57
66
|
function detectClientLanguage(i18nStore) {
|
|
58
67
|
const { defaultLocale, supportedLocales } = i18nStore;
|
|
59
68
|
if (typeof navigator !== "undefined" && (navigator == null ? void 0 : navigator.languages)) {
|
|
@@ -100,7 +109,9 @@ var IntlInstanceCache = class _IntlInstanceCache {
|
|
|
100
109
|
this.createServerIntlInstance = (sheetTitle, i18nStore) => {
|
|
101
110
|
const currentLocale = i18nStore.getCurrentLocale();
|
|
102
111
|
const targetMessages = i18nStore.getTargetMessages(sheetTitle);
|
|
103
|
-
if (!i18nStore || !currentLocale)
|
|
112
|
+
if (!i18nStore || !currentLocale) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
104
115
|
const newIntl = createIntl(
|
|
105
116
|
{ locale: currentLocale, messages: targetMessages },
|
|
106
117
|
intlCache
|
|
@@ -128,6 +139,50 @@ var IntlInstanceCache = class _IntlInstanceCache {
|
|
|
128
139
|
};
|
|
129
140
|
var intlInstanceCache = IntlInstanceCache.init();
|
|
130
141
|
|
|
142
|
+
// src/Service/TranslationService.ts
|
|
143
|
+
import { validator as validator2 } from "@sheet-i18n/shared-utils";
|
|
144
|
+
var TranslationService = class {
|
|
145
|
+
constructor(intlInstance) {
|
|
146
|
+
this.intlInstance = intlInstance;
|
|
147
|
+
}
|
|
148
|
+
translate(id, values, opts, _descriptor) {
|
|
149
|
+
const descriptor = this.createDescriptor(id, _descriptor);
|
|
150
|
+
const targetTranslation = this.findTargetTranslation(id, {
|
|
151
|
+
descriptor,
|
|
152
|
+
values,
|
|
153
|
+
opts
|
|
154
|
+
});
|
|
155
|
+
return targetTranslation;
|
|
156
|
+
}
|
|
157
|
+
/** helpers */
|
|
158
|
+
createDescriptor(id, _descriptor) {
|
|
159
|
+
var _a;
|
|
160
|
+
return __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
161
|
+
id,
|
|
162
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
findTargetTranslation(id, $tParams) {
|
|
166
|
+
var _a, _b, _c, _d, _e;
|
|
167
|
+
const targetTranslation = (_b = (_a = this == null ? void 0 : this.intlInstance) == null ? void 0 : _a.messages) == null ? void 0 : _b[id];
|
|
168
|
+
if (validator2.isNullish(targetTranslation)) {
|
|
169
|
+
return id;
|
|
170
|
+
}
|
|
171
|
+
if (typeof targetTranslation === "string" && targetTranslation.trim() === "") {
|
|
172
|
+
return "";
|
|
173
|
+
}
|
|
174
|
+
if (!$tParams || !($tParams == null ? void 0 : $tParams.descriptor)) {
|
|
175
|
+
return "";
|
|
176
|
+
}
|
|
177
|
+
return (_e = (_d = (_c = this.intlInstance) == null ? void 0 : _c.$t) == null ? void 0 : _d.call(
|
|
178
|
+
_c,
|
|
179
|
+
$tParams.descriptor,
|
|
180
|
+
$tParams.values,
|
|
181
|
+
$tParams.opts
|
|
182
|
+
)) != null ? _e : "";
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
131
186
|
// src/useTranslation.ts
|
|
132
187
|
function useTranslation({
|
|
133
188
|
sheetTitle,
|
|
@@ -138,20 +193,12 @@ function useTranslation({
|
|
|
138
193
|
i18nStore
|
|
139
194
|
);
|
|
140
195
|
const t = (id, values, opts, _descriptor) => {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
id,
|
|
144
|
-
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
145
|
-
});
|
|
146
|
-
return (_d = (_c = (_b = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _b.call(intlInstance, descriptor, values, opts)) != null ? _c : id) != null ? _d : "";
|
|
196
|
+
const translationService = new TranslationService(intlInstance);
|
|
197
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
147
198
|
};
|
|
148
199
|
t.dynamic = (id, values, opts, _descriptor) => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
id,
|
|
152
|
-
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
153
|
-
});
|
|
154
|
-
return (_d = (_c = (_b = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _b.call(intlInstance, descriptor, values, opts)) != null ? _c : id) != null ? _d : "";
|
|
200
|
+
const translationService = new TranslationService(intlInstance);
|
|
201
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
155
202
|
};
|
|
156
203
|
return { t };
|
|
157
204
|
}
|
|
@@ -169,37 +216,24 @@ function getTranslation({
|
|
|
169
216
|
i18nStore
|
|
170
217
|
}) {
|
|
171
218
|
const t = (id, values, opts, _descriptor) => {
|
|
172
|
-
var _a, _b, _c, _d;
|
|
173
|
-
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
174
|
-
id,
|
|
175
|
-
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
176
|
-
});
|
|
177
219
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
178
220
|
sheetTitle,
|
|
179
221
|
i18nStore,
|
|
180
222
|
"server"
|
|
181
223
|
);
|
|
182
|
-
|
|
224
|
+
const translationService = new TranslationService(intlInstance);
|
|
225
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
183
226
|
};
|
|
184
227
|
t.dynamic = (id, values, opts, _descriptor) => {
|
|
185
|
-
var _a, _b, _c, _d;
|
|
186
|
-
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
187
|
-
id,
|
|
188
|
-
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
189
|
-
});
|
|
190
228
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
191
229
|
sheetTitle,
|
|
192
230
|
i18nStore,
|
|
193
231
|
"server"
|
|
194
232
|
);
|
|
195
|
-
|
|
233
|
+
const translationService = new TranslationService(intlInstance);
|
|
234
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
196
235
|
};
|
|
197
236
|
t.promise = (id, values, opts, _descriptor) => {
|
|
198
|
-
var _a;
|
|
199
|
-
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
200
|
-
id,
|
|
201
|
-
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
202
|
-
});
|
|
203
237
|
const intlInitPromise = new Promise((resolve) => {
|
|
204
238
|
setTimeout(() => {
|
|
205
239
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
@@ -212,34 +246,18 @@ function getTranslation({
|
|
|
212
246
|
});
|
|
213
247
|
return new Promise((resolve) => {
|
|
214
248
|
intlInitPromise.then((intlInstance) => {
|
|
215
|
-
|
|
216
|
-
resolve(
|
|
249
|
+
const translationService = new TranslationService(intlInstance);
|
|
250
|
+
resolve(translationService.translate(id, values, opts, _descriptor));
|
|
217
251
|
});
|
|
218
252
|
});
|
|
219
253
|
};
|
|
220
|
-
t.lazy = (id, values, opts, _descriptor) => {
|
|
221
|
-
var _a;
|
|
222
|
-
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
223
|
-
id,
|
|
224
|
-
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
225
|
-
});
|
|
226
|
-
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
227
|
-
sheetTitle,
|
|
228
|
-
i18nStore,
|
|
229
|
-
"server"
|
|
230
|
-
);
|
|
231
|
-
return () => {
|
|
232
|
-
var _a2, _b, _c;
|
|
233
|
-
return (_c = (_b = (_a2 = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _a2.call(intlInstance, descriptor, values, opts)) != null ? _b : id) != null ? _c : "";
|
|
234
|
-
};
|
|
235
|
-
};
|
|
236
254
|
return { t };
|
|
237
255
|
}
|
|
238
256
|
|
|
239
257
|
// src/createI18nContext.tsx
|
|
240
258
|
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
241
259
|
function createI18nContext(i18nStore) {
|
|
242
|
-
if (
|
|
260
|
+
if (validator3.isNullish(i18nStore)) {
|
|
243
261
|
throw new InvalidI18nContextStateError(
|
|
244
262
|
"\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
|
|
245
263
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sheet-i18n/react-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
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,14 +27,14 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"react": "^18.2.0",
|
|
29
29
|
"react-intl": "^7.0.4",
|
|
30
|
-
"@sheet-i18n/shared-utils": "1.3.
|
|
31
|
-
"@sheet-i18n/
|
|
32
|
-
"@sheet-i18n/
|
|
30
|
+
"@sheet-i18n/shared-utils": "1.3.9",
|
|
31
|
+
"@sheet-i18n/errors": "1.3.9",
|
|
32
|
+
"@sheet-i18n/react-core": "1.0.7"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/react": "^19.0.2",
|
|
36
36
|
"@types/react-dom": "^19.0.2",
|
|
37
|
-
"@sheet-i18n/typescript-config": "1.3.
|
|
37
|
+
"@sheet-i18n/typescript-config": "1.3.9"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "tsup",
|