@sheet-i18n/react-client 1.1.0 → 1.2.0-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +99 -57
- package/dist/index.mjs +93 -51
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -5,7 +5,7 @@ import { I18nStore } from '@sheet-i18n/react-core';
|
|
|
5
5
|
type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
6
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
|
+
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]] extends never ? string : keyof TLocaleSet[TSupportedLocales[number]] : string> = {
|
|
9
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;
|
|
10
10
|
} & {
|
|
11
11
|
t: {
|
|
@@ -14,7 +14,7 @@ type GetTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSe
|
|
|
14
14
|
};
|
|
15
15
|
};
|
|
16
16
|
|
|
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> = {
|
|
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]] extends never ? string : keyof TLocaleSet[TSupportedLocales[number]] : string> = {
|
|
18
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
19
|
} & {
|
|
20
20
|
t: {
|
|
@@ -33,8 +33,8 @@ declare function createI18nContext<TSupportedLocales extends readonly string[],
|
|
|
33
33
|
currentLocale?: string;
|
|
34
34
|
children?: React.ReactNode;
|
|
35
35
|
}) => react_jsx_runtime.JSX.Element;
|
|
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>;
|
|
36
|
+
useTranslation: <TSheetTitle extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] extends never ? string : keyof TLocaleSet[TSupportedLocales[number]] : string>(sheetTitle: TSheetTitle) => UseTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle>;
|
|
37
|
+
getTranslation: <TSheetTitle extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] extends never ? string : keyof TLocaleSet[TSupportedLocales[number]] : string>(sheetTitle: TSheetTitle) => GetTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle>;
|
|
38
38
|
};
|
|
39
39
|
|
|
40
40
|
export { createI18nContext };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { I18nStore } from '@sheet-i18n/react-core';
|
|
|
5
5
|
type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
6
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
|
+
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]] extends never ? string : keyof TLocaleSet[TSupportedLocales[number]] : string> = {
|
|
9
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;
|
|
10
10
|
} & {
|
|
11
11
|
t: {
|
|
@@ -14,7 +14,7 @@ type GetTranslationReturn<TSupportedLocales extends readonly string[], TLocaleSe
|
|
|
14
14
|
};
|
|
15
15
|
};
|
|
16
16
|
|
|
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> = {
|
|
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]] extends never ? string : keyof TLocaleSet[TSupportedLocales[number]] : string> = {
|
|
18
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
19
|
} & {
|
|
20
20
|
t: {
|
|
@@ -33,8 +33,8 @@ declare function createI18nContext<TSupportedLocales extends readonly string[],
|
|
|
33
33
|
currentLocale?: string;
|
|
34
34
|
children?: React.ReactNode;
|
|
35
35
|
}) => react_jsx_runtime.JSX.Element;
|
|
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>;
|
|
36
|
+
useTranslation: <TSheetTitle extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] extends never ? string : keyof TLocaleSet[TSupportedLocales[number]] : string>(sheetTitle: TSheetTitle) => UseTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle>;
|
|
37
|
+
getTranslation: <TSheetTitle extends TTypeSafe extends true ? keyof TLocaleSet[TSupportedLocales[number]] extends never ? string : keyof TLocaleSet[TSupportedLocales[number]] : string>(sheetTitle: TSheetTitle) => GetTranslationReturn<TSupportedLocales, TLocaleSet, TTypeSafe, TSheetTitle>;
|
|
38
38
|
};
|
|
39
39
|
|
|
40
40
|
export { createI18nContext };
|
package/dist/index.js
CHANGED
|
@@ -43,64 +43,21 @@ __export(src_exports, {
|
|
|
43
43
|
module.exports = __toCommonJS(src_exports);
|
|
44
44
|
|
|
45
45
|
// src/createI18nContext.tsx
|
|
46
|
-
var
|
|
46
|
+
var import_shared_utils4 = require("@sheet-i18n/shared-utils");
|
|
47
47
|
var import_react_core = require("@sheet-i18n/react-core");
|
|
48
48
|
|
|
49
49
|
// src/IntlProvider.tsx
|
|
50
|
-
var
|
|
50
|
+
var import_react_intl2 = require("react-intl");
|
|
51
|
+
var import_react2 = require("react");
|
|
52
|
+
|
|
53
|
+
// src/useDynamicLocale.ts
|
|
54
|
+
var import_shared_utils2 = require("@sheet-i18n/shared-utils");
|
|
51
55
|
var import_react = require("react");
|
|
52
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
53
|
-
function IntlProvider({
|
|
54
|
-
i18nStore,
|
|
55
|
-
currentLocale,
|
|
56
|
-
children
|
|
57
|
-
}) {
|
|
58
|
-
const { locale } = useIntlLocale({ i18nStore, currentLocale });
|
|
59
|
-
const onIntlError = (err) => {
|
|
60
|
-
const typeSafe = i18nStore.typeSafe;
|
|
61
|
-
if (typeSafe) {
|
|
62
|
-
console.error(err);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
66
|
-
import_react_intl.IntlProvider,
|
|
67
|
-
{
|
|
68
|
-
locale,
|
|
69
|
-
messages: i18nStore == null ? void 0 : i18nStore.localeSet[locale],
|
|
70
|
-
onError: onIntlError,
|
|
71
|
-
children
|
|
72
|
-
}
|
|
73
|
-
);
|
|
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
|
-
}
|
|
89
|
-
function detectClientLanguage(i18nStore) {
|
|
90
|
-
const { defaultLocale, supportedLocales } = i18nStore;
|
|
91
|
-
if (typeof navigator !== "undefined" && (navigator == null ? void 0 : navigator.languages)) {
|
|
92
|
-
const preferredLocale = navigator.languages.find(
|
|
93
|
-
(lang) => supportedLocales == null ? void 0 : supportedLocales.includes(lang)
|
|
94
|
-
);
|
|
95
|
-
return preferredLocale != null ? preferredLocale : defaultLocale;
|
|
96
|
-
}
|
|
97
|
-
return defaultLocale != null ? defaultLocale : "";
|
|
98
|
-
}
|
|
99
56
|
|
|
100
57
|
// src/Service/IntlInstanceCache.ts
|
|
101
|
-
var
|
|
58
|
+
var import_react_intl = require("react-intl");
|
|
102
59
|
var import_shared_utils = require("@sheet-i18n/shared-utils");
|
|
103
|
-
var intlCache = (0,
|
|
60
|
+
var intlCache = (0, import_react_intl.createIntlCache)();
|
|
104
61
|
var IntlInstanceCache = class _IntlInstanceCache {
|
|
105
62
|
constructor() {
|
|
106
63
|
this.cache = {};
|
|
@@ -116,11 +73,14 @@ var IntlInstanceCache = class _IntlInstanceCache {
|
|
|
116
73
|
this.getTotalCache = () => {
|
|
117
74
|
return this.cache;
|
|
118
75
|
};
|
|
76
|
+
this.resetTotalCache = () => {
|
|
77
|
+
this.cache = {};
|
|
78
|
+
};
|
|
119
79
|
this.createClientIntlInstance = (sheetTitle, i18nStore) => {
|
|
120
|
-
const currentIntl = (0,
|
|
80
|
+
const currentIntl = (0, import_react_intl.useIntl)();
|
|
121
81
|
const currentLocale = i18nStore.getCurrentLocale();
|
|
122
82
|
const targetMessages = i18nStore.getTargetMessages(sheetTitle);
|
|
123
|
-
const newIntl = (0,
|
|
83
|
+
const newIntl = (0, import_react_intl.createIntl)(
|
|
124
84
|
__spreadProps(__spreadValues({}, currentIntl), { messages: targetMessages }),
|
|
125
85
|
intlCache
|
|
126
86
|
);
|
|
@@ -135,7 +95,7 @@ var IntlInstanceCache = class _IntlInstanceCache {
|
|
|
135
95
|
if (!i18nStore || !currentLocale) {
|
|
136
96
|
return null;
|
|
137
97
|
}
|
|
138
|
-
const newIntl = (0,
|
|
98
|
+
const newIntl = (0, import_react_intl.createIntl)(
|
|
139
99
|
{ locale: currentLocale, messages: targetMessages },
|
|
140
100
|
intlCache
|
|
141
101
|
);
|
|
@@ -162,8 +122,90 @@ var IntlInstanceCache = class _IntlInstanceCache {
|
|
|
162
122
|
};
|
|
163
123
|
var intlInstanceCache = IntlInstanceCache.init();
|
|
164
124
|
|
|
125
|
+
// src/useDynamicLocale.ts
|
|
126
|
+
function useDynamicLocale(useDynamicLocaleProps) {
|
|
127
|
+
const { i18nStore } = useDynamicLocaleProps != null ? useDynamicLocaleProps : {};
|
|
128
|
+
const currentLocale = i18nStore == null ? void 0 : i18nStore.getCurrentLocale();
|
|
129
|
+
const [isLoading, setIsLoading] = (0, import_react.useState)(false);
|
|
130
|
+
(0, import_react.useLayoutEffect)(() => {
|
|
131
|
+
var _a;
|
|
132
|
+
if (import_shared_utils2.validator.isNullish(currentLocale)) return;
|
|
133
|
+
const dynamicLoaders = (_a = i18nStore == null ? void 0 : i18nStore.dynamicLoaders) != null ? _a : useDynamicLocaleProps == null ? void 0 : useDynamicLocaleProps.dynamicLoaders;
|
|
134
|
+
if (!dynamicLoaders) return;
|
|
135
|
+
const targetDynamicLoader = dynamicLoaders[currentLocale];
|
|
136
|
+
if (!targetDynamicLoader) return;
|
|
137
|
+
const currentLocaleSet = i18nStore == null ? void 0 : i18nStore.getLocaleSet();
|
|
138
|
+
const targetLocaleData = currentLocaleSet == null ? void 0 : currentLocaleSet[currentLocale];
|
|
139
|
+
if (targetLocaleData) return;
|
|
140
|
+
setIsLoading(true);
|
|
141
|
+
targetDynamicLoader == null ? void 0 : targetDynamicLoader().then((res) => {
|
|
142
|
+
const { default: localeData } = res;
|
|
143
|
+
i18nStore == null ? void 0 : i18nStore.mergeLocaleSet(currentLocale, localeData);
|
|
144
|
+
intlInstanceCache.resetTotalCache();
|
|
145
|
+
setIsLoading(false);
|
|
146
|
+
}).catch((err) => {
|
|
147
|
+
console.log(`\u26A0\uFE0F dynamic loader error:
|
|
148
|
+
${err}`);
|
|
149
|
+
setIsLoading(false);
|
|
150
|
+
});
|
|
151
|
+
}, [currentLocale]);
|
|
152
|
+
return {
|
|
153
|
+
isLoading
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/IntlProvider.tsx
|
|
158
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
159
|
+
function IntlProvider({
|
|
160
|
+
i18nStore,
|
|
161
|
+
currentLocale,
|
|
162
|
+
children
|
|
163
|
+
}) {
|
|
164
|
+
const { locale } = useIntlLocale({ i18nStore, currentLocale });
|
|
165
|
+
const onIntlError = (err) => {
|
|
166
|
+
const typeSafe = i18nStore.typeSafe;
|
|
167
|
+
if (typeSafe) {
|
|
168
|
+
console.error(err);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
const { isLoading } = useDynamicLocale({ i18nStore });
|
|
172
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
173
|
+
import_react_intl2.IntlProvider,
|
|
174
|
+
{
|
|
175
|
+
locale,
|
|
176
|
+
messages: i18nStore == null ? void 0 : i18nStore.localeSet[locale],
|
|
177
|
+
onError: onIntlError,
|
|
178
|
+
children: !isLoading && children
|
|
179
|
+
}
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
function useIntlLocale({
|
|
183
|
+
i18nStore,
|
|
184
|
+
currentLocale
|
|
185
|
+
}) {
|
|
186
|
+
var _a;
|
|
187
|
+
const locale = (_a = currentLocale != null ? currentLocale : detectClientLanguage(i18nStore)) != null ? _a : i18nStore.defaultLocale;
|
|
188
|
+
i18nStore.setCurrentLocale(locale);
|
|
189
|
+
(0, import_react2.useEffect)(() => {
|
|
190
|
+
i18nStore.setCurrentLocale(locale);
|
|
191
|
+
}, [locale]);
|
|
192
|
+
return {
|
|
193
|
+
locale
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function detectClientLanguage(i18nStore) {
|
|
197
|
+
const { defaultLocale, supportedLocales } = i18nStore;
|
|
198
|
+
if (typeof navigator !== "undefined" && (navigator == null ? void 0 : navigator.languages)) {
|
|
199
|
+
const preferredLocale = navigator.languages.find(
|
|
200
|
+
(lang) => supportedLocales == null ? void 0 : supportedLocales.includes(lang)
|
|
201
|
+
);
|
|
202
|
+
return preferredLocale != null ? preferredLocale : defaultLocale;
|
|
203
|
+
}
|
|
204
|
+
return defaultLocale != null ? defaultLocale : "";
|
|
205
|
+
}
|
|
206
|
+
|
|
165
207
|
// src/Service/TranslationService.ts
|
|
166
|
-
var
|
|
208
|
+
var import_shared_utils3 = require("@sheet-i18n/shared-utils");
|
|
167
209
|
var TranslationService = class {
|
|
168
210
|
constructor(intlInstance) {
|
|
169
211
|
this.intlInstance = intlInstance;
|
|
@@ -188,7 +230,7 @@ var TranslationService = class {
|
|
|
188
230
|
findTargetTranslation(id, $tParams) {
|
|
189
231
|
var _a, _b, _c, _d, _e;
|
|
190
232
|
const targetTranslation = (_b = (_a = this == null ? void 0 : this.intlInstance) == null ? void 0 : _a.messages) == null ? void 0 : _b[id];
|
|
191
|
-
if (
|
|
233
|
+
if (import_shared_utils3.validator.isNullish(targetTranslation)) {
|
|
192
234
|
return id;
|
|
193
235
|
}
|
|
194
236
|
if (typeof targetTranslation === "string" && targetTranslation.trim() === "") {
|
|
@@ -280,7 +322,7 @@ function getTranslation({
|
|
|
280
322
|
// src/createI18nContext.tsx
|
|
281
323
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
282
324
|
function createI18nContext(i18nStore) {
|
|
283
|
-
if (
|
|
325
|
+
if (import_shared_utils4.validator.isNullish(i18nStore)) {
|
|
284
326
|
throw new InvalidI18nContextStateError(
|
|
285
327
|
"\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
|
|
286
328
|
);
|
package/dist/index.mjs
CHANGED
|
@@ -20,59 +20,16 @@ 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 validator4 } 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
28
|
import { useEffect } from "react";
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
children
|
|
34
|
-
}) {
|
|
35
|
-
const { locale } = useIntlLocale({ i18nStore, currentLocale });
|
|
36
|
-
const onIntlError = (err) => {
|
|
37
|
-
const typeSafe = i18nStore.typeSafe;
|
|
38
|
-
if (typeSafe) {
|
|
39
|
-
console.error(err);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
return /* @__PURE__ */ jsx(
|
|
43
|
-
ReactIntlProvider,
|
|
44
|
-
{
|
|
45
|
-
locale,
|
|
46
|
-
messages: i18nStore == null ? void 0 : i18nStore.localeSet[locale],
|
|
47
|
-
onError: onIntlError,
|
|
48
|
-
children
|
|
49
|
-
}
|
|
50
|
-
);
|
|
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
|
-
}
|
|
66
|
-
function detectClientLanguage(i18nStore) {
|
|
67
|
-
const { defaultLocale, supportedLocales } = i18nStore;
|
|
68
|
-
if (typeof navigator !== "undefined" && (navigator == null ? void 0 : navigator.languages)) {
|
|
69
|
-
const preferredLocale = navigator.languages.find(
|
|
70
|
-
(lang) => supportedLocales == null ? void 0 : supportedLocales.includes(lang)
|
|
71
|
-
);
|
|
72
|
-
return preferredLocale != null ? preferredLocale : defaultLocale;
|
|
73
|
-
}
|
|
74
|
-
return defaultLocale != null ? defaultLocale : "";
|
|
75
|
-
}
|
|
29
|
+
|
|
30
|
+
// src/useDynamicLocale.ts
|
|
31
|
+
import { validator as validator2 } from "@sheet-i18n/shared-utils";
|
|
32
|
+
import { useLayoutEffect, useState } from "react";
|
|
76
33
|
|
|
77
34
|
// src/Service/IntlInstanceCache.ts
|
|
78
35
|
import { createIntl, createIntlCache, useIntl } from "react-intl";
|
|
@@ -93,6 +50,9 @@ var IntlInstanceCache = class _IntlInstanceCache {
|
|
|
93
50
|
this.getTotalCache = () => {
|
|
94
51
|
return this.cache;
|
|
95
52
|
};
|
|
53
|
+
this.resetTotalCache = () => {
|
|
54
|
+
this.cache = {};
|
|
55
|
+
};
|
|
96
56
|
this.createClientIntlInstance = (sheetTitle, i18nStore) => {
|
|
97
57
|
const currentIntl = useIntl();
|
|
98
58
|
const currentLocale = i18nStore.getCurrentLocale();
|
|
@@ -139,8 +99,90 @@ var IntlInstanceCache = class _IntlInstanceCache {
|
|
|
139
99
|
};
|
|
140
100
|
var intlInstanceCache = IntlInstanceCache.init();
|
|
141
101
|
|
|
102
|
+
// src/useDynamicLocale.ts
|
|
103
|
+
function useDynamicLocale(useDynamicLocaleProps) {
|
|
104
|
+
const { i18nStore } = useDynamicLocaleProps != null ? useDynamicLocaleProps : {};
|
|
105
|
+
const currentLocale = i18nStore == null ? void 0 : i18nStore.getCurrentLocale();
|
|
106
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
107
|
+
useLayoutEffect(() => {
|
|
108
|
+
var _a;
|
|
109
|
+
if (validator2.isNullish(currentLocale)) return;
|
|
110
|
+
const dynamicLoaders = (_a = i18nStore == null ? void 0 : i18nStore.dynamicLoaders) != null ? _a : useDynamicLocaleProps == null ? void 0 : useDynamicLocaleProps.dynamicLoaders;
|
|
111
|
+
if (!dynamicLoaders) return;
|
|
112
|
+
const targetDynamicLoader = dynamicLoaders[currentLocale];
|
|
113
|
+
if (!targetDynamicLoader) return;
|
|
114
|
+
const currentLocaleSet = i18nStore == null ? void 0 : i18nStore.getLocaleSet();
|
|
115
|
+
const targetLocaleData = currentLocaleSet == null ? void 0 : currentLocaleSet[currentLocale];
|
|
116
|
+
if (targetLocaleData) return;
|
|
117
|
+
setIsLoading(true);
|
|
118
|
+
targetDynamicLoader == null ? void 0 : targetDynamicLoader().then((res) => {
|
|
119
|
+
const { default: localeData } = res;
|
|
120
|
+
i18nStore == null ? void 0 : i18nStore.mergeLocaleSet(currentLocale, localeData);
|
|
121
|
+
intlInstanceCache.resetTotalCache();
|
|
122
|
+
setIsLoading(false);
|
|
123
|
+
}).catch((err) => {
|
|
124
|
+
console.log(`\u26A0\uFE0F dynamic loader error:
|
|
125
|
+
${err}`);
|
|
126
|
+
setIsLoading(false);
|
|
127
|
+
});
|
|
128
|
+
}, [currentLocale]);
|
|
129
|
+
return {
|
|
130
|
+
isLoading
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/IntlProvider.tsx
|
|
135
|
+
import { jsx } from "react/jsx-runtime";
|
|
136
|
+
function IntlProvider({
|
|
137
|
+
i18nStore,
|
|
138
|
+
currentLocale,
|
|
139
|
+
children
|
|
140
|
+
}) {
|
|
141
|
+
const { locale } = useIntlLocale({ i18nStore, currentLocale });
|
|
142
|
+
const onIntlError = (err) => {
|
|
143
|
+
const typeSafe = i18nStore.typeSafe;
|
|
144
|
+
if (typeSafe) {
|
|
145
|
+
console.error(err);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
const { isLoading } = useDynamicLocale({ i18nStore });
|
|
149
|
+
return /* @__PURE__ */ jsx(
|
|
150
|
+
ReactIntlProvider,
|
|
151
|
+
{
|
|
152
|
+
locale,
|
|
153
|
+
messages: i18nStore == null ? void 0 : i18nStore.localeSet[locale],
|
|
154
|
+
onError: onIntlError,
|
|
155
|
+
children: !isLoading && children
|
|
156
|
+
}
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
function useIntlLocale({
|
|
160
|
+
i18nStore,
|
|
161
|
+
currentLocale
|
|
162
|
+
}) {
|
|
163
|
+
var _a;
|
|
164
|
+
const locale = (_a = currentLocale != null ? currentLocale : detectClientLanguage(i18nStore)) != null ? _a : i18nStore.defaultLocale;
|
|
165
|
+
i18nStore.setCurrentLocale(locale);
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
i18nStore.setCurrentLocale(locale);
|
|
168
|
+
}, [locale]);
|
|
169
|
+
return {
|
|
170
|
+
locale
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function detectClientLanguage(i18nStore) {
|
|
174
|
+
const { defaultLocale, supportedLocales } = i18nStore;
|
|
175
|
+
if (typeof navigator !== "undefined" && (navigator == null ? void 0 : navigator.languages)) {
|
|
176
|
+
const preferredLocale = navigator.languages.find(
|
|
177
|
+
(lang) => supportedLocales == null ? void 0 : supportedLocales.includes(lang)
|
|
178
|
+
);
|
|
179
|
+
return preferredLocale != null ? preferredLocale : defaultLocale;
|
|
180
|
+
}
|
|
181
|
+
return defaultLocale != null ? defaultLocale : "";
|
|
182
|
+
}
|
|
183
|
+
|
|
142
184
|
// src/Service/TranslationService.ts
|
|
143
|
-
import { validator as
|
|
185
|
+
import { validator as validator3 } from "@sheet-i18n/shared-utils";
|
|
144
186
|
var TranslationService = class {
|
|
145
187
|
constructor(intlInstance) {
|
|
146
188
|
this.intlInstance = intlInstance;
|
|
@@ -165,7 +207,7 @@ var TranslationService = class {
|
|
|
165
207
|
findTargetTranslation(id, $tParams) {
|
|
166
208
|
var _a, _b, _c, _d, _e;
|
|
167
209
|
const targetTranslation = (_b = (_a = this == null ? void 0 : this.intlInstance) == null ? void 0 : _a.messages) == null ? void 0 : _b[id];
|
|
168
|
-
if (
|
|
210
|
+
if (validator3.isNullish(targetTranslation)) {
|
|
169
211
|
return id;
|
|
170
212
|
}
|
|
171
213
|
if (typeof targetTranslation === "string" && targetTranslation.trim() === "") {
|
|
@@ -257,7 +299,7 @@ function getTranslation({
|
|
|
257
299
|
// src/createI18nContext.tsx
|
|
258
300
|
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
259
301
|
function createI18nContext(i18nStore) {
|
|
260
|
-
if (
|
|
302
|
+
if (validator4.isNullish(i18nStore)) {
|
|
261
303
|
throw new InvalidI18nContextStateError(
|
|
262
304
|
"\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
|
|
263
305
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sheet-i18n/react-client",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0-canary.0",
|
|
4
4
|
"description": "a client package for react modules used by sheet-i18n",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -25,16 +25,16 @@
|
|
|
25
25
|
},
|
|
26
26
|
"license": "ISC",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@sheet-i18n/shared-utils": "1.
|
|
29
|
-
"@sheet-i18n/errors": "1.
|
|
30
|
-
"@sheet-i18n/react-core": "1.
|
|
28
|
+
"@sheet-i18n/shared-utils": "1.5.0-canary.0",
|
|
29
|
+
"@sheet-i18n/errors": "1.5.0-canary.0",
|
|
30
|
+
"@sheet-i18n/react-core": "1.2.0-canary.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/react": "^19.0.2",
|
|
34
34
|
"@types/react-dom": "^19.0.2",
|
|
35
35
|
"react": "^18.2.0",
|
|
36
36
|
"react-intl": "^7.0.4",
|
|
37
|
-
"@sheet-i18n/typescript-config": "1.
|
|
37
|
+
"@sheet-i18n/typescript-config": "1.5.0-canary.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"react": "^18 || ^19",
|