@sheet-i18n/react-client 1.0.8 → 1.0.9
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 +70 -56
- package/dist/index.mjs +70 -56
- 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
|
@@ -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,46 @@ var IntlInstanceCache = class _IntlInstanceCache {
|
|
|
151
162
|
};
|
|
152
163
|
var intlInstanceCache = IntlInstanceCache.init();
|
|
153
164
|
|
|
165
|
+
// src/Service/TranslationService.ts
|
|
166
|
+
var TranslationService = class {
|
|
167
|
+
constructor(intlInstance) {
|
|
168
|
+
this.intlInstance = intlInstance;
|
|
169
|
+
}
|
|
170
|
+
translate(id, values, opts, _descriptor) {
|
|
171
|
+
const descriptor = this.createDescriptor(id, _descriptor);
|
|
172
|
+
const targetTranslation = this.findTargetTranslation(id, {
|
|
173
|
+
descriptor,
|
|
174
|
+
values,
|
|
175
|
+
opts
|
|
176
|
+
});
|
|
177
|
+
return targetTranslation;
|
|
178
|
+
}
|
|
179
|
+
/** helpers */
|
|
180
|
+
createDescriptor(id, _descriptor) {
|
|
181
|
+
var _a;
|
|
182
|
+
return __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
183
|
+
id,
|
|
184
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
findTargetTranslation(id, $tParams) {
|
|
188
|
+
var _a, _b, _c, _d, _e;
|
|
189
|
+
const targetTranslation = (_b = (_a = this == null ? void 0 : this.intlInstance) == null ? void 0 : _a.messages) == null ? void 0 : _b[id];
|
|
190
|
+
if (typeof targetTranslation === "string" && targetTranslation.trim() === "") {
|
|
191
|
+
return "";
|
|
192
|
+
}
|
|
193
|
+
if (!$tParams || !($tParams == null ? void 0 : $tParams.descriptor)) {
|
|
194
|
+
return "";
|
|
195
|
+
}
|
|
196
|
+
return (_e = (_d = (_c = this.intlInstance) == null ? void 0 : _c.$t) == null ? void 0 : _d.call(
|
|
197
|
+
_c,
|
|
198
|
+
$tParams.descriptor,
|
|
199
|
+
$tParams.values,
|
|
200
|
+
$tParams.opts
|
|
201
|
+
)) != null ? _e : "";
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
154
205
|
// src/useTranslation.ts
|
|
155
206
|
function useTranslation({
|
|
156
207
|
sheetTitle,
|
|
@@ -161,20 +212,12 @@ function useTranslation({
|
|
|
161
212
|
i18nStore
|
|
162
213
|
);
|
|
163
214
|
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 : "";
|
|
215
|
+
const translationService = new TranslationService(intlInstance);
|
|
216
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
170
217
|
};
|
|
171
218
|
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 : "";
|
|
219
|
+
const translationService = new TranslationService(intlInstance);
|
|
220
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
178
221
|
};
|
|
179
222
|
return { t };
|
|
180
223
|
}
|
|
@@ -192,37 +235,24 @@ function getTranslation({
|
|
|
192
235
|
i18nStore
|
|
193
236
|
}) {
|
|
194
237
|
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
238
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
201
239
|
sheetTitle,
|
|
202
240
|
i18nStore,
|
|
203
241
|
"server"
|
|
204
242
|
);
|
|
205
|
-
|
|
243
|
+
const translationService = new TranslationService(intlInstance);
|
|
244
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
206
245
|
};
|
|
207
246
|
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
247
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
214
248
|
sheetTitle,
|
|
215
249
|
i18nStore,
|
|
216
250
|
"server"
|
|
217
251
|
);
|
|
218
|
-
|
|
252
|
+
const translationService = new TranslationService(intlInstance);
|
|
253
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
219
254
|
};
|
|
220
255
|
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
256
|
const intlInitPromise = new Promise((resolve) => {
|
|
227
257
|
setTimeout(() => {
|
|
228
258
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
@@ -235,27 +265,11 @@ function getTranslation({
|
|
|
235
265
|
});
|
|
236
266
|
return new Promise((resolve) => {
|
|
237
267
|
intlInitPromise.then((intlInstance) => {
|
|
238
|
-
|
|
239
|
-
resolve(
|
|
268
|
+
const translationService = new TranslationService(intlInstance);
|
|
269
|
+
resolve(translationService.translate(id, values, opts, _descriptor));
|
|
240
270
|
});
|
|
241
271
|
});
|
|
242
272
|
};
|
|
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
273
|
return { t };
|
|
260
274
|
}
|
|
261
275
|
|
package/dist/index.mjs
CHANGED
|
@@ -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,46 @@ var IntlInstanceCache = class _IntlInstanceCache {
|
|
|
128
139
|
};
|
|
129
140
|
var intlInstanceCache = IntlInstanceCache.init();
|
|
130
141
|
|
|
142
|
+
// src/Service/TranslationService.ts
|
|
143
|
+
var TranslationService = class {
|
|
144
|
+
constructor(intlInstance) {
|
|
145
|
+
this.intlInstance = intlInstance;
|
|
146
|
+
}
|
|
147
|
+
translate(id, values, opts, _descriptor) {
|
|
148
|
+
const descriptor = this.createDescriptor(id, _descriptor);
|
|
149
|
+
const targetTranslation = this.findTargetTranslation(id, {
|
|
150
|
+
descriptor,
|
|
151
|
+
values,
|
|
152
|
+
opts
|
|
153
|
+
});
|
|
154
|
+
return targetTranslation;
|
|
155
|
+
}
|
|
156
|
+
/** helpers */
|
|
157
|
+
createDescriptor(id, _descriptor) {
|
|
158
|
+
var _a;
|
|
159
|
+
return __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
160
|
+
id,
|
|
161
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
findTargetTranslation(id, $tParams) {
|
|
165
|
+
var _a, _b, _c, _d, _e;
|
|
166
|
+
const targetTranslation = (_b = (_a = this == null ? void 0 : this.intlInstance) == null ? void 0 : _a.messages) == null ? void 0 : _b[id];
|
|
167
|
+
if (typeof targetTranslation === "string" && targetTranslation.trim() === "") {
|
|
168
|
+
return "";
|
|
169
|
+
}
|
|
170
|
+
if (!$tParams || !($tParams == null ? void 0 : $tParams.descriptor)) {
|
|
171
|
+
return "";
|
|
172
|
+
}
|
|
173
|
+
return (_e = (_d = (_c = this.intlInstance) == null ? void 0 : _c.$t) == null ? void 0 : _d.call(
|
|
174
|
+
_c,
|
|
175
|
+
$tParams.descriptor,
|
|
176
|
+
$tParams.values,
|
|
177
|
+
$tParams.opts
|
|
178
|
+
)) != null ? _e : "";
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
131
182
|
// src/useTranslation.ts
|
|
132
183
|
function useTranslation({
|
|
133
184
|
sheetTitle,
|
|
@@ -138,20 +189,12 @@ function useTranslation({
|
|
|
138
189
|
i18nStore
|
|
139
190
|
);
|
|
140
191
|
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 : "";
|
|
192
|
+
const translationService = new TranslationService(intlInstance);
|
|
193
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
147
194
|
};
|
|
148
195
|
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 : "";
|
|
196
|
+
const translationService = new TranslationService(intlInstance);
|
|
197
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
155
198
|
};
|
|
156
199
|
return { t };
|
|
157
200
|
}
|
|
@@ -169,37 +212,24 @@ function getTranslation({
|
|
|
169
212
|
i18nStore
|
|
170
213
|
}) {
|
|
171
214
|
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
215
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
178
216
|
sheetTitle,
|
|
179
217
|
i18nStore,
|
|
180
218
|
"server"
|
|
181
219
|
);
|
|
182
|
-
|
|
220
|
+
const translationService = new TranslationService(intlInstance);
|
|
221
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
183
222
|
};
|
|
184
223
|
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
224
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
191
225
|
sheetTitle,
|
|
192
226
|
i18nStore,
|
|
193
227
|
"server"
|
|
194
228
|
);
|
|
195
|
-
|
|
229
|
+
const translationService = new TranslationService(intlInstance);
|
|
230
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
196
231
|
};
|
|
197
232
|
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
233
|
const intlInitPromise = new Promise((resolve) => {
|
|
204
234
|
setTimeout(() => {
|
|
205
235
|
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
@@ -212,27 +242,11 @@ function getTranslation({
|
|
|
212
242
|
});
|
|
213
243
|
return new Promise((resolve) => {
|
|
214
244
|
intlInitPromise.then((intlInstance) => {
|
|
215
|
-
|
|
216
|
-
resolve(
|
|
245
|
+
const translationService = new TranslationService(intlInstance);
|
|
246
|
+
resolve(translationService.translate(id, values, opts, _descriptor));
|
|
217
247
|
});
|
|
218
248
|
});
|
|
219
249
|
};
|
|
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
250
|
return { t };
|
|
237
251
|
}
|
|
238
252
|
|
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.9",
|
|
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/
|
|
31
|
-
"@sheet-i18n/
|
|
32
|
-
"@sheet-i18n/
|
|
30
|
+
"@sheet-i18n/errors": "1.3.8",
|
|
31
|
+
"@sheet-i18n/shared-utils": "1.3.8",
|
|
32
|
+
"@sheet-i18n/react-core": "1.0.6"
|
|
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.8"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "tsup",
|