sheet-i18n 1.8.0-canary.3 → 1.8.0-canary.5
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 +122 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -485,6 +485,128 @@ export default function App() {
|
|
|
485
485
|
}
|
|
486
486
|
```
|
|
487
487
|
|
|
488
|
+
## 🌐 Advanced Interpolation Strategies
|
|
489
|
+
|
|
490
|
+
sheet-i18n provides advanced interpolation strategies for different rendering environments. These strategies ensure optimal performance and user experience across various deployment scenarios.
|
|
491
|
+
|
|
492
|
+
### 🖥️ Client-Side Rendering (CSR) Interpolation
|
|
493
|
+
|
|
494
|
+
For client-side applications, sheet-i18n offers `StorageBasedIntlProvider` with `LocaleStorageManager` to handle **persistent locale settings** and interpolation seamlessly.
|
|
495
|
+
|
|
496
|
+
#### **What is Persistent Locale Settings?**
|
|
497
|
+
|
|
498
|
+
The `StorageBasedIntlProvider` provides **persistent translation settings** that automatically remember and restore the user's language preference across browser sessions. This means:
|
|
499
|
+
|
|
500
|
+
- **Session Persistence**: User's language choice persists even after closing and reopening the browser
|
|
501
|
+
- **Cross-Tab Synchronization**: Language changes are synchronized across all open tabs/windows
|
|
502
|
+
- **Automatic Restoration**: When users return to your app, their preferred language is automatically restored
|
|
503
|
+
- **Fallback Handling**: If stored preferences are invalid, the system gracefully falls back to default settings
|
|
504
|
+
|
|
505
|
+
#### **Setup for CSR Interpolation**
|
|
506
|
+
|
|
507
|
+
```tsx
|
|
508
|
+
// i18nContext.tsx
|
|
509
|
+
import { createI18nContext } from '@sheet-i18n/react';
|
|
510
|
+
import { i18nStore } from './i18nStore';
|
|
511
|
+
|
|
512
|
+
export const {
|
|
513
|
+
StorageBasedIntlProvider,
|
|
514
|
+
useTranslation,
|
|
515
|
+
getLocaleStorageManager,
|
|
516
|
+
useLocaleStorage,
|
|
517
|
+
} = createI18nContext(i18nStore);
|
|
518
|
+
|
|
519
|
+
// Create a storage manager
|
|
520
|
+
// if you don't pass the storage service as an argument,
|
|
521
|
+
// it automatically utilize "LocalStorage" as the storage service
|
|
522
|
+
export const localeStorageManager = getLocaleStorageManager();
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
#### **Implementation Example**
|
|
526
|
+
|
|
527
|
+
```tsx
|
|
528
|
+
// App.tsx
|
|
529
|
+
import React from 'react';
|
|
530
|
+
import { StorageBasedIntlProvider, localeStorageManager } from './i18nContext';
|
|
531
|
+
|
|
532
|
+
const App = () => {
|
|
533
|
+
return (
|
|
534
|
+
// StorageBasedIntlProvider automatically re-render
|
|
535
|
+
// if storage local data is changed
|
|
536
|
+
<StorageBasedIntlProvider storageManager={localeStorageManager}>
|
|
537
|
+
<YourApp />
|
|
538
|
+
</StorageBasedIntlProvider>
|
|
539
|
+
);
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
// Component with persistent locale switching
|
|
543
|
+
const WelcomeComponent = () => {
|
|
544
|
+
const { t } = useTranslation('welcome');
|
|
545
|
+
const { locale } = useLocaleStorage(localeStorageManager);
|
|
546
|
+
|
|
547
|
+
return (
|
|
548
|
+
<div>
|
|
549
|
+
<h1>{t('welcome_message')}</h1>
|
|
550
|
+
<p>Current language: {locale}</p>
|
|
551
|
+
|
|
552
|
+
{/* Language change handler */}
|
|
553
|
+
<button onClick={() => localeStorageManager.setLocale('ko')}>
|
|
554
|
+
한국어
|
|
555
|
+
</button>
|
|
556
|
+
<button onClick={() => localeStorageManager.setLocale('en')}>
|
|
557
|
+
English
|
|
558
|
+
</button>
|
|
559
|
+
</div>
|
|
560
|
+
);
|
|
561
|
+
};
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
#### **How Persistence Works**
|
|
565
|
+
|
|
566
|
+
1. **Initial Load**: When the app starts, `StorageBasedIntlProvider` automatically checks for previously saved locale preferences
|
|
567
|
+
2. **Automatic Restoration**: If a saved preference exists, it's automatically applied; otherwise, the default locale is used
|
|
568
|
+
3. **Real-time Updates**: When users change the language, the new preference is immediately saved to storage and re-render the page
|
|
569
|
+
|
|
570
|
+
#### **Custom Storage Service**
|
|
571
|
+
|
|
572
|
+
For advanced use cases, you can implement custom storage services to control how locale preferences are persisted:
|
|
573
|
+
|
|
574
|
+
```tsx
|
|
575
|
+
// customStorageService.ts
|
|
576
|
+
import { IStorageService } from '@sheet-i18n/react';
|
|
577
|
+
|
|
578
|
+
export class CustomStorageService implements IStorageService<string> {
|
|
579
|
+
private storage = new Map<string, string>();
|
|
580
|
+
|
|
581
|
+
getItem(key: string): string {
|
|
582
|
+
// Retrieve persisted locale preference
|
|
583
|
+
return this.storage.get(key) || 'en';
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
setItem(key: string, value: string): boolean {
|
|
587
|
+
// Persist locale preference for cross-session memory
|
|
588
|
+
this.storage.set(key, value);
|
|
589
|
+
return true;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
removeItem(key: string): boolean {
|
|
593
|
+
// Clear persisted locale preference
|
|
594
|
+
return this.storage.delete(key);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
clear(): boolean {
|
|
598
|
+
// Clear all persisted preferences
|
|
599
|
+
this.storage.clear();
|
|
600
|
+
return true;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// Usage with custom persistence strategy
|
|
605
|
+
const customStorageManager = getLocaleStorageManager(
|
|
606
|
+
new CustomStorageService()
|
|
607
|
+
);
|
|
608
|
+
```
|
|
609
|
+
|
|
488
610
|
</details>
|
|
489
611
|
|
|
490
612
|
---
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sheet-i18n",
|
|
3
3
|
"description": "All-in-one i18n toolchain: seamlessly integrate Google Sheets, CLI, and react-i18n for easy translation workflows.",
|
|
4
|
-
"version": "1.8.0-canary.
|
|
4
|
+
"version": "1.8.0-canary.5",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "https://github.com/chltjdrhd777/sheet-i18n"
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"homepage": "https://github.com/chltjdrhd777/sheet-i18n",
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@sheet-i18n/importer": "1.8.0-canary.0",
|
|
49
|
-
"@sheet-i18n/react": "1.5.0-canary.
|
|
49
|
+
"@sheet-i18n/react": "1.5.0-canary.5"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"tsup": "^6.0.0",
|