@sheet-i18n/react 1.5.0-canary.4 → 1.5.0-canary.6
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 +130 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ This package provides tools to handle translations in React applications using c
|
|
|
17
17
|
## 🚀 Getting Started(Manually)
|
|
18
18
|
|
|
19
19
|
> ⚡ **Strongly recommended to use the init CLI for setup**
|
|
20
|
-
> 👉 [Please follow the Init CLI
|
|
20
|
+
> 👉 [Please follow the Init CLI](https://www.npmjs.com/package/@sheet-i18n/cli)
|
|
21
21
|
>
|
|
22
22
|
> If you don't want to use the CLI, you can follow the `Manual Setup` below.
|
|
23
23
|
|
|
@@ -454,6 +454,135 @@ export default function App() {
|
|
|
454
454
|
}
|
|
455
455
|
```
|
|
456
456
|
|
|
457
|
+
## 🌐 Advanced Interpolation Strategies
|
|
458
|
+
|
|
459
|
+
sheet-i18n provides advanced interpolation strategies for different rendering environments. These strategies ensure optimal performance and user experience across various deployment scenarios.
|
|
460
|
+
|
|
461
|
+
### 🖥️ Client-Side Rendering (CSR) Interpolation
|
|
462
|
+
|
|
463
|
+
For client-side applications, sheet-i18n offers `StorageBasedIntlProvider` with `LocaleStorageManager` to handle **persistent locale settings** and interpolation seamlessly.
|
|
464
|
+
|
|
465
|
+
#### **What is Persistent Locale Settings?**
|
|
466
|
+
|
|
467
|
+
The `StorageBasedIntlProvider` provides **persistent translation settings** that automatically remember and restore the user's language preference across browser sessions. This means:
|
|
468
|
+
|
|
469
|
+
- **Session Persistence**: User's language choice persists even after closing and reopening the browser
|
|
470
|
+
- **Cross-Tab Synchronization**: Language changes are synchronized across all open tabs/windows
|
|
471
|
+
- **Automatic Restoration**: When users return to your app, their preferred language is automatically restored
|
|
472
|
+
- **Fallback Handling**: If stored preferences are invalid, the system gracefully falls back to default settings
|
|
473
|
+
|
|
474
|
+
#### **Setup for CSR Interpolation**
|
|
475
|
+
|
|
476
|
+
```tsx
|
|
477
|
+
// i18nContext.tsx
|
|
478
|
+
import { createI18nContext } from '@sheet-i18n/react';
|
|
479
|
+
import { i18nStore } from './i18nStore';
|
|
480
|
+
|
|
481
|
+
export const {
|
|
482
|
+
StorageBasedIntlProvider,
|
|
483
|
+
useTranslation,
|
|
484
|
+
getLocaleStorageManager,
|
|
485
|
+
useLocaleStorage,
|
|
486
|
+
} = createI18nContext(i18nStore);
|
|
487
|
+
|
|
488
|
+
// Create a storage manager
|
|
489
|
+
// if you don't pass the storage service as an argument,
|
|
490
|
+
// it automatically utilize "LocalStorage" as the storage service
|
|
491
|
+
export const localeStorageManager = getLocaleStorageManager();
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
#### **Implementation Example**
|
|
495
|
+
|
|
496
|
+
```tsx
|
|
497
|
+
// App.tsx
|
|
498
|
+
import React from 'react';
|
|
499
|
+
import { StorageBasedIntlProvider, localeStorageManager } from './i18nContext';
|
|
500
|
+
|
|
501
|
+
const App = () => {
|
|
502
|
+
return (
|
|
503
|
+
// StorageBasedIntlProvider automatically re-render
|
|
504
|
+
// if storage local data is changed
|
|
505
|
+
<StorageBasedIntlProvider storageManager={localeStorageManager}>
|
|
506
|
+
<YourApp />
|
|
507
|
+
</StorageBasedIntlProvider>
|
|
508
|
+
);
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
// Component with persistent locale switching
|
|
512
|
+
const WelcomeComponent = () => {
|
|
513
|
+
const { t } = useTranslation('welcome');
|
|
514
|
+
const { locale } = useLocaleStorage(localeStorageManager);
|
|
515
|
+
|
|
516
|
+
return (
|
|
517
|
+
<div>
|
|
518
|
+
<h1>{t('welcome_message')}</h1>
|
|
519
|
+
<p>Current language: {locale}</p>
|
|
520
|
+
|
|
521
|
+
{/* Language change handler */}
|
|
522
|
+
<button onClick={() => localeStorageManager.setLocale('ko')}>
|
|
523
|
+
한국어
|
|
524
|
+
</button>
|
|
525
|
+
<button onClick={() => localeStorageManager.setLocale('en')}>
|
|
526
|
+
English
|
|
527
|
+
</button>
|
|
528
|
+
</div>
|
|
529
|
+
);
|
|
530
|
+
};
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
#### **How Persistence Works**
|
|
534
|
+
|
|
535
|
+
1. **Initial Load**: When the app starts, `StorageBasedIntlProvider` automatically checks for previously saved locale preferences
|
|
536
|
+
2. **Automatic Restoration**: If a saved preference exists, it's automatically applied; otherwise, the default locale is used
|
|
537
|
+
3. **Real-time Updates**: When users change the language, the new preference is immediately saved to storage and re-render the page
|
|
538
|
+
|
|
539
|
+
#### **Custom Storage Service**
|
|
540
|
+
|
|
541
|
+
For advanced use cases, you can implement custom storage services to control how locale preferences are persisted:
|
|
542
|
+
|
|
543
|
+
```tsx
|
|
544
|
+
// customStorageService.ts
|
|
545
|
+
import { IStorageService } from '@sheet-i18n/react';
|
|
546
|
+
|
|
547
|
+
export class CustomStorageService implements IStorageService<string> {
|
|
548
|
+
private storage = new Map<string, string>();
|
|
549
|
+
|
|
550
|
+
getItem(key: string): string {
|
|
551
|
+
// Retrieve persisted locale preference
|
|
552
|
+
return this.storage.get(key) || 'en';
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
setItem(key: string, value: string): boolean {
|
|
556
|
+
// Persist locale preference for cross-session memory
|
|
557
|
+
this.storage.set(key, value);
|
|
558
|
+
return true;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
removeItem(key: string): boolean {
|
|
562
|
+
// Clear persisted locale preference
|
|
563
|
+
return this.storage.delete(key);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
clear(): boolean {
|
|
567
|
+
// Clear all persisted preferences
|
|
568
|
+
this.storage.clear();
|
|
569
|
+
return true;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// Usage with custom persistence strategy
|
|
574
|
+
const customStorageManager = getLocaleStorageManager(
|
|
575
|
+
new CustomStorageService()
|
|
576
|
+
);
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
#### **Storage Options**
|
|
580
|
+
|
|
581
|
+
- **LocalStorage (Default)**: Persists across browser sessions and device restarts
|
|
582
|
+
- **SessionStorage**: Persists only for the current browser session
|
|
583
|
+
- **Custom Storage**: Implement your own persistence strategy (e.g., server-side storage, encrypted storage)
|
|
584
|
+
- **Memory Storage**: No persistence (for testing or temporary use cases)
|
|
585
|
+
|
|
457
586
|
## License 📜
|
|
458
587
|
|
|
459
588
|
This project is licensed under the ISC License. See the LICENSE file for details.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sheet-i18n/react",
|
|
3
|
-
"version": "1.5.0-canary.
|
|
3
|
+
"version": "1.5.0-canary.6",
|
|
4
4
|
"description": "i18n client logic based on react",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@sheet-i18n/react-core": "1.5.0-canary.0",
|
|
36
|
-
"@sheet-i18n/react-client": "1.5.0-canary.
|
|
36
|
+
"@sheet-i18n/react-client": "1.5.0-canary.3"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsup",
|