@sheet-i18n/react-client 1.0.0 → 1.0.2
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 +16 -2
- package/dist/index.d.ts +16 -2
- package/dist/index.js +121 -14
- package/dist/index.mjs +122 -19
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -2,11 +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 ? 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 ? string : 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
|
-
type CustomReactNode = Exclude<React.ReactNode, bigint | null | undefined>;
|
|
8
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> = {
|
|
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 ?
|
|
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
|
+
};
|
|
10
23
|
};
|
|
11
24
|
|
|
12
25
|
type IntlProviderProps<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>, TTypeSafe extends boolean = true> = {
|
|
@@ -21,6 +34,7 @@ declare function createI18nContext<TSupportedLocales extends readonly string[],
|
|
|
21
34
|
children?: React.ReactNode;
|
|
22
35
|
}) => react_jsx_runtime.JSX.Element;
|
|
23
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>;
|
|
24
38
|
};
|
|
25
39
|
|
|
26
40
|
export { createI18nContext };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,11 +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 ? 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 ? string : 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
|
-
type CustomReactNode = Exclude<React.ReactNode, bigint | null | undefined>;
|
|
8
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> = {
|
|
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 ?
|
|
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
|
+
};
|
|
10
23
|
};
|
|
11
24
|
|
|
12
25
|
type IntlProviderProps<TSupportedLocales extends readonly string[], TLocaleSet extends Record<TSupportedLocales[number], Record<string, any>>, TTypeSafe extends boolean = true> = {
|
|
@@ -21,6 +34,7 @@ declare function createI18nContext<TSupportedLocales extends readonly string[],
|
|
|
21
34
|
children?: React.ReactNode;
|
|
22
35
|
}) => react_jsx_runtime.JSX.Element;
|
|
23
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>;
|
|
24
38
|
};
|
|
25
39
|
|
|
26
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,39 @@ 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 intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
187
|
+
sheetTitle,
|
|
188
|
+
i18nStore,
|
|
189
|
+
"server"
|
|
190
|
+
);
|
|
191
|
+
const t = (id, values, opts, _descriptor) => {
|
|
192
|
+
var _a, _b, _c;
|
|
193
|
+
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
194
|
+
id,
|
|
195
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
196
|
+
});
|
|
197
|
+
return (_c = (_b = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _b.call(intlInstance, descriptor, values, opts)) != null ? _c : id;
|
|
198
|
+
};
|
|
199
|
+
t.dynamic = (id, values, opts, _descriptor) => {
|
|
200
|
+
var _a, _b, _c;
|
|
201
|
+
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
202
|
+
id,
|
|
203
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
204
|
+
});
|
|
205
|
+
return (_c = (_b = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _b.call(intlInstance, descriptor, values, opts)) != null ? _c : id;
|
|
206
|
+
};
|
|
207
|
+
return { t };
|
|
208
|
+
}
|
|
209
|
+
|
|
105
210
|
// src/createI18nContext.tsx
|
|
106
211
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
107
212
|
function createI18nContext(i18nStore) {
|
|
108
|
-
if (
|
|
213
|
+
if (import_shared_utils2.validator.isNullish(i18nStore)) {
|
|
109
214
|
throw new InvalidI18nContextStateError(
|
|
110
215
|
"\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
|
|
111
216
|
);
|
|
@@ -127,9 +232,11 @@ function createI18nContext(i18nStore) {
|
|
|
127
232
|
}
|
|
128
233
|
);
|
|
129
234
|
const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore });
|
|
235
|
+
const getTranslationImpl = (sheetTitle) => getTranslation({ sheetTitle, i18nStore });
|
|
130
236
|
return {
|
|
131
237
|
IntlProvider: IntlProviderImpl,
|
|
132
|
-
useTranslation: useTranslationImpl
|
|
238
|
+
useTranslation: useTranslationImpl,
|
|
239
|
+
getTranslation: getTranslationImpl
|
|
133
240
|
};
|
|
134
241
|
}
|
|
135
242
|
// 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,39 @@ 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 intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
164
|
+
sheetTitle,
|
|
165
|
+
i18nStore,
|
|
166
|
+
"server"
|
|
167
|
+
);
|
|
168
|
+
const t = (id, values, opts, _descriptor) => {
|
|
169
|
+
var _a, _b, _c;
|
|
170
|
+
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
171
|
+
id,
|
|
172
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
173
|
+
});
|
|
174
|
+
return (_c = (_b = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _b.call(intlInstance, descriptor, values, opts)) != null ? _c : id;
|
|
175
|
+
};
|
|
176
|
+
t.dynamic = (id, values, opts, _descriptor) => {
|
|
177
|
+
var _a, _b, _c;
|
|
178
|
+
const descriptor = __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
179
|
+
id,
|
|
180
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
181
|
+
});
|
|
182
|
+
return (_c = (_b = intlInstance == null ? void 0 : intlInstance.$t) == null ? void 0 : _b.call(intlInstance, descriptor, values, opts)) != null ? _c : id;
|
|
183
|
+
};
|
|
184
|
+
return { t };
|
|
185
|
+
}
|
|
186
|
+
|
|
86
187
|
// src/createI18nContext.tsx
|
|
87
188
|
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
88
189
|
function createI18nContext(i18nStore) {
|
|
89
|
-
if (
|
|
190
|
+
if (validator2.isNullish(i18nStore)) {
|
|
90
191
|
throw new InvalidI18nContextStateError(
|
|
91
192
|
"\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
|
|
92
193
|
);
|
|
@@ -108,9 +209,11 @@ function createI18nContext(i18nStore) {
|
|
|
108
209
|
}
|
|
109
210
|
);
|
|
110
211
|
const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore });
|
|
212
|
+
const getTranslationImpl = (sheetTitle) => getTranslation({ sheetTitle, i18nStore });
|
|
111
213
|
return {
|
|
112
214
|
IntlProvider: IntlProviderImpl,
|
|
113
|
-
useTranslation: useTranslationImpl
|
|
215
|
+
useTranslation: useTranslationImpl,
|
|
216
|
+
getTranslation: getTranslationImpl
|
|
114
217
|
};
|
|
115
218
|
}
|
|
116
219
|
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.2",
|
|
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.3",
|
|
31
|
+
"@sheet-i18n/errors": "1.3.3",
|
|
32
|
+
"@sheet-i18n/react-core": "1.0.1"
|
|
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.3"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "tsup",
|