sheet-i18n 1.3.9 β†’ 1.3.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.
Files changed (2) hide show
  1. package/README.md +72 -55
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -30,9 +30,9 @@ The `sheet-i18n` ecosystem is divided into three main packages:
30
30
  - `sheet-i18n/react`
31
31
  - `@sheet-i18n/cli`
32
32
 
33
- ---
33
+ <br/>
34
34
 
35
- <details open>
35
+ <details>
36
36
  <summary>🌍 Server Export Function - sheet-i18n/exporter</summary>
37
37
 
38
38
  #### `sheet-i18n/exporter`
@@ -152,9 +152,9 @@ The exported translations will be saved in a format that is easy to use for loca
152
152
  <details>
153
153
  <summary>βš›οΈ Frontend Translation Provider - sheet-i18n/react</summary>
154
154
 
155
- #### `sheet-i18n/react`
155
+ ### `@sheet-i18n/react`
156
156
 
157
- The **client-side i18n library** subpackage.
157
+ The **client-side i18n library** subpackage of sheet-i18n.
158
158
 
159
159
  This package provides tools to handle translations in React applications using context and hooks. It simplifies internationalization workflows by offering functions and components to manage, access, and use locale-specific translation data.
160
160
 
@@ -163,7 +163,8 @@ This package provides tools to handle translations in React applications using c
163
163
  - **`I18nStore`**: _Store Creation Class_ for managing translation data.
164
164
  - **`createI18nContext`**: _React Context_ to generate providers and hooks for translation.
165
165
  - **`IntlProvider`**: _React Translation Provider_ for managing current locale.
166
- - **`useTranslation`**: _Translation Hook_ for easy access to translation messages.
166
+ - **`useTranslation`**: _Client Side Translation Hook_ for easy access to translation messages on the client side.
167
+ - **`getTranslation`**: _Static Translation Function_ for easy access to translation messages on Static module files.
167
168
 
168
169
  ## πŸš€ Getting Started
169
170
 
@@ -380,33 +381,12 @@ const translatedMessage = t('login');
380
381
 
381
382
  ### `getTranslation`
382
383
 
383
- A hook to access translations in static modules.
384
+ A function to access translations in the environment where cannot use react context and hooks.
384
385
 
385
386
  #### Parameters:
386
387
 
387
388
  - **`sheetTitle`**: The sheet title of the translation group.
388
389
 
389
- #### Example:
390
-
391
- ```tsx
392
- // data.ts
393
- import { getTranslation } from './i18nContext';
394
-
395
- const { t } = getTranslation('header');
396
-
397
- export const STATUS_CATEGORY = [
398
- t('Total Energy Usage'),
399
- t('Total Waste Usage'),
400
- ];
401
-
402
- // App.tsx
403
- import { STATUS_CATEGORY } from './data';
404
-
405
- export default function App() {
406
- return STATUS_CATEGORY.map((item) => <div>{item}</div>);
407
- }
408
- ```
409
-
410
390
  ### `t Function`
411
391
 
412
392
  The t function is used to retrieve a translation string based on a key and optionally interpolate variables. It provides flexibility to include dynamic values, such as plain strings or React components, for enhanced localization capabilities.
@@ -443,7 +423,7 @@ A robust and flexible translation library designed for efficient handling of tra
443
423
 
444
424
  - **Client-Side Translation (Explicit Strings)**: Translate explicit string keys directly in your components.
445
425
  - **Client-Side Translation (Dynamic Strings)**: Dynamically match translations from JSON for unknown variables.
446
- - **Static Module Translation**: Use translations in static modules for configuration constants.
426
+ - **Module Translation**: Use translations in the place where the react context or hooks cannot be used.
447
427
 
448
428
  ### Usage
449
429
 
@@ -485,30 +465,78 @@ const deviceName = data?.device?.name;
485
465
  return <div>{t.dynamic(deviceName)}</div>;
486
466
  ```
487
467
 
488
- #### 3. **Static Module Translation**
468
+ #### 3. **Module Translation**
469
+
470
+ The `getTranslation` function allows you to use translations outside of React components, such as in static configuration files, constants, or utility modules.
471
+
472
+ It provide two possible supports
473
+
474
+ 1. **`t` (Synchronous Translation)** – Use when translation values are available **at runtime**.
475
+ 2. **`t.promise` (Asynchronous Translation)** – Use when the module’s evaluation order is uncertain.
476
+
477
+ #### **βœ… Usage Scenarios**
478
+
479
+ #### **Scenario 1: Context where the translation value is evaluated at runtime**
480
+
481
+ - For the common example, **t** call expression in a **function module**
482
+ - This behaves the same as `useTranslation`'s `t` function.
483
+
484
+ ##### **Example**
485
+
486
+ ```tsx
487
+ // module.ts
488
+ import { getTranslation } from './i18nContext';
489
+
490
+ const { t } = getTranslation('header');
491
+
492
+ export function getStatusCategory {
493
+ return [t('Total Energy Usage'), t('Total Waste Usage')];
494
+ };
495
+
496
+ // App.tsx
497
+ // the "t" call expression is evaluated at function call timing
498
+ import { getStatusCategory } from './module';
499
+
500
+ export default function App() {
501
+ return getStatusCategory().map((item) => <div>{item}</div>);
502
+ }
503
+ ```
504
+
505
+ **Result:** The translation is resolved **immediately** during runtime.
489
506
 
490
- Use `getTranslation` to initialize translations for static configurations, constants, or data that needs to be translated.
507
+ #### **⚠️ Scenario 2: Context where the translation value is evaluated immediately**
491
508
 
492
- The **"t"** function from `getTranslation` returns a **Promise** that resolves to the translated string.
509
+ - If the module is **imported dynamically in a client-side component**, the evaluation order of `getTranslation` and `t` call expression may not be guaranteed.
510
+ - Since JavaScript **evaluates modules at import time**, it may attempt to access translation values before `IntlProvider` has fully initialized the locale.
511
+ - In this case, use **`t.promise`** to ensure the translation resolves asynchronously.
493
512
 
494
- > πŸ’‘ Note <br/>
495
- > The **`t`** function returned by **`getTranslation`** resolves its Promise in the background. For display purposes, you don't need to wait for the Promise to resolve, allowing you to use it without async/await.<br/> However, if you need to work with the fulfilled translated value during runtime, you have use async/await to retrieve it.
513
+ #### **Example**
496
514
 
497
515
  ```tsx
498
- // data.ts
516
+ // module.ts
517
+ // The "t" result in the array will be resolved asynchronously
499
518
  import { getTranslation } from './i18nContext';
519
+
500
520
  const { t } = getTranslation('header');
501
521
 
502
522
  export const STATUS_CATEGORY = [
503
- t('Total Energy Usage'),
504
- t('Total Waste Usage'),
523
+ t.promise('Total Energy Usage'),
524
+ t.promise('Total Waste Usage'),
505
525
  ];
506
526
 
507
527
  // App.tsx
508
- import { STATUS_CATEGORY } from './data';
528
+ // So, t.promise ensure the current client-side locale data
529
+ // is fully initialized before the translations are resolved
530
+ import { STATUS_CATEGORY } from './module.ts';
509
531
 
510
532
  export default function App() {
511
- return STATUS_CATEGORY.map((item) => <div>{item}</div>);
533
+ return (
534
+ <div>
535
+ {STATUS_CATEGORY.map((item, index) => {
536
+ return <div key={index}>{item}</div>;
537
+ })}
538
+ </div>
539
+ );
512
540
  }
513
541
  ```
514
542
 
@@ -700,6 +728,9 @@ Example `sheet.config.json`:
700
728
 
701
729
  ```json
702
730
  {
731
+ "watchEntry": "src",
732
+ "detectExtensions": ["tsx", "ts", "jsx", "js"],
733
+
703
734
  "googleSheetConfig": {
704
735
  "credentials": {
705
736
  "sheetId": "YOUR_GOOGLE_SHEET_ID",
@@ -714,9 +745,9 @@ Example `sheet.config.json`:
714
745
  }
715
746
  ```
716
747
 
717
- #### 1. Watch command configuration
748
+ #### 1. File change monitoring configuration
718
749
 
719
- - `watchDirectory` (optional): Path of the directory to monitor during `watch` command based on the current working directory (default: `src`)
750
+ - `watchEntry` (optional): Entry path to detect file changes relative to current working directory (default: `src`)
720
751
  - `detectExtensions` (optional): File extensions to monitor (default: `jsx`, `js`, `tsx`, `ts`).
721
752
 
722
753
  #### 2. Register command configuration
@@ -745,7 +776,7 @@ Sets up the `sheet.config.json` file in your project. This configuration file is
745
776
  ### πŸ‘€ watch
746
777
 
747
778
  ```shell
748
- npx sheet-i18n watch [options]
779
+ npx sheet-i18n watch
749
780
  ```
750
781
 
751
782
  Monitors files or directories for changes and logs relevant updates.
@@ -787,16 +818,6 @@ The _watch result_ is
787
818
  }
788
819
  ```
789
820
 
790
- #### Options:
791
-
792
- - `-d, --directory <directory>`: Specify the directory to watch.
793
-
794
- Example:
795
-
796
- ```shell
797
- npx sheet-i18n watch --directory src
798
- ```
799
-
800
821
  </br>
801
822
 
802
823
  ### πŸ“€ register
@@ -817,10 +838,6 @@ Registers translation data to Google Sheets.
817
838
 
818
839
  #### Options:
819
840
 
820
- - `-d, --directory <directory>`
821
- Specify the directory to scan for translation data, relative to the current working directory.
822
- **Default**: `src`
823
-
824
841
  - `-s, --scope <scope>`
825
842
  Define the scope of the registration process:
826
843
  - **`diff`**: Only scans translation keys that have been modified in the Git history (based on `git diff`).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sheet-i18n",
3
- "version": "1.3.9",
3
+ "version": "1.3.11",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/chltjdrhd777/sheet-i18n"
@@ -36,13 +36,13 @@
36
36
  },
37
37
  "license": "ISC",
38
38
  "dependencies": {
39
- "@sheet-i18n/exporter": "1.3.5",
40
- "@sheet-i18n/react": "1.0.4"
39
+ "@sheet-i18n/exporter": "1.3.7",
40
+ "@sheet-i18n/react": "1.0.6"
41
41
  },
42
42
  "devDependencies": {
43
43
  "tsup": "^6.0.0",
44
44
  "typescript": "^5.0.0",
45
- "@sheet-i18n/typescript-config": "1.3.5"
45
+ "@sheet-i18n/typescript-config": "1.3.6"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsup",