sheet-i18n 1.8.0-canary.1 β 1.8.0-canary.11
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 +134 -12
- package/package.json +6 -5
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.ts
|
|
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
|
---
|
|
@@ -548,7 +670,7 @@ This will automatically generate:
|
|
|
548
670
|
- A preconfigured `i18nStore` using `@sheet-i18n/react`
|
|
549
671
|
- A ready-to-use `i18nContext.ts` that exports `IntlProvider`, `useTranslation`, and `getTranslation`
|
|
550
672
|
- Example locale JSON files (e.g., `en.json`, `ko.json`)
|
|
551
|
-
- The `sheet.config.
|
|
673
|
+
- The `sheet.config.ts` file for managing your CLI behavior and Google Sheets credentials
|
|
552
674
|
|
|
553
675
|
> π‘ All files will be created in the recommended structure (e.g. `src/i18n`, `src/locales`), and safely skipped if they already exist.
|
|
554
676
|
|
|
@@ -558,7 +680,7 @@ This will automatically generate:
|
|
|
558
680
|
|
|
559
681
|
- You donβt need to manually initiate `I18nStore` or set up the React context.
|
|
560
682
|
- You donβt need to manually prepare locale JSON templates.
|
|
561
|
-
- You donβt need to manually configure `sheet.config.
|
|
683
|
+
- You donβt need to manually configure `sheet.config.ts`.
|
|
562
684
|
|
|
563
685
|
Just run the init command and youβre ready to use `useTranslation()` in your components.
|
|
564
686
|
|
|
@@ -568,18 +690,18 @@ Just run the init command and youβre ready to use `useTranslation()` in your c
|
|
|
568
690
|
|
|
569
691
|
```bash
|
|
570
692
|
π¦ your-project/
|
|
571
|
-
βββ π .gitignore # Automatically appends 'sheet.config.
|
|
572
|
-
βββ π sheet.config.
|
|
693
|
+
βββ π .gitignore # Automatically appends 'sheet.config.ts' to ignore list
|
|
694
|
+
βββ π sheet.config.ts # CLI configuration file (contains Google Sheet credentials & options)
|
|
573
695
|
βββ π src/
|
|
574
696
|
βββ π i18n/
|
|
575
|
-
βββ π i18nContext.
|
|
697
|
+
βββ π i18nContext.ts # React context for IntlProvider, useTranslation, and getTranslation
|
|
576
698
|
βββ π i18nStore.ts # Initializes I18nStore with supported locales and translation data
|
|
577
699
|
βββ π en.json # Placeholder for English translation strings
|
|
578
700
|
βββ π ko.json # Placeholder for Korean translation strings
|
|
579
701
|
```
|
|
580
702
|
|
|
581
703
|
β
The CLI automatically creates this structure after running `npx sheet-i18n init`.
|
|
582
|
-
π‘οΈ If `sheet.config.
|
|
704
|
+
π‘οΈ If `sheet.config.ts` or `src/i18n` already exist, the CLI will skip the creation of these files.
|
|
583
705
|
|
|
584
706
|
after running `npx sheet-i18n init`, you can mount the IntlProvider in your app.
|
|
585
707
|
|
|
@@ -623,11 +745,11 @@ const YourComponent = () => {
|
|
|
623
745
|
|
|
624
746
|
your translations are now ready to use in your application.
|
|
625
747
|
|
|
626
|
-
### βοΈ sheet.config.
|
|
748
|
+
### βοΈ sheet.config.ts
|
|
627
749
|
|
|
628
|
-
The CLI requires a `sheet.config.
|
|
750
|
+
The CLI requires a `sheet.config.ts` file in the root of your project. Run `sheet-i18n init` to generate this file automatically.
|
|
629
751
|
|
|
630
|
-
Example `sheet.config.
|
|
752
|
+
Example `sheet.config.ts`:
|
|
631
753
|
|
|
632
754
|
```json
|
|
633
755
|
{
|
|
@@ -684,7 +806,7 @@ Example `sheet.config.json`:
|
|
|
684
806
|
npx sheet-i18n init
|
|
685
807
|
```
|
|
686
808
|
|
|
687
|
-
Sets up the `sheet.config.
|
|
809
|
+
Sets up the `sheet.config.ts` file in your project. This configuration file is required for all other commands.
|
|
688
810
|
|
|
689
811
|
</br>
|
|
690
812
|
|
|
@@ -754,7 +876,7 @@ Registers translation data to Google Sheets.
|
|
|
754
876
|
|
|
755
877
|
2. It updates rows and adds "translation" entries using the `GOOGLETRANSLATE` function supported by Google Spreadsheets for automated translations.
|
|
756
878
|
|
|
757
|
-
3. After registering, it asks whether you want to update the locale JSON data locally. It then exports the JSON data to the `importPath` specified in `sheet.config.
|
|
879
|
+
3. After registering, it asks whether you want to update the locale JSON data locally. It then exports the JSON data to the `importPath` specified in `sheet.config.ts` (default is the current working directory).
|
|
758
880
|
|
|
759
881
|
#### Options:
|
|
760
882
|
|
|
@@ -821,7 +943,7 @@ npx sheet-i18n import
|
|
|
821
943
|
```
|
|
822
944
|
|
|
823
945
|
Exports translation data from Google Sheets to local export directory.
|
|
824
|
-
The configuration of export command is based on **`sheet.config.
|
|
946
|
+
The configuration of export command is based on **`sheet.config.ts`** on your root.
|
|
825
947
|
|
|
826
948
|
```json
|
|
827
949
|
{
|
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.11",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "https://github.com/chltjdrhd777/sheet-i18n"
|
|
@@ -45,16 +45,17 @@
|
|
|
45
45
|
"license": "ISC",
|
|
46
46
|
"homepage": "https://github.com/chltjdrhd777/sheet-i18n",
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@sheet-i18n/importer": "1.8.0-canary.
|
|
49
|
-
"@sheet-i18n/react": "1.5.0-canary.
|
|
48
|
+
"@sheet-i18n/importer": "1.8.0-canary.2",
|
|
49
|
+
"@sheet-i18n/react": "1.5.0-canary.11"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"tsup": "^6.0.0",
|
|
53
53
|
"typescript": "^5.0.0",
|
|
54
|
-
"@sheet-i18n/typescript-config": "1.8.0-canary.
|
|
54
|
+
"@sheet-i18n/typescript-config": "1.8.0-canary.2"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
57
|
"build": "tsup",
|
|
58
|
-
"dev": "tsup --watch"
|
|
58
|
+
"dev": "tsup --watch",
|
|
59
|
+
"typecheck": "npx tsc --noEmit"
|
|
59
60
|
}
|
|
60
61
|
}
|