@sheet-i18n/react 1.6.1 → 1.6.2-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/README.md +51 -5
- package/dist/index.d.mts +112 -2
- package/dist/index.d.ts +112 -2
- package/dist/index.js +558 -4
- package/dist/index.mjs +553 -2
- package/package.json +8 -3
package/dist/index.js
CHANGED
|
@@ -1,8 +1,30 @@
|
|
|
1
|
+
"use client";
|
|
1
2
|
"use strict";
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
3
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
7
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
9
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
+
var __spreadValues = (a, b) => {
|
|
13
|
+
for (var prop in b || (b = {}))
|
|
14
|
+
if (__hasOwnProp.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
if (__getOwnPropSymbols)
|
|
17
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
+
if (__propIsEnum.call(b, prop))
|
|
19
|
+
__defNormalProp(a, prop, b[prop]);
|
|
20
|
+
}
|
|
21
|
+
return a;
|
|
22
|
+
};
|
|
23
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
+
var __export = (target, all) => {
|
|
25
|
+
for (var name in all)
|
|
26
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
27
|
+
};
|
|
6
28
|
var __copyProps = (to, from, except, desc) => {
|
|
7
29
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
30
|
for (let key of __getOwnPropNames(from))
|
|
@@ -16,11 +38,543 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
16
38
|
|
|
17
39
|
// src/index.ts
|
|
18
40
|
var src_exports = {};
|
|
41
|
+
__export(src_exports, {
|
|
42
|
+
LocaleStorageManager: () => LocaleStorageManager,
|
|
43
|
+
createI18nContext: () => createI18nContext,
|
|
44
|
+
ruleFactory: () => ruleFactory
|
|
45
|
+
});
|
|
19
46
|
module.exports = __toCommonJS(src_exports);
|
|
20
|
-
__reExport(src_exports, require("@sheet-i18n/
|
|
21
|
-
|
|
47
|
+
__reExport(src_exports, require("@sheet-i18n/core"), module.exports);
|
|
48
|
+
|
|
49
|
+
// src/createI18nContext.tsx
|
|
50
|
+
var import_shared_utils6 = require("@sheet-i18n/shared-utils");
|
|
51
|
+
var import_core = require("@sheet-i18n/core");
|
|
52
|
+
|
|
53
|
+
// src/Provider/IntlProvider.tsx
|
|
54
|
+
var import_react_intl2 = require("react-intl");
|
|
55
|
+
var import_react2 = require("react");
|
|
56
|
+
|
|
57
|
+
// src/hooks/useDynamicLocale.ts
|
|
58
|
+
var import_shared_utils2 = require("@sheet-i18n/shared-utils");
|
|
59
|
+
var import_react = require("react");
|
|
60
|
+
|
|
61
|
+
// src/Service/IntlInstanceCache.ts
|
|
62
|
+
var import_react_intl = require("react-intl");
|
|
63
|
+
var import_shared_utils = require("@sheet-i18n/shared-utils");
|
|
64
|
+
var intlCache = (0, import_react_intl.createIntlCache)();
|
|
65
|
+
var IntlInstanceCache = class _IntlInstanceCache {
|
|
66
|
+
constructor() {
|
|
67
|
+
this.cache = {};
|
|
68
|
+
this.getCachedIntlInstance = (sheetTitle, i18nStore, environment = "client") => {
|
|
69
|
+
const currentLocale = i18nStore.getCurrentLocale();
|
|
70
|
+
const cachedIntlInstance = this.cache[sheetTitle];
|
|
71
|
+
const sameLocale = (cachedIntlInstance == null ? void 0 : cachedIntlInstance.locale) === currentLocale;
|
|
72
|
+
if (cachedIntlInstance && sameLocale) {
|
|
73
|
+
return cachedIntlInstance.intlInstance;
|
|
74
|
+
}
|
|
75
|
+
return environment === "client" ? this.createClientIntlInstance(sheetTitle, i18nStore) : this.createServerIntlInstance(sheetTitle, i18nStore);
|
|
76
|
+
};
|
|
77
|
+
this.getTotalCache = () => {
|
|
78
|
+
return this.cache;
|
|
79
|
+
};
|
|
80
|
+
this.resetTotalCache = () => {
|
|
81
|
+
this.cache = {};
|
|
82
|
+
};
|
|
83
|
+
this.createClientIntlInstance = (sheetTitle, i18nStore) => {
|
|
84
|
+
const currentIntl = (0, import_react_intl.useIntl)();
|
|
85
|
+
const currentLocale = i18nStore.getCurrentLocale();
|
|
86
|
+
const targetMessages = i18nStore.getTargetMessages(sheetTitle);
|
|
87
|
+
const newIntl = (0, import_react_intl.createIntl)(
|
|
88
|
+
__spreadProps(__spreadValues({}, currentIntl), { messages: targetMessages }),
|
|
89
|
+
intlCache
|
|
90
|
+
);
|
|
91
|
+
if (currentLocale) {
|
|
92
|
+
this.setCachedIntlInstance(sheetTitle, currentLocale, newIntl);
|
|
93
|
+
}
|
|
94
|
+
return newIntl;
|
|
95
|
+
};
|
|
96
|
+
this.createServerIntlInstance = (sheetTitle, i18nStore) => {
|
|
97
|
+
const currentLocale = i18nStore.getCurrentLocale();
|
|
98
|
+
const targetMessages = i18nStore.getTargetMessages(sheetTitle);
|
|
99
|
+
if (!i18nStore || !currentLocale) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const newIntl = (0, import_react_intl.createIntl)(
|
|
103
|
+
{ locale: currentLocale, messages: targetMessages },
|
|
104
|
+
intlCache
|
|
105
|
+
);
|
|
106
|
+
if (currentLocale) {
|
|
107
|
+
this.setCachedIntlInstance(sheetTitle, currentLocale, newIntl);
|
|
108
|
+
}
|
|
109
|
+
return newIntl;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
static init() {
|
|
113
|
+
if (!_IntlInstanceCache.instance) {
|
|
114
|
+
_IntlInstanceCache.instance = new _IntlInstanceCache();
|
|
115
|
+
}
|
|
116
|
+
return _IntlInstanceCache.instance;
|
|
117
|
+
}
|
|
118
|
+
setCachedIntlInstance(sheetTitle, currentLocale, intlInstance) {
|
|
119
|
+
if (import_shared_utils.validator.isNullish(this.cache[sheetTitle])) {
|
|
120
|
+
this.cache[sheetTitle] = {
|
|
121
|
+
locale: currentLocale,
|
|
122
|
+
intlInstance
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
var intlInstanceCache = IntlInstanceCache.init();
|
|
128
|
+
|
|
129
|
+
// src/hooks/useDynamicLocale.ts
|
|
130
|
+
function useDynamicLocale(useDynamicLocaleProps) {
|
|
131
|
+
const { i18nStore } = useDynamicLocaleProps != null ? useDynamicLocaleProps : {};
|
|
132
|
+
const currentLocale = i18nStore == null ? void 0 : i18nStore.getCurrentLocale();
|
|
133
|
+
const [isLoading, setIsLoading] = (0, import_react.useState)(false);
|
|
134
|
+
(0, import_react.useLayoutEffect)(() => {
|
|
135
|
+
var _a;
|
|
136
|
+
if (import_shared_utils2.validator.isNullish(currentLocale)) return;
|
|
137
|
+
const dynamicLoaders = (_a = i18nStore == null ? void 0 : i18nStore.dynamicLoaders) != null ? _a : useDynamicLocaleProps == null ? void 0 : useDynamicLocaleProps.dynamicLoaders;
|
|
138
|
+
if (!dynamicLoaders) return;
|
|
139
|
+
const targetDynamicLoader = dynamicLoaders[currentLocale];
|
|
140
|
+
if (!targetDynamicLoader) return;
|
|
141
|
+
const currentLocaleSet = i18nStore == null ? void 0 : i18nStore.localeSet;
|
|
142
|
+
const targetLocaleData = currentLocaleSet == null ? void 0 : currentLocaleSet[currentLocale];
|
|
143
|
+
if (targetLocaleData) return;
|
|
144
|
+
setIsLoading(true);
|
|
145
|
+
targetDynamicLoader == null ? void 0 : targetDynamicLoader().then((res) => {
|
|
146
|
+
const { default: localeData } = res;
|
|
147
|
+
i18nStore == null ? void 0 : i18nStore.mergeLocaleSet(currentLocale, localeData);
|
|
148
|
+
intlInstanceCache.resetTotalCache();
|
|
149
|
+
setIsLoading(false);
|
|
150
|
+
}).catch((err) => {
|
|
151
|
+
console.log(`\u26A0\uFE0F dynamic loader error:
|
|
152
|
+
${err}`);
|
|
153
|
+
setIsLoading(false);
|
|
154
|
+
});
|
|
155
|
+
}, [currentLocale]);
|
|
156
|
+
return {
|
|
157
|
+
isLoading
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// src/Provider/IntlProvider.tsx
|
|
162
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
163
|
+
function IntlProvider({
|
|
164
|
+
i18nStore,
|
|
165
|
+
currentLocale,
|
|
166
|
+
children: childrenFromProps
|
|
167
|
+
}) {
|
|
168
|
+
const { locale } = useIntlLocale({ i18nStore, currentLocale });
|
|
169
|
+
const onIntlError = (err) => {
|
|
170
|
+
const typeSafe = i18nStore.typeSafe;
|
|
171
|
+
if (typeSafe) {
|
|
172
|
+
console.error(err);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
const { isLoading } = useDynamicLocale({ i18nStore });
|
|
176
|
+
const children = typeof childrenFromProps === "function" ? childrenFromProps(locale) : childrenFromProps;
|
|
177
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
178
|
+
import_react_intl2.IntlProvider,
|
|
179
|
+
{
|
|
180
|
+
locale,
|
|
181
|
+
messages: i18nStore == null ? void 0 : i18nStore.localeSet[locale],
|
|
182
|
+
onError: onIntlError,
|
|
183
|
+
children: !isLoading && children
|
|
184
|
+
},
|
|
185
|
+
`sheet-i18n-locale-${locale}`
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
function useIntlLocale({
|
|
189
|
+
i18nStore,
|
|
190
|
+
currentLocale
|
|
191
|
+
}) {
|
|
192
|
+
const locale = currentLocale || detectClientLanguage(i18nStore) || i18nStore.defaultLocale;
|
|
193
|
+
i18nStore.setCurrentLocale(locale);
|
|
194
|
+
(0, import_react2.useEffect)(() => {
|
|
195
|
+
i18nStore.setCurrentLocale(locale);
|
|
196
|
+
}, [locale]);
|
|
197
|
+
return {
|
|
198
|
+
locale
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function detectClientLanguage(i18nStore) {
|
|
202
|
+
const { defaultLocale, supportedLocales } = i18nStore;
|
|
203
|
+
if (typeof navigator !== "undefined" && (navigator == null ? void 0 : navigator.languages)) {
|
|
204
|
+
const preferredLocale = navigator.languages.find(
|
|
205
|
+
(lang) => supportedLocales == null ? void 0 : supportedLocales.includes(lang)
|
|
206
|
+
);
|
|
207
|
+
return preferredLocale != null ? preferredLocale : defaultLocale;
|
|
208
|
+
}
|
|
209
|
+
return defaultLocale != null ? defaultLocale : "";
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// src/Service/TranslationService.ts
|
|
213
|
+
var import_shared_utils3 = require("@sheet-i18n/shared-utils");
|
|
214
|
+
var TranslationService = class {
|
|
215
|
+
constructor(intlInstance) {
|
|
216
|
+
this.intlInstance = intlInstance;
|
|
217
|
+
}
|
|
218
|
+
translate(id, values, opts, _descriptor) {
|
|
219
|
+
const descriptor = this.createDescriptor(id, _descriptor);
|
|
220
|
+
const targetTranslation = this.findTargetTranslation(id, {
|
|
221
|
+
descriptor,
|
|
222
|
+
values,
|
|
223
|
+
opts
|
|
224
|
+
});
|
|
225
|
+
return targetTranslation;
|
|
226
|
+
}
|
|
227
|
+
/** helpers */
|
|
228
|
+
createDescriptor(id, _descriptor) {
|
|
229
|
+
var _a;
|
|
230
|
+
return __spreadProps(__spreadValues({}, _descriptor != null ? _descriptor : {}), {
|
|
231
|
+
id,
|
|
232
|
+
defaultMessage: (_a = _descriptor == null ? void 0 : _descriptor.defaultMessage) != null ? _a : id
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
findTargetTranslation(id, $tParams) {
|
|
236
|
+
var _a, _b, _c, _d, _e;
|
|
237
|
+
const targetTranslation = (_b = (_a = this == null ? void 0 : this.intlInstance) == null ? void 0 : _a.messages) == null ? void 0 : _b[id];
|
|
238
|
+
if (import_shared_utils3.validator.isNullish(targetTranslation)) {
|
|
239
|
+
return id;
|
|
240
|
+
}
|
|
241
|
+
if (typeof targetTranslation === "string" && targetTranslation.trim() === "") {
|
|
242
|
+
return "";
|
|
243
|
+
}
|
|
244
|
+
if (!$tParams || !($tParams == null ? void 0 : $tParams.descriptor)) {
|
|
245
|
+
return "";
|
|
246
|
+
}
|
|
247
|
+
return (_e = (_d = (_c = this.intlInstance) == null ? void 0 : _c.$t) == null ? void 0 : _d.call(
|
|
248
|
+
_c,
|
|
249
|
+
$tParams.descriptor,
|
|
250
|
+
$tParams.values,
|
|
251
|
+
$tParams.opts
|
|
252
|
+
)) != null ? _e : "";
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// src/hooks/useTranslation.ts
|
|
257
|
+
function useTranslation({
|
|
258
|
+
sheetTitle,
|
|
259
|
+
i18nStore,
|
|
260
|
+
rules
|
|
261
|
+
}) {
|
|
262
|
+
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
263
|
+
sheetTitle,
|
|
264
|
+
i18nStore
|
|
265
|
+
);
|
|
266
|
+
const translationService = new TranslationService(intlInstance);
|
|
267
|
+
const t = (id, values, opts, _descriptor) => {
|
|
268
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
269
|
+
};
|
|
270
|
+
t.dynamic = (id, values, opts, _descriptor) => {
|
|
271
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
272
|
+
};
|
|
273
|
+
t.rule = (ruleKey, values) => {
|
|
274
|
+
const ruleFn = rules == null ? void 0 : rules[ruleKey];
|
|
275
|
+
if (!ruleFn) return ruleKey;
|
|
276
|
+
const messageId = ruleFn(values, {});
|
|
277
|
+
return translationService.translate(messageId, values);
|
|
278
|
+
};
|
|
279
|
+
return { t };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// src/Errors.ts
|
|
283
|
+
var import_errors = require("@sheet-i18n/errors");
|
|
284
|
+
var InvalidI18nContextStateError = class extends import_errors.CustomError {
|
|
285
|
+
};
|
|
286
|
+
var IsNotInstanceOfI18nStoreError = class extends import_errors.CustomError {
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
// src/hooks/getTranslation.ts
|
|
290
|
+
function getTranslation({
|
|
291
|
+
sheetTitle,
|
|
292
|
+
i18nStore
|
|
293
|
+
}) {
|
|
294
|
+
const t = (id, values, opts, _descriptor) => {
|
|
295
|
+
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
296
|
+
sheetTitle,
|
|
297
|
+
i18nStore,
|
|
298
|
+
"server"
|
|
299
|
+
);
|
|
300
|
+
const translationService = new TranslationService(intlInstance);
|
|
301
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
302
|
+
};
|
|
303
|
+
t.dynamic = (id, values, opts, _descriptor) => {
|
|
304
|
+
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
305
|
+
sheetTitle,
|
|
306
|
+
i18nStore,
|
|
307
|
+
"server"
|
|
308
|
+
);
|
|
309
|
+
const translationService = new TranslationService(intlInstance);
|
|
310
|
+
return translationService.translate(id, values, opts, _descriptor);
|
|
311
|
+
};
|
|
312
|
+
t.promise = (id, values, opts, _descriptor) => {
|
|
313
|
+
const intlInitPromise = new Promise((resolve) => {
|
|
314
|
+
setTimeout(() => {
|
|
315
|
+
const intlInstance = intlInstanceCache.getCachedIntlInstance(
|
|
316
|
+
sheetTitle,
|
|
317
|
+
i18nStore,
|
|
318
|
+
"server"
|
|
319
|
+
);
|
|
320
|
+
resolve(intlInstance);
|
|
321
|
+
}, 1);
|
|
322
|
+
});
|
|
323
|
+
return new Promise((resolve) => {
|
|
324
|
+
intlInitPromise.then((intlInstance) => {
|
|
325
|
+
const translationService = new TranslationService(intlInstance);
|
|
326
|
+
resolve(translationService.translate(id, values, opts, _descriptor));
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
};
|
|
330
|
+
return { t };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// src/Service/StorageService.ts
|
|
334
|
+
var import_shared_utils4 = require("@sheet-i18n/shared-utils");
|
|
335
|
+
var StorageService = class {
|
|
336
|
+
constructor(storage) {
|
|
337
|
+
this.storage = null;
|
|
338
|
+
this.isClientSide = typeof window !== "undefined";
|
|
339
|
+
if (storage) {
|
|
340
|
+
this.storage = this.validateStorage(storage);
|
|
341
|
+
} else if (this.isClientSide) {
|
|
342
|
+
this.storage = this.initializeWindowLocalStorage();
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
validateStorage(storage) {
|
|
346
|
+
const requiredMethods = [
|
|
347
|
+
"getItem",
|
|
348
|
+
"setItem",
|
|
349
|
+
"removeItem",
|
|
350
|
+
"clear"
|
|
351
|
+
];
|
|
352
|
+
try {
|
|
353
|
+
for (const method of requiredMethods) {
|
|
354
|
+
if (typeof storage[method] !== "function") {
|
|
355
|
+
throw new Error(
|
|
356
|
+
`Invalid storage object: missing required method '${method}'`
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return storage;
|
|
361
|
+
} catch (error) {
|
|
362
|
+
console.error(error);
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
initializeWindowLocalStorage() {
|
|
367
|
+
try {
|
|
368
|
+
const testKey = "__storage_test__";
|
|
369
|
+
localStorage.setItem(testKey, "test");
|
|
370
|
+
localStorage.removeItem(testKey);
|
|
371
|
+
return localStorage;
|
|
372
|
+
} catch (error) {
|
|
373
|
+
console.warn("Window LocalStorage is not available:", error);
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
getItem(key) {
|
|
378
|
+
if (!this.storage || !key) {
|
|
379
|
+
return "";
|
|
380
|
+
}
|
|
381
|
+
try {
|
|
382
|
+
return this.storage.getItem(key);
|
|
383
|
+
} catch (error) {
|
|
384
|
+
console.error(`Failed to get item with key "${key}":`, error);
|
|
385
|
+
return "";
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
setItem(key, value) {
|
|
389
|
+
if (!this.storage || !key) {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
try {
|
|
393
|
+
this.storage.setItem(key, value);
|
|
394
|
+
return true;
|
|
395
|
+
} catch (error) {
|
|
396
|
+
console.error(`Failed to set item with key "${key}":`, error);
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
removeItem(key) {
|
|
401
|
+
if (!this.storage || !key) {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
try {
|
|
405
|
+
this.storage.removeItem(key);
|
|
406
|
+
return true;
|
|
407
|
+
} catch (error) {
|
|
408
|
+
console.error(`Failed to remove item with key "${key}":`, error);
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
clear() {
|
|
413
|
+
if (!this.storage) {
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
try {
|
|
417
|
+
this.storage.clear();
|
|
418
|
+
return true;
|
|
419
|
+
} catch (error) {
|
|
420
|
+
console.error("Failed to clear storage:", error);
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
var LocaleStorageManager = class {
|
|
426
|
+
constructor(storageService, i18nStore, localeStorageKey = "sheet-i18n-locale") {
|
|
427
|
+
this.storageService = storageService;
|
|
428
|
+
this.i18nStore = i18nStore;
|
|
429
|
+
this.localeStorageKey = localeStorageKey;
|
|
430
|
+
this.observerManager = new import_shared_utils4.ObserverManager();
|
|
431
|
+
this.initializeCurrentLocale = () => {
|
|
432
|
+
var _a, _b;
|
|
433
|
+
const storedLocale = this.storageService.getItem(this.localeStorageKey);
|
|
434
|
+
if (!storedLocale || storedLocale === "") {
|
|
435
|
+
const defaultLocale = this.i18nStore.currentLocale || this.i18nStore.defaultLocale;
|
|
436
|
+
this.storageService.setItem(this.localeStorageKey, defaultLocale);
|
|
437
|
+
}
|
|
438
|
+
(_b = (_a = this.i18nStore) == null ? void 0 : _a.observerManager) == null ? void 0 : _b.addListener({
|
|
439
|
+
listenerId: this.localeStorageKey,
|
|
440
|
+
listener: (newLocale) => {
|
|
441
|
+
this.storageService.setItem(this.localeStorageKey, newLocale);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
};
|
|
445
|
+
this.getLocale = () => {
|
|
446
|
+
const stored = this.storageService.getItem(this.localeStorageKey);
|
|
447
|
+
if (!stored || stored === "") {
|
|
448
|
+
return this.i18nStore.defaultLocale;
|
|
449
|
+
}
|
|
450
|
+
return stored;
|
|
451
|
+
};
|
|
452
|
+
this.setLocale = (locale) => {
|
|
453
|
+
this.storageService.setItem(this.localeStorageKey, locale);
|
|
454
|
+
this.observerManager.notify(locale);
|
|
455
|
+
};
|
|
456
|
+
this.initializeCurrentLocale();
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
var getLocaleStorageManager = (i18nStore, storage) => {
|
|
460
|
+
const localeStorageService = new StorageService(storage);
|
|
461
|
+
const localeStorageManager = new LocaleStorageManager(
|
|
462
|
+
localeStorageService,
|
|
463
|
+
i18nStore
|
|
464
|
+
);
|
|
465
|
+
return localeStorageManager;
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
// src/hooks/useLocaleStorage.ts
|
|
469
|
+
var import_react3 = require("react");
|
|
470
|
+
var LISTENER_ID = "LOCALE_STORAGE_LISTENER_ID";
|
|
471
|
+
function useLocaleStorage(localeStorageManager) {
|
|
472
|
+
const [locale, setLocale] = (0, import_react3.useState)();
|
|
473
|
+
(0, import_react3.useEffect)(() => {
|
|
474
|
+
const storedLocale = localeStorageManager.getLocale();
|
|
475
|
+
setLocale(storedLocale);
|
|
476
|
+
}, []);
|
|
477
|
+
(0, import_react3.useEffect)(() => {
|
|
478
|
+
var _a;
|
|
479
|
+
(_a = localeStorageManager == null ? void 0 : localeStorageManager.observerManager) == null ? void 0 : _a.addListener({
|
|
480
|
+
listenerId: LISTENER_ID,
|
|
481
|
+
listener: (newLocale) => {
|
|
482
|
+
setLocale(newLocale);
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
return () => {
|
|
486
|
+
var _a2;
|
|
487
|
+
(_a2 = localeStorageManager == null ? void 0 : localeStorageManager.observerManager) == null ? void 0 : _a2.removeListener(LISTENER_ID);
|
|
488
|
+
};
|
|
489
|
+
}, []);
|
|
490
|
+
return { locale };
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// src/Provider/StorageBasedIntlProvider.tsx
|
|
494
|
+
var import_shared_utils5 = require("@sheet-i18n/shared-utils");
|
|
495
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
496
|
+
function StorageBasedIntlProvider({
|
|
497
|
+
i18nStore,
|
|
498
|
+
storageManager,
|
|
499
|
+
fallbackUI = null,
|
|
500
|
+
children: childrenFromProps
|
|
501
|
+
}) {
|
|
502
|
+
const { locale } = useLocaleStorage(storageManager);
|
|
503
|
+
if (import_shared_utils5.validator.isNullish(locale)) {
|
|
504
|
+
return fallbackUI;
|
|
505
|
+
}
|
|
506
|
+
const children = typeof childrenFromProps === "function" ? childrenFromProps(locale) : childrenFromProps;
|
|
507
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(IntlProvider, { currentLocale: locale, i18nStore, children });
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// src/createI18nContext.tsx
|
|
511
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
512
|
+
function createI18nContext(i18nStore, plugins) {
|
|
513
|
+
if (import_shared_utils6.validator.isNullish(i18nStore)) {
|
|
514
|
+
throw new InvalidI18nContextStateError(
|
|
515
|
+
"\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
if (i18nStore instanceof import_core.I18nStore !== true) {
|
|
519
|
+
throw new IsNotInstanceOfI18nStoreError(
|
|
520
|
+
"\u26A0\uFE0F The provided i18nStore is invalid. Please ensure a store instance you passed is an instance of I18nStore."
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
const { rules } = plugins != null ? plugins : {};
|
|
524
|
+
const IntlProviderImpl = ({
|
|
525
|
+
currentLocale,
|
|
526
|
+
children
|
|
527
|
+
}) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
528
|
+
IntlProvider,
|
|
529
|
+
{
|
|
530
|
+
currentLocale,
|
|
531
|
+
i18nStore,
|
|
532
|
+
children
|
|
533
|
+
}
|
|
534
|
+
);
|
|
535
|
+
const StorageBasedIntlProviderImpl = ({
|
|
536
|
+
storageManager,
|
|
537
|
+
children
|
|
538
|
+
}) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
539
|
+
StorageBasedIntlProvider,
|
|
540
|
+
{
|
|
541
|
+
i18nStore,
|
|
542
|
+
storageManager,
|
|
543
|
+
children
|
|
544
|
+
}
|
|
545
|
+
);
|
|
546
|
+
const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore, rules });
|
|
547
|
+
const getTranslationImpl = (sheetTitle) => getTranslation({ sheetTitle, i18nStore });
|
|
548
|
+
const getLocaleStorageManagerImpl = (storageService) => {
|
|
549
|
+
return getLocaleStorageManager(i18nStore, storageService);
|
|
550
|
+
};
|
|
551
|
+
return {
|
|
552
|
+
IntlProvider: IntlProviderImpl,
|
|
553
|
+
StorageBasedIntlProvider: StorageBasedIntlProviderImpl,
|
|
554
|
+
useTranslation: useTranslationImpl,
|
|
555
|
+
getTranslation: getTranslationImpl,
|
|
556
|
+
getLocaleStorageManager: getLocaleStorageManagerImpl,
|
|
557
|
+
useLocaleStorage
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// src/Service/RuleService.ts
|
|
562
|
+
var RuleService = class {
|
|
563
|
+
constructor(i18nStore) {
|
|
564
|
+
this.i18nStore = i18nStore;
|
|
565
|
+
this.createRule = (ruleFn) => {
|
|
566
|
+
const currentLocaleSet = this.i18nStore.getCurrentLocaleSet();
|
|
567
|
+
return (values) => ruleFn(values, currentLocaleSet);
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
var ruleFactory = (i18nStore) => {
|
|
572
|
+
return new RuleService(i18nStore);
|
|
573
|
+
};
|
|
22
574
|
// Annotate the CommonJS export names for ESM import in node:
|
|
23
575
|
0 && (module.exports = {
|
|
24
|
-
|
|
25
|
-
|
|
576
|
+
LocaleStorageManager,
|
|
577
|
+
createI18nContext,
|
|
578
|
+
ruleFactory,
|
|
579
|
+
...require("@sheet-i18n/core")
|
|
26
580
|
});
|