@sheet-i18n/react-client 1.0.1 → 1.0.3
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 +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +136 -14
- package/dist/index.mjs +137 -19
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -2,10 +2,24 @@ 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$1<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
6
|
+
type $TParams$1 = Partial<UseIntlParams$1>;
|
|
7
|
+
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$1[0], TOpts extends $TParams$1[1], TDescriptor extends $TParams$1[2]>(id: TMessageId, values?: TValues, opts?: TOpts, _descriptor?: TDescriptor) => TTypeSafe extends true ? Promise<TMessageId> : any;
|
|
9
|
+
} & {
|
|
10
|
+
t: {
|
|
11
|
+
dynamic: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, TValues extends $TParams$1[0], TOpts extends $TParams$1[1], TDescriptor extends $TParams$1[2]>(id: string, values?: TValues, opts?: TOpts, _descriptor?: TDescriptor) => TTypeSafe extends true ? Promise<TMessageId> : any;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
|
|
5
15
|
type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
6
16
|
type $TParams = Partial<UseIntlParams>;
|
|
7
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> = {
|
|
8
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;
|
|
19
|
+
} & {
|
|
20
|
+
t: {
|
|
21
|
+
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;
|
|
22
|
+
};
|
|
9
23
|
};
|
|
10
24
|
|
|
11
25
|
type IntlProviderProps<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>, TTypeSafe extends boolean = true> = {
|
|
@@ -20,6 +34,7 @@ declare function createI18nContext<TSupportedLocales extends readonly string[],
|
|
|
20
34
|
children?: React.ReactNode;
|
|
21
35
|
}) => react_jsx_runtime.JSX.Element;
|
|
22
36
|
useTranslation: <TSheetTitle extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] : string>(sheetTitle: TSheetTitle) => UseTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle>;
|
|
37
|
+
getTranslation: <TSheetTitle extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] : string>(sheetTitle: TSheetTitle) => GetTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle>;
|
|
23
38
|
};
|
|
24
39
|
|
|
25
40
|
export { createI18nContext };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,10 +2,24 @@ 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$1<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
6
|
+
type $TParams$1 = Partial<UseIntlParams$1>;
|
|
7
|
+
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$1[0], TOpts extends $TParams$1[1], TDescriptor extends $TParams$1[2]>(id: TMessageId, values?: TValues, opts?: TOpts, _descriptor?: TDescriptor) => TTypeSafe extends true ? Promise<TMessageId> : any;
|
|
9
|
+
} & {
|
|
10
|
+
t: {
|
|
11
|
+
dynamic: <TMessageId extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]][TSheetTitle] : string, TValues extends $TParams$1[0], TOpts extends $TParams$1[1], TDescriptor extends $TParams$1[2]>(id: string, values?: TValues, opts?: TOpts, _descriptor?: TDescriptor) => TTypeSafe extends true ? Promise<TMessageId> : any;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
|
|
5
15
|
type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
6
16
|
type $TParams = Partial<UseIntlParams>;
|
|
7
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> = {
|
|
8
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;
|
|
19
|
+
} & {
|
|
20
|
+
t: {
|
|
21
|
+
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;
|
|
22
|
+
};
|
|
9
23
|
};
|
|
10
24
|
|
|
11
25
|
type IntlProviderProps<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>, TTypeSafe extends boolean = true> = {
|
|
@@ -20,6 +34,7 @@ declare function createI18nContext<TSupportedLocales extends readonly string[],
|
|
|
20
34
|
children?: React.ReactNode;
|
|
21
35
|
}) => react_jsx_runtime.JSX.Element;
|
|
22
36
|
useTranslation: <TSheetTitle extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] : string>(sheetTitle: TSheetTitle) => UseTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle>;
|
|
37
|
+
getTranslation: <TSheetTitle extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] : string>(sheetTitle: TSheetTitle) => GetTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle>;
|
|
23
38
|
};
|
|
24
39
|
|
|
25
40
|
export { createI18nContext };
|
package/dist/index.js
CHANGED
|
@@ -43,18 +43,23 @@ __export(src_exports, {
|
|
|
43
43
|
module.exports = __toCommonJS(src_exports);
|
|
44
44
|
|
|
45
45
|
// src/createI18nContext.tsx
|
|
46
|
-
var
|
|
46
|
+
var import_shared_utils2 = require("@sheet-i18n/shared-utils");
|
|
47
47
|
var import_react_core = require("@sheet-i18n/react-core");
|
|
48
48
|
|
|
49
49
|
// src/IntlProvider.tsx
|
|
50
50
|
var import_react_intl = require("react-intl");
|
|
51
|
+
var import_react = require("react");
|
|
51
52
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
52
53
|
function IntlProvider({
|
|
53
54
|
i18nStore,
|
|
54
55
|
currentLocale,
|
|
55
56
|
children
|
|
56
57
|
}) {
|
|
57
|
-
|
|
58
|
+
var _a;
|
|
59
|
+
const locale = (_a = currentLocale != null ? currentLocale : detectClientLanguage(i18nStore)) != null ? _a : i18nStore.defaultLocale;
|
|
60
|
+
(0, import_react.useEffect)(() => {
|
|
61
|
+
i18nStore.setCurrentLocale(locale);
|
|
62
|
+
}, [locale]);
|
|
58
63
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
59
64
|
import_react_intl.IntlProvider,
|
|
60
65
|
{
|
|
@@ -75,22 +80,93 @@ function detectClientLanguage(i18nStore) {
|
|
|
75
80
|
return defaultLocale != null ? defaultLocale : "";
|
|
76
81
|
}
|
|
77
82
|
|
|
78
|
-
// src/
|
|
83
|
+
// src/Service/IntlInstanceCache.ts
|
|
79
84
|
var import_react_intl2 = require("react-intl");
|
|
85
|
+
var import_shared_utils = require("@sheet-i18n/shared-utils");
|
|
80
86
|
var intlCache = (0, import_react_intl2.createIntlCache)();
|
|
87
|
+
var IntlInstanceCache = class _IntlInstanceCache {
|
|
88
|
+
constructor() {
|
|
89
|
+
this.cache = {};
|
|
90
|
+
this.getCachedIntlInstance = (sheetTitle, i18nStore, environment = "client") => {
|
|
91
|
+
const currentLocale = i18nStore.getCurrentLocale();
|
|
92
|
+
const cachedIntlInstance = this.cache[sheetTitle];
|
|
93
|
+
const sameLocale = (cachedIntlInstance == null ? void 0 : cachedIntlInstance.locale) === currentLocale;
|
|
94
|
+
if (cachedIntlInstance && sameLocale) {
|
|
95
|
+
return cachedIntlInstance.intlInstance;
|
|
96
|
+
}
|
|
97
|
+
return environment === "client" ? this.createClientIntlInstance(sheetTitle, i18nStore) : this.createServerIntlInstance(sheetTitle, i18nStore);
|
|
98
|
+
};
|
|
99
|
+
this.getTotalCache = () => {
|
|
100
|
+
return this.cache;
|
|
101
|
+
};
|
|
102
|
+
this.createClientIntlInstance = (sheetTitle, i18nStore) => {
|
|
103
|
+
const currentIntl = (0, import_react_intl2.useIntl)();
|
|
104
|
+
const currentLocale = i18nStore.getCurrentLocale();
|
|
105
|
+
const targetMessages = i18nStore.getTargetMessages(sheetTitle);
|
|
106
|
+
const newIntl = (0, import_react_intl2.createIntl)(
|
|
107
|
+
__spreadProps(__spreadValues({}, currentIntl), { messages: targetMessages }),
|
|
108
|
+
intlCache
|
|
109
|
+
);
|
|
110
|
+
if (currentLocale) {
|
|
111
|
+
this.setCachedIntlInstance(sheetTitle, currentLocale, newIntl);
|
|
112
|
+
}
|
|
113
|
+
return newIntl;
|
|
114
|
+
};
|
|
115
|
+
this.createServerIntlInstance = (sheetTitle, i18nStore) => {
|
|
116
|
+
const currentLocale = i18nStore.getCurrentLocale();
|
|
117
|
+
const targetMessages = i18nStore.getTargetMessages(sheetTitle);
|
|
118
|
+
if (!i18nStore || !currentLocale) return null;
|
|
119
|
+
const newIntl = (0, import_react_intl2.createIntl)(
|
|
120
|
+
{ locale: currentLocale, messages: targetMessages },
|
|
121
|
+
intlCache
|
|
122
|
+
);
|
|
123
|
+
if (currentLocale) {
|
|
124
|
+
this.setCachedIntlInstance(sheetTitle, currentLocale, newIntl);
|
|
125
|
+
}
|
|
126
|
+
return newIntl;
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
static init() {
|
|
130
|
+
if (!_IntlInstanceCache.instance) {
|
|
131
|
+
_IntlInstanceCache.instance = new _IntlInstanceCache();
|
|
132
|
+
}
|
|
133
|
+
return _IntlInstanceCache.instance;
|
|
134
|
+
}
|
|
135
|
+
setCachedIntlInstance(sheetTitle, currentLocale, intlInstance) {
|
|
136
|
+
if (import_shared_utils.validator.isNullish(this.cache[sheetTitle])) {
|
|
137
|
+
this.cache[sheetTitle] = {
|
|
138
|
+
locale: currentLocale,
|
|
139
|
+
intlInstance
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
var intlInstanceCache = IntlInstanceCache.init();
|
|
145
|
+
|
|
146
|
+
// src/useTranslation.ts
|
|
81
147
|
function useTranslation({
|
|
82
|
-
sheetTitle
|
|
148
|
+
sheetTitle,
|
|
149
|
+
i18nStore
|
|
83
150
|
}) {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const newIntl = (0, import_react_intl2.createIntl)(
|
|
88
|
-
__spreadProps(__spreadValues({}, currentIntl), { messages: targetMessages }),
|
|
89
|
-
intlCache
|
|
151
|
+
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
152
|
+
sheetTitle,
|
|
153
|
+
i18nStore
|
|
90
154
|
);
|
|
91
155
|
const t = (id, values, opts, _descriptor) => {
|
|
92
|
-
|
|
93
|
-
|
|
156
|
+
var _a;
|
|
157
|
+
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
158
|
+
id,
|
|
159
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
160
|
+
});
|
|
161
|
+
return intlInstance ? intlInstance.$t(descriptor, values, opts) : id;
|
|
162
|
+
};
|
|
163
|
+
t.dynamic = (id, values, opts, _descriptor) => {
|
|
164
|
+
var _a;
|
|
165
|
+
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
166
|
+
id,
|
|
167
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
168
|
+
});
|
|
169
|
+
return intlInstance ? intlInstance.$t(descriptor, values, opts) : id;
|
|
94
170
|
};
|
|
95
171
|
return { t };
|
|
96
172
|
}
|
|
@@ -102,10 +178,54 @@ var InvalidI18nContextStateError = class extends import_errors.CustomError {
|
|
|
102
178
|
var IsNotInstanceOfI18nStoreError = class extends import_errors.CustomError {
|
|
103
179
|
};
|
|
104
180
|
|
|
181
|
+
// src/getTranslation.ts
|
|
182
|
+
function getTranslation({
|
|
183
|
+
sheetTitle,
|
|
184
|
+
i18nStore
|
|
185
|
+
}) {
|
|
186
|
+
const intlInitPromise = new Promise((resolve) => {
|
|
187
|
+
setTimeout(() => {
|
|
188
|
+
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
189
|
+
sheetTitle,
|
|
190
|
+
i18nStore,
|
|
191
|
+
"server"
|
|
192
|
+
);
|
|
193
|
+
resolve(intlInstance);
|
|
194
|
+
}, 1);
|
|
195
|
+
});
|
|
196
|
+
const t = (id, values, opts, _descriptor) => {
|
|
197
|
+
var _a;
|
|
198
|
+
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
199
|
+
id,
|
|
200
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
201
|
+
});
|
|
202
|
+
return new Promise((resolve) => {
|
|
203
|
+
intlInitPromise.then((intlInstance) => {
|
|
204
|
+
var _a2, _b;
|
|
205
|
+
resolve((_b = (_a2 = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _a2.call(intlInstance, descriptor, values, opts)) != null ? _b : id);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
};
|
|
209
|
+
t.dynamic = (id, values, opts, _descriptor) => {
|
|
210
|
+
var _a;
|
|
211
|
+
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
212
|
+
id,
|
|
213
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
214
|
+
});
|
|
215
|
+
return new Promise((resolve) => {
|
|
216
|
+
intlInitPromise.then((intlInstance) => {
|
|
217
|
+
var _a2, _b;
|
|
218
|
+
resolve((_b = (_a2 = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _a2.call(intlInstance, descriptor, values, opts)) != null ? _b : id);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
return { t };
|
|
223
|
+
}
|
|
224
|
+
|
|
105
225
|
// src/createI18nContext.tsx
|
|
106
226
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
107
227
|
function createI18nContext(i18nStore) {
|
|
108
|
-
if (
|
|
228
|
+
if (import_shared_utils2.validator.isNullish(i18nStore)) {
|
|
109
229
|
throw new InvalidI18nContextStateError(
|
|
110
230
|
"\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
|
|
111
231
|
);
|
|
@@ -127,9 +247,11 @@ function createI18nContext(i18nStore) {
|
|
|
127
247
|
}
|
|
128
248
|
);
|
|
129
249
|
const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore });
|
|
250
|
+
const getTranslationImpl = (sheetTitle) => getTranslation({ sheetTitle, i18nStore });
|
|
130
251
|
return {
|
|
131
252
|
IntlProvider: IntlProviderImpl,
|
|
132
|
-
useTranslation: useTranslationImpl
|
|
253
|
+
useTranslation: useTranslationImpl,
|
|
254
|
+
getTranslation: getTranslationImpl
|
|
133
255
|
};
|
|
134
256
|
}
|
|
135
257
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.mjs
CHANGED
|
@@ -20,18 +20,23 @@ var __spreadValues = (a, b) => {
|
|
|
20
20
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
21
21
|
|
|
22
22
|
// src/createI18nContext.tsx
|
|
23
|
-
import { validator } from "@sheet-i18n/shared-utils";
|
|
23
|
+
import { validator as validator2 } from "@sheet-i18n/shared-utils";
|
|
24
24
|
import { I18nStore } from "@sheet-i18n/react-core";
|
|
25
25
|
|
|
26
26
|
// src/IntlProvider.tsx
|
|
27
27
|
import { IntlProvider as ReactIntlProvider } from "react-intl";
|
|
28
|
+
import { useEffect } from "react";
|
|
28
29
|
import { jsx } from "react/jsx-runtime";
|
|
29
30
|
function IntlProvider({
|
|
30
31
|
i18nStore,
|
|
31
32
|
currentLocale,
|
|
32
33
|
children
|
|
33
34
|
}) {
|
|
34
|
-
|
|
35
|
+
var _a;
|
|
36
|
+
const locale = (_a = currentLocale != null ? currentLocale : detectClientLanguage(i18nStore)) != null ? _a : i18nStore.defaultLocale;
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
i18nStore.setCurrentLocale(locale);
|
|
39
|
+
}, [locale]);
|
|
35
40
|
return /* @__PURE__ */ jsx(
|
|
36
41
|
ReactIntlProvider,
|
|
37
42
|
{
|
|
@@ -52,26 +57,93 @@ function detectClientLanguage(i18nStore) {
|
|
|
52
57
|
return defaultLocale != null ? defaultLocale : "";
|
|
53
58
|
}
|
|
54
59
|
|
|
55
|
-
// src/
|
|
56
|
-
import {
|
|
57
|
-
|
|
58
|
-
createIntl,
|
|
59
|
-
useIntl
|
|
60
|
-
} from "react-intl";
|
|
60
|
+
// src/Service/IntlInstanceCache.ts
|
|
61
|
+
import { createIntl, createIntlCache, useIntl } from "react-intl";
|
|
62
|
+
import { validator } from "@sheet-i18n/shared-utils";
|
|
61
63
|
var intlCache = createIntlCache();
|
|
64
|
+
var IntlInstanceCache = class _IntlInstanceCache {
|
|
65
|
+
constructor() {
|
|
66
|
+
this.cache = {};
|
|
67
|
+
this.getCachedIntlInstance = (sheetTitle, i18nStore, environment = "client") => {
|
|
68
|
+
const currentLocale = i18nStore.getCurrentLocale();
|
|
69
|
+
const cachedIntlInstance = this.cache[sheetTitle];
|
|
70
|
+
const sameLocale = (cachedIntlInstance == null ? void 0 : cachedIntlInstance.locale) === currentLocale;
|
|
71
|
+
if (cachedIntlInstance && sameLocale) {
|
|
72
|
+
return cachedIntlInstance.intlInstance;
|
|
73
|
+
}
|
|
74
|
+
return environment === "client" ? this.createClientIntlInstance(sheetTitle, i18nStore) : this.createServerIntlInstance(sheetTitle, i18nStore);
|
|
75
|
+
};
|
|
76
|
+
this.getTotalCache = () => {
|
|
77
|
+
return this.cache;
|
|
78
|
+
};
|
|
79
|
+
this.createClientIntlInstance = (sheetTitle, i18nStore) => {
|
|
80
|
+
const currentIntl = useIntl();
|
|
81
|
+
const currentLocale = i18nStore.getCurrentLocale();
|
|
82
|
+
const targetMessages = i18nStore.getTargetMessages(sheetTitle);
|
|
83
|
+
const newIntl = createIntl(
|
|
84
|
+
__spreadProps(__spreadValues({}, currentIntl), { messages: targetMessages }),
|
|
85
|
+
intlCache
|
|
86
|
+
);
|
|
87
|
+
if (currentLocale) {
|
|
88
|
+
this.setCachedIntlInstance(sheetTitle, currentLocale, newIntl);
|
|
89
|
+
}
|
|
90
|
+
return newIntl;
|
|
91
|
+
};
|
|
92
|
+
this.createServerIntlInstance = (sheetTitle, i18nStore) => {
|
|
93
|
+
const currentLocale = i18nStore.getCurrentLocale();
|
|
94
|
+
const targetMessages = i18nStore.getTargetMessages(sheetTitle);
|
|
95
|
+
if (!i18nStore || !currentLocale) return null;
|
|
96
|
+
const newIntl = createIntl(
|
|
97
|
+
{ locale: currentLocale, messages: targetMessages },
|
|
98
|
+
intlCache
|
|
99
|
+
);
|
|
100
|
+
if (currentLocale) {
|
|
101
|
+
this.setCachedIntlInstance(sheetTitle, currentLocale, newIntl);
|
|
102
|
+
}
|
|
103
|
+
return newIntl;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
static init() {
|
|
107
|
+
if (!_IntlInstanceCache.instance) {
|
|
108
|
+
_IntlInstanceCache.instance = new _IntlInstanceCache();
|
|
109
|
+
}
|
|
110
|
+
return _IntlInstanceCache.instance;
|
|
111
|
+
}
|
|
112
|
+
setCachedIntlInstance(sheetTitle, currentLocale, intlInstance) {
|
|
113
|
+
if (validator.isNullish(this.cache[sheetTitle])) {
|
|
114
|
+
this.cache[sheetTitle] = {
|
|
115
|
+
locale: currentLocale,
|
|
116
|
+
intlInstance
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
var intlInstanceCache = IntlInstanceCache.init();
|
|
122
|
+
|
|
123
|
+
// src/useTranslation.ts
|
|
62
124
|
function useTranslation({
|
|
63
|
-
sheetTitle
|
|
125
|
+
sheetTitle,
|
|
126
|
+
i18nStore
|
|
64
127
|
}) {
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const newIntl = createIntl(
|
|
69
|
-
__spreadProps(__spreadValues({}, currentIntl), { messages: targetMessages }),
|
|
70
|
-
intlCache
|
|
128
|
+
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
129
|
+
sheetTitle,
|
|
130
|
+
i18nStore
|
|
71
131
|
);
|
|
72
132
|
const t = (id, values, opts, _descriptor) => {
|
|
73
|
-
|
|
74
|
-
|
|
133
|
+
var _a;
|
|
134
|
+
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
135
|
+
id,
|
|
136
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
137
|
+
});
|
|
138
|
+
return intlInstance ? intlInstance.$t(descriptor, values, opts) : id;
|
|
139
|
+
};
|
|
140
|
+
t.dynamic = (id, values, opts, _descriptor) => {
|
|
141
|
+
var _a;
|
|
142
|
+
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
143
|
+
id,
|
|
144
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
145
|
+
});
|
|
146
|
+
return intlInstance ? intlInstance.$t(descriptor, values, opts) : id;
|
|
75
147
|
};
|
|
76
148
|
return { t };
|
|
77
149
|
}
|
|
@@ -83,10 +155,54 @@ var InvalidI18nContextStateError = class extends CustomError {
|
|
|
83
155
|
var IsNotInstanceOfI18nStoreError = class extends CustomError {
|
|
84
156
|
};
|
|
85
157
|
|
|
158
|
+
// src/getTranslation.ts
|
|
159
|
+
function getTranslation({
|
|
160
|
+
sheetTitle,
|
|
161
|
+
i18nStore
|
|
162
|
+
}) {
|
|
163
|
+
const intlInitPromise = new Promise((resolve) => {
|
|
164
|
+
setTimeout(() => {
|
|
165
|
+
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
166
|
+
sheetTitle,
|
|
167
|
+
i18nStore,
|
|
168
|
+
"server"
|
|
169
|
+
);
|
|
170
|
+
resolve(intlInstance);
|
|
171
|
+
}, 1);
|
|
172
|
+
});
|
|
173
|
+
const t = (id, values, opts, _descriptor) => {
|
|
174
|
+
var _a;
|
|
175
|
+
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
176
|
+
id,
|
|
177
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
178
|
+
});
|
|
179
|
+
return new Promise((resolve) => {
|
|
180
|
+
intlInitPromise.then((intlInstance) => {
|
|
181
|
+
var _a2, _b;
|
|
182
|
+
resolve((_b = (_a2 = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _a2.call(intlInstance, descriptor, values, opts)) != null ? _b : id);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
t.dynamic = (id, values, opts, _descriptor) => {
|
|
187
|
+
var _a;
|
|
188
|
+
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
189
|
+
id,
|
|
190
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
191
|
+
});
|
|
192
|
+
return new Promise((resolve) => {
|
|
193
|
+
intlInitPromise.then((intlInstance) => {
|
|
194
|
+
var _a2, _b;
|
|
195
|
+
resolve((_b = (_a2 = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _a2.call(intlInstance, descriptor, values, opts)) != null ? _b : id);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
};
|
|
199
|
+
return { t };
|
|
200
|
+
}
|
|
201
|
+
|
|
86
202
|
// src/createI18nContext.tsx
|
|
87
203
|
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
88
204
|
function createI18nContext(i18nStore) {
|
|
89
|
-
if (
|
|
205
|
+
if (validator2.isNullish(i18nStore)) {
|
|
90
206
|
throw new InvalidI18nContextStateError(
|
|
91
207
|
"\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
|
|
92
208
|
);
|
|
@@ -108,9 +224,11 @@ function createI18nContext(i18nStore) {
|
|
|
108
224
|
}
|
|
109
225
|
);
|
|
110
226
|
const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore });
|
|
227
|
+
const getTranslationImpl = (sheetTitle) => getTranslation({ sheetTitle, i18nStore });
|
|
111
228
|
return {
|
|
112
229
|
IntlProvider: IntlProviderImpl,
|
|
113
|
-
useTranslation: useTranslationImpl
|
|
230
|
+
useTranslation: useTranslationImpl,
|
|
231
|
+
getTranslation: getTranslationImpl
|
|
114
232
|
};
|
|
115
233
|
}
|
|
116
234
|
export {
|
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.3",
|
|
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/errors": "1.3.
|
|
32
|
-
"@sheet-i18n/react-core": "1.0.
|
|
30
|
+
"@sheet-i18n/shared-utils": "1.3.4",
|
|
31
|
+
"@sheet-i18n/errors": "1.3.4",
|
|
32
|
+
"@sheet-i18n/react-core": "1.0.2"
|
|
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.4"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "tsup",
|