@sheet-i18n/react-client 1.4.0 → 1.5.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 +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +161 -5
- package/dist/index.mjs +159 -3
- package/package.json +7 -7
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
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
|
+
import { ObserverManager } from '@sheet-i18n/shared-utils';
|
|
4
5
|
|
|
5
6
|
type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
6
7
|
type ExtendedUseIntlParams<D = MessageDescriptor> = UseIntlParams<D> extends [infer A, infer B, ...infer Rest] ? A extends Record<string, infer V> ? [Record<string, V | React.ReactNode>, B, ...Rest] : [A, B, ...Rest] : UseIntlParams<D>;
|
|
@@ -39,6 +40,33 @@ type IntlProviderProps<TSupportedLocales extends readonly string[], TLocaleSet e
|
|
|
39
40
|
children: React.ReactNode;
|
|
40
41
|
};
|
|
41
42
|
|
|
43
|
+
interface IStorageService<V extends string> {
|
|
44
|
+
getItem(key: string): V;
|
|
45
|
+
setItem(key: string, value: V): boolean;
|
|
46
|
+
removeItem(key: string): boolean;
|
|
47
|
+
clear(): boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* locale storage manager
|
|
51
|
+
* implements ILocaleStorageManager (injected StorageService)
|
|
52
|
+
*/
|
|
53
|
+
interface ILocaleStorageManager<TSupportedLocales extends readonly string[]> {
|
|
54
|
+
getLocale(): TSupportedLocales[number];
|
|
55
|
+
setLocale(locale: TSupportedLocales[number]): void;
|
|
56
|
+
}
|
|
57
|
+
declare class LocaleStorageManager<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean = false> implements ILocaleStorageManager<TSupportedLocales> {
|
|
58
|
+
private readonly storageService;
|
|
59
|
+
private readonly i18nStore;
|
|
60
|
+
private readonly localeStorageKey;
|
|
61
|
+
observerManager: ObserverManager<TSupportedLocales[number]>;
|
|
62
|
+
constructor(storageService: IStorageService<TSupportedLocales[number]>, i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, localeStorageKey?: string);
|
|
63
|
+
private initializeCurrentLocale;
|
|
64
|
+
getLocale: () => TSupportedLocales[number];
|
|
65
|
+
setLocale: (locale: TSupportedLocales[number]) => void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
declare function useLocaleStorage<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean = false>(localeStorageManager: LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>): TSupportedLocales[number];
|
|
69
|
+
|
|
42
70
|
declare function createI18nContext<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean = false>(i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>): {
|
|
43
71
|
IntlProvider: ({ currentLocale, children, }: Omit<IntlProviderProps<TSupportedLocales, TLocaleSet>, "currentLocale" | "i18nStore"> & {
|
|
44
72
|
currentLocale?: string;
|
|
@@ -46,6 +74,8 @@ declare function createI18nContext<TSupportedLocales extends readonly string[],
|
|
|
46
74
|
}) => react_jsx_runtime.JSX.Element;
|
|
47
75
|
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>;
|
|
48
76
|
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>;
|
|
77
|
+
getLocaleStorageManager: (storage?: IStorageService<string>) => LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>;
|
|
78
|
+
useLocaleStorage: typeof useLocaleStorage;
|
|
49
79
|
};
|
|
50
80
|
|
|
51
81
|
export { createI18nContext };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
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
|
+
import { ObserverManager } from '@sheet-i18n/shared-utils';
|
|
4
5
|
|
|
5
6
|
type UseIntlParams<D = MessageDescriptor> = Parameters<IntlShape['$t']> extends [D, ...infer R] ? [...R, Omit<D, 'id'>] : never;
|
|
6
7
|
type ExtendedUseIntlParams<D = MessageDescriptor> = UseIntlParams<D> extends [infer A, infer B, ...infer Rest] ? A extends Record<string, infer V> ? [Record<string, V | React.ReactNode>, B, ...Rest] : [A, B, ...Rest] : UseIntlParams<D>;
|
|
@@ -39,6 +40,33 @@ type IntlProviderProps<TSupportedLocales extends readonly string[], TLocaleSet e
|
|
|
39
40
|
children: React.ReactNode;
|
|
40
41
|
};
|
|
41
42
|
|
|
43
|
+
interface IStorageService<V extends string> {
|
|
44
|
+
getItem(key: string): V;
|
|
45
|
+
setItem(key: string, value: V): boolean;
|
|
46
|
+
removeItem(key: string): boolean;
|
|
47
|
+
clear(): boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* locale storage manager
|
|
51
|
+
* implements ILocaleStorageManager (injected StorageService)
|
|
52
|
+
*/
|
|
53
|
+
interface ILocaleStorageManager<TSupportedLocales extends readonly string[]> {
|
|
54
|
+
getLocale(): TSupportedLocales[number];
|
|
55
|
+
setLocale(locale: TSupportedLocales[number]): void;
|
|
56
|
+
}
|
|
57
|
+
declare class LocaleStorageManager<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean = false> implements ILocaleStorageManager<TSupportedLocales> {
|
|
58
|
+
private readonly storageService;
|
|
59
|
+
private readonly i18nStore;
|
|
60
|
+
private readonly localeStorageKey;
|
|
61
|
+
observerManager: ObserverManager<TSupportedLocales[number]>;
|
|
62
|
+
constructor(storageService: IStorageService<TSupportedLocales[number]>, i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>, localeStorageKey?: string);
|
|
63
|
+
private initializeCurrentLocale;
|
|
64
|
+
getLocale: () => TSupportedLocales[number];
|
|
65
|
+
setLocale: (locale: TSupportedLocales[number]) => void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
declare function useLocaleStorage<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean = false>(localeStorageManager: LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>): TSupportedLocales[number];
|
|
69
|
+
|
|
42
70
|
declare function createI18nContext<TSupportedLocales extends readonly string[], TLocaleSet extends Partial<Record<TSupportedLocales[number], Record<string, any>>>, TTypeSafe extends boolean = false>(i18nStore: I18nStore<TSupportedLocales, TLocaleSet, TTypeSafe>): {
|
|
43
71
|
IntlProvider: ({ currentLocale, children, }: Omit<IntlProviderProps<TSupportedLocales, TLocaleSet>, "currentLocale" | "i18nStore"> & {
|
|
44
72
|
currentLocale?: string;
|
|
@@ -46,6 +74,8 @@ declare function createI18nContext<TSupportedLocales extends readonly string[],
|
|
|
46
74
|
}) => react_jsx_runtime.JSX.Element;
|
|
47
75
|
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>;
|
|
48
76
|
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>;
|
|
77
|
+
getLocaleStorageManager: (storage?: IStorageService<string>) => LocaleStorageManager<TSupportedLocales, TLocaleSet, TTypeSafe>;
|
|
78
|
+
useLocaleStorage: typeof useLocaleStorage;
|
|
49
79
|
};
|
|
50
80
|
|
|
51
81
|
export { createI18nContext };
|
package/dist/index.js
CHANGED
|
@@ -43,7 +43,7 @@ __export(src_exports, {
|
|
|
43
43
|
module.exports = __toCommonJS(src_exports);
|
|
44
44
|
|
|
45
45
|
// src/createI18nContext.tsx
|
|
46
|
-
var
|
|
46
|
+
var import_shared_utils5 = require("@sheet-i18n/shared-utils");
|
|
47
47
|
var import_react_core = require("@sheet-i18n/react-core");
|
|
48
48
|
|
|
49
49
|
// src/IntlProvider.tsx
|
|
@@ -183,8 +183,7 @@ function useIntlLocale({
|
|
|
183
183
|
i18nStore,
|
|
184
184
|
currentLocale
|
|
185
185
|
}) {
|
|
186
|
-
|
|
187
|
-
const locale = (_a = currentLocale != null ? currentLocale : detectClientLanguage(i18nStore)) != null ? _a : i18nStore.defaultLocale;
|
|
186
|
+
const locale = currentLocale || detectClientLanguage(i18nStore) || i18nStore.defaultLocale;
|
|
188
187
|
i18nStore.setCurrentLocale(locale);
|
|
189
188
|
(0, import_react2.useEffect)(() => {
|
|
190
189
|
i18nStore.setCurrentLocale(locale);
|
|
@@ -319,10 +318,157 @@ function getTranslation({
|
|
|
319
318
|
return { t };
|
|
320
319
|
}
|
|
321
320
|
|
|
321
|
+
// src/Service/LocalStorageService.ts
|
|
322
|
+
var import_shared_utils4 = require("@sheet-i18n/shared-utils");
|
|
323
|
+
var LocalStorageService = class {
|
|
324
|
+
constructor(storage) {
|
|
325
|
+
this.storage = null;
|
|
326
|
+
this.isClientSide = typeof window !== "undefined";
|
|
327
|
+
if (storage) {
|
|
328
|
+
this.storage = this.validateStorage(storage);
|
|
329
|
+
} else if (this.isClientSide) {
|
|
330
|
+
this.storage = this.initializeWindowLocalStorage();
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
validateStorage(storage) {
|
|
334
|
+
const requiredMethods = [
|
|
335
|
+
"getItem",
|
|
336
|
+
"setItem",
|
|
337
|
+
"removeItem",
|
|
338
|
+
"clear"
|
|
339
|
+
];
|
|
340
|
+
try {
|
|
341
|
+
for (const method of requiredMethods) {
|
|
342
|
+
if (typeof storage[method] !== "function") {
|
|
343
|
+
throw new Error(
|
|
344
|
+
`Invalid storage object: missing required method '${method}'`
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return storage;
|
|
349
|
+
} catch (error) {
|
|
350
|
+
console.error(error);
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
initializeWindowLocalStorage() {
|
|
355
|
+
try {
|
|
356
|
+
const testKey = "__storage_test__";
|
|
357
|
+
localStorage.setItem(testKey, "test");
|
|
358
|
+
localStorage.removeItem(testKey);
|
|
359
|
+
return localStorage;
|
|
360
|
+
} catch (error) {
|
|
361
|
+
console.warn("Window LocalStorage is not available:", error);
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
getItem(key) {
|
|
366
|
+
if (!this.storage || !key) {
|
|
367
|
+
return "";
|
|
368
|
+
}
|
|
369
|
+
try {
|
|
370
|
+
return this.storage.getItem(key);
|
|
371
|
+
} catch (error) {
|
|
372
|
+
console.error(`Failed to get item with key "${key}":`, error);
|
|
373
|
+
return "";
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
setItem(key, value) {
|
|
377
|
+
if (!this.storage || !key) {
|
|
378
|
+
return false;
|
|
379
|
+
}
|
|
380
|
+
try {
|
|
381
|
+
this.storage.setItem(key, value);
|
|
382
|
+
return true;
|
|
383
|
+
} catch (error) {
|
|
384
|
+
console.error(`Failed to set item with key "${key}":`, error);
|
|
385
|
+
return false;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
removeItem(key) {
|
|
389
|
+
if (!this.storage || !key) {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
try {
|
|
393
|
+
this.storage.removeItem(key);
|
|
394
|
+
return true;
|
|
395
|
+
} catch (error) {
|
|
396
|
+
console.error(`Failed to remove item with key "${key}":`, error);
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
clear() {
|
|
401
|
+
if (!this.storage) {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
try {
|
|
405
|
+
this.storage.clear();
|
|
406
|
+
return true;
|
|
407
|
+
} catch (error) {
|
|
408
|
+
console.error("Failed to clear storage:", error);
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
var LocaleStorageManager = class {
|
|
414
|
+
constructor(storageService, i18nStore, localeStorageKey = "sheet-i18n-locale") {
|
|
415
|
+
this.storageService = storageService;
|
|
416
|
+
this.i18nStore = i18nStore;
|
|
417
|
+
this.localeStorageKey = localeStorageKey;
|
|
418
|
+
this.observerManager = new import_shared_utils4.ObserverManager();
|
|
419
|
+
this.initializeCurrentLocale = () => {
|
|
420
|
+
var _a, _b;
|
|
421
|
+
if (this.storageService && this.i18nStore.currentLocale) {
|
|
422
|
+
this.storageService.setItem(
|
|
423
|
+
this.localeStorageKey,
|
|
424
|
+
this.i18nStore.currentLocale
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
(_b = (_a = this.i18nStore) == null ? void 0 : _a.observerManager) == null ? void 0 : _b.addListener({
|
|
428
|
+
listenerId: this.localeStorageKey,
|
|
429
|
+
listener: (newLocale) => {
|
|
430
|
+
this.storageService.setItem(this.localeStorageKey, newLocale);
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
};
|
|
434
|
+
this.getLocale = () => {
|
|
435
|
+
const stored = this.storageService.getItem(this.localeStorageKey);
|
|
436
|
+
return stored != null ? stored : "";
|
|
437
|
+
};
|
|
438
|
+
this.setLocale = (locale) => {
|
|
439
|
+
this.storageService.setItem(this.localeStorageKey, locale);
|
|
440
|
+
this.observerManager.notify(locale);
|
|
441
|
+
};
|
|
442
|
+
this.initializeCurrentLocale();
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
// src/useLocaleStorage.ts
|
|
447
|
+
var import_react3 = require("react");
|
|
448
|
+
var LISTENER_ID = "LOCALE_STORAGE_LISTENER_ID";
|
|
449
|
+
function useLocaleStorage(localeStorageManager) {
|
|
450
|
+
var _a;
|
|
451
|
+
const [locale, setLocale] = (0, import_react3.useState)((_a = localeStorageManager == null ? void 0 : localeStorageManager.getLocale) == null ? void 0 : _a.call(localeStorageManager));
|
|
452
|
+
(0, import_react3.useEffect)(() => {
|
|
453
|
+
var _a2;
|
|
454
|
+
(_a2 = localeStorageManager == null ? void 0 : localeStorageManager.observerManager) == null ? void 0 : _a2.addListener({
|
|
455
|
+
listenerId: LISTENER_ID,
|
|
456
|
+
listener: (newLocale) => {
|
|
457
|
+
setLocale(newLocale);
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
return () => {
|
|
461
|
+
var _a3;
|
|
462
|
+
(_a3 = localeStorageManager == null ? void 0 : localeStorageManager.observerManager) == null ? void 0 : _a3.removeListener(LISTENER_ID);
|
|
463
|
+
};
|
|
464
|
+
}, []);
|
|
465
|
+
return locale;
|
|
466
|
+
}
|
|
467
|
+
|
|
322
468
|
// src/createI18nContext.tsx
|
|
323
469
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
324
470
|
function createI18nContext(i18nStore) {
|
|
325
|
-
if (
|
|
471
|
+
if (import_shared_utils5.validator.isNullish(i18nStore)) {
|
|
326
472
|
throw new InvalidI18nContextStateError(
|
|
327
473
|
"\u26A0\uFE0F no i18nStore provided. To use createI18nContext, you must provide an i18nStore as a parameter"
|
|
328
474
|
);
|
|
@@ -345,10 +491,20 @@ function createI18nContext(i18nStore) {
|
|
|
345
491
|
);
|
|
346
492
|
const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore });
|
|
347
493
|
const getTranslationImpl = (sheetTitle) => getTranslation({ sheetTitle, i18nStore });
|
|
494
|
+
const getLocaleStorageManager = (storage) => {
|
|
495
|
+
const localStorageService = new LocalStorageService(storage);
|
|
496
|
+
const localeStorageManager = new LocaleStorageManager(
|
|
497
|
+
localStorageService,
|
|
498
|
+
i18nStore
|
|
499
|
+
);
|
|
500
|
+
return localeStorageManager;
|
|
501
|
+
};
|
|
348
502
|
return {
|
|
349
503
|
IntlProvider: IntlProviderImpl,
|
|
350
504
|
useTranslation: useTranslationImpl,
|
|
351
|
-
getTranslation: getTranslationImpl
|
|
505
|
+
getTranslation: getTranslationImpl,
|
|
506
|
+
getLocaleStorageManager,
|
|
507
|
+
useLocaleStorage
|
|
352
508
|
};
|
|
353
509
|
}
|
|
354
510
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.mjs
CHANGED
|
@@ -160,8 +160,7 @@ function useIntlLocale({
|
|
|
160
160
|
i18nStore,
|
|
161
161
|
currentLocale
|
|
162
162
|
}) {
|
|
163
|
-
|
|
164
|
-
const locale = (_a = currentLocale != null ? currentLocale : detectClientLanguage(i18nStore)) != null ? _a : i18nStore.defaultLocale;
|
|
163
|
+
const locale = currentLocale || detectClientLanguage(i18nStore) || i18nStore.defaultLocale;
|
|
165
164
|
i18nStore.setCurrentLocale(locale);
|
|
166
165
|
useEffect(() => {
|
|
167
166
|
i18nStore.setCurrentLocale(locale);
|
|
@@ -296,6 +295,153 @@ function getTranslation({
|
|
|
296
295
|
return { t };
|
|
297
296
|
}
|
|
298
297
|
|
|
298
|
+
// src/Service/LocalStorageService.ts
|
|
299
|
+
import { ObserverManager } from "@sheet-i18n/shared-utils";
|
|
300
|
+
var LocalStorageService = class {
|
|
301
|
+
constructor(storage) {
|
|
302
|
+
this.storage = null;
|
|
303
|
+
this.isClientSide = typeof window !== "undefined";
|
|
304
|
+
if (storage) {
|
|
305
|
+
this.storage = this.validateStorage(storage);
|
|
306
|
+
} else if (this.isClientSide) {
|
|
307
|
+
this.storage = this.initializeWindowLocalStorage();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
validateStorage(storage) {
|
|
311
|
+
const requiredMethods = [
|
|
312
|
+
"getItem",
|
|
313
|
+
"setItem",
|
|
314
|
+
"removeItem",
|
|
315
|
+
"clear"
|
|
316
|
+
];
|
|
317
|
+
try {
|
|
318
|
+
for (const method of requiredMethods) {
|
|
319
|
+
if (typeof storage[method] !== "function") {
|
|
320
|
+
throw new Error(
|
|
321
|
+
`Invalid storage object: missing required method '${method}'`
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return storage;
|
|
326
|
+
} catch (error) {
|
|
327
|
+
console.error(error);
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
initializeWindowLocalStorage() {
|
|
332
|
+
try {
|
|
333
|
+
const testKey = "__storage_test__";
|
|
334
|
+
localStorage.setItem(testKey, "test");
|
|
335
|
+
localStorage.removeItem(testKey);
|
|
336
|
+
return localStorage;
|
|
337
|
+
} catch (error) {
|
|
338
|
+
console.warn("Window LocalStorage is not available:", error);
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
getItem(key) {
|
|
343
|
+
if (!this.storage || !key) {
|
|
344
|
+
return "";
|
|
345
|
+
}
|
|
346
|
+
try {
|
|
347
|
+
return this.storage.getItem(key);
|
|
348
|
+
} catch (error) {
|
|
349
|
+
console.error(`Failed to get item with key "${key}":`, error);
|
|
350
|
+
return "";
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
setItem(key, value) {
|
|
354
|
+
if (!this.storage || !key) {
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
try {
|
|
358
|
+
this.storage.setItem(key, value);
|
|
359
|
+
return true;
|
|
360
|
+
} catch (error) {
|
|
361
|
+
console.error(`Failed to set item with key "${key}":`, error);
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
removeItem(key) {
|
|
366
|
+
if (!this.storage || !key) {
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
try {
|
|
370
|
+
this.storage.removeItem(key);
|
|
371
|
+
return true;
|
|
372
|
+
} catch (error) {
|
|
373
|
+
console.error(`Failed to remove item with key "${key}":`, error);
|
|
374
|
+
return false;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
clear() {
|
|
378
|
+
if (!this.storage) {
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
try {
|
|
382
|
+
this.storage.clear();
|
|
383
|
+
return true;
|
|
384
|
+
} catch (error) {
|
|
385
|
+
console.error("Failed to clear storage:", error);
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
var LocaleStorageManager = class {
|
|
391
|
+
constructor(storageService, i18nStore, localeStorageKey = "sheet-i18n-locale") {
|
|
392
|
+
this.storageService = storageService;
|
|
393
|
+
this.i18nStore = i18nStore;
|
|
394
|
+
this.localeStorageKey = localeStorageKey;
|
|
395
|
+
this.observerManager = new ObserverManager();
|
|
396
|
+
this.initializeCurrentLocale = () => {
|
|
397
|
+
var _a, _b;
|
|
398
|
+
if (this.storageService && this.i18nStore.currentLocale) {
|
|
399
|
+
this.storageService.setItem(
|
|
400
|
+
this.localeStorageKey,
|
|
401
|
+
this.i18nStore.currentLocale
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
(_b = (_a = this.i18nStore) == null ? void 0 : _a.observerManager) == null ? void 0 : _b.addListener({
|
|
405
|
+
listenerId: this.localeStorageKey,
|
|
406
|
+
listener: (newLocale) => {
|
|
407
|
+
this.storageService.setItem(this.localeStorageKey, newLocale);
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
};
|
|
411
|
+
this.getLocale = () => {
|
|
412
|
+
const stored = this.storageService.getItem(this.localeStorageKey);
|
|
413
|
+
return stored != null ? stored : "";
|
|
414
|
+
};
|
|
415
|
+
this.setLocale = (locale) => {
|
|
416
|
+
this.storageService.setItem(this.localeStorageKey, locale);
|
|
417
|
+
this.observerManager.notify(locale);
|
|
418
|
+
};
|
|
419
|
+
this.initializeCurrentLocale();
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
// src/useLocaleStorage.ts
|
|
424
|
+
import { useEffect as useEffect2, useState as useState2 } from "react";
|
|
425
|
+
var LISTENER_ID = "LOCALE_STORAGE_LISTENER_ID";
|
|
426
|
+
function useLocaleStorage(localeStorageManager) {
|
|
427
|
+
var _a;
|
|
428
|
+
const [locale, setLocale] = useState2((_a = localeStorageManager == null ? void 0 : localeStorageManager.getLocale) == null ? void 0 : _a.call(localeStorageManager));
|
|
429
|
+
useEffect2(() => {
|
|
430
|
+
var _a2;
|
|
431
|
+
(_a2 = localeStorageManager == null ? void 0 : localeStorageManager.observerManager) == null ? void 0 : _a2.addListener({
|
|
432
|
+
listenerId: LISTENER_ID,
|
|
433
|
+
listener: (newLocale) => {
|
|
434
|
+
setLocale(newLocale);
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
return () => {
|
|
438
|
+
var _a3;
|
|
439
|
+
(_a3 = localeStorageManager == null ? void 0 : localeStorageManager.observerManager) == null ? void 0 : _a3.removeListener(LISTENER_ID);
|
|
440
|
+
};
|
|
441
|
+
}, []);
|
|
442
|
+
return locale;
|
|
443
|
+
}
|
|
444
|
+
|
|
299
445
|
// src/createI18nContext.tsx
|
|
300
446
|
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
301
447
|
function createI18nContext(i18nStore) {
|
|
@@ -322,10 +468,20 @@ function createI18nContext(i18nStore) {
|
|
|
322
468
|
);
|
|
323
469
|
const useTranslationImpl = (sheetTitle) => useTranslation({ sheetTitle, i18nStore });
|
|
324
470
|
const getTranslationImpl = (sheetTitle) => getTranslation({ sheetTitle, i18nStore });
|
|
471
|
+
const getLocaleStorageManager = (storage) => {
|
|
472
|
+
const localStorageService = new LocalStorageService(storage);
|
|
473
|
+
const localeStorageManager = new LocaleStorageManager(
|
|
474
|
+
localStorageService,
|
|
475
|
+
i18nStore
|
|
476
|
+
);
|
|
477
|
+
return localeStorageManager;
|
|
478
|
+
};
|
|
325
479
|
return {
|
|
326
480
|
IntlProvider: IntlProviderImpl,
|
|
327
481
|
useTranslation: useTranslationImpl,
|
|
328
|
-
getTranslation: getTranslationImpl
|
|
482
|
+
getTranslation: getTranslationImpl,
|
|
483
|
+
getLocaleStorageManager,
|
|
484
|
+
useLocaleStorage
|
|
329
485
|
};
|
|
330
486
|
}
|
|
331
487
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sheet-i18n/react-client",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.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,20 +25,20 @@
|
|
|
25
25
|
},
|
|
26
26
|
"license": "ISC",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@sheet-i18n/
|
|
29
|
-
"@sheet-i18n/errors": "1.
|
|
30
|
-
"@sheet-i18n/
|
|
28
|
+
"@sheet-i18n/shared-utils": "1.8.0-canary.0",
|
|
29
|
+
"@sheet-i18n/errors": "1.8.0-canary.0",
|
|
30
|
+
"@sheet-i18n/react-core": "1.5.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.8.0-canary.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"react": "^18 || ^19",
|
|
41
|
-
"react-dom": "^18 || ^19",
|
|
40
|
+
"react": "^18 || ^19 || ^20 || ^21",
|
|
41
|
+
"react-dom": "^18 || ^19 || ^20 || ^21",
|
|
42
42
|
"react-intl": "^7.0.4"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|