sheet-i18n 1.2.4 β†’ 1.3.2

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 +332 -4
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -24,15 +24,16 @@ npm install sheet-i18n
24
24
 
25
25
  It serves as a bridge between your translation data and your application, offering an **end-to-end solution** for exporting, managing, and integrating translations into your projects.
26
26
 
27
- The `sheet-i18n` ecosystem is divided into two main packages:
27
+ The `sheet-i18n` ecosystem is divided into three main packages:
28
28
 
29
29
  - `sheet-i18n/exporter`
30
30
  - `sheet-i18n/react`
31
+ - `@sheet-i18n/cli`<div style="display: inline-block; width:5px"></div><img src="https://img.shields.io/badge/New-red" style="width: 20px; transform:translateY(1px)" alt="New">
31
32
 
32
33
  ---
33
34
 
34
- <details open>
35
- <summary>🌍 Server Export Function - sheet-i18n/exporter</summary>
35
+ <details>
36
+ <summary>πŸ”§ Server Export Function - sheet-i18n/exporter</summary>
36
37
 
37
38
  #### `sheet-i18n/exporter`
38
39
 
@@ -148,7 +149,7 @@ The exported translations will be saved in a format that is easy to use for loca
148
149
 
149
150
  ---
150
151
 
151
- <details open>
152
+ <details>
152
153
  <summary>βš›οΈ Frontend Translation Provider - sheet-i18n/react</summary>
153
154
 
154
155
  #### `sheet-i18n/react`
@@ -414,3 +415,330 @@ This project is licensed under the ISC License.
414
415
  [chltjdrhd777@gmail.com](mailto:chltjdrhd777@gmail.com)
415
416
 
416
417
  </details>
418
+
419
+ ---
420
+
421
+ <details>
422
+ <summary>🌍 CLI for Automated Translation Management - @sheet-i18n/cli</summary>
423
+
424
+ #### @sheet-i18n/cli
425
+
426
+ A CLI tool for efficient translation management using Google Sheets, with a focus on developer experience (DX).
427
+
428
+ ## Features
429
+
430
+ - ✨ **Initialize** CLI configuration for translation workflows.
431
+ - πŸ‘€ **Watch** files or directories for changes.
432
+ - πŸ“€ **Register** translation data to Google Sheets.
433
+ - πŸ› οΈ Built with **TypeScript** for type safety.
434
+
435
+ ## Core Concepts 🌟
436
+
437
+ ### 😭 Traditional Translation Workflow
438
+
439
+ The traditional process of managing translations with Google Spreadsheets often includes these steps:
440
+
441
+ 1. Translator identifies the text to be translated from the page (without developer involvement).
442
+ 2. Translator adds the translation data to a Google Spreadsheet.
443
+ 3. Developer creates a script to locally download the translation data from the sheet.
444
+ 4. Developer inspects the translation data, matches it with the code, and finds the corresponding values in the source code (this can be time-consuming).
445
+ 5. If discrepancies exist between the spreadsheet and the code, developers request changes from the translator.
446
+ 6. Once resolved, the developer applies the translations using an internationalization (intl) library.
447
+ 7. For additional translations, developers manually import and apply them in the code.
448
+ 8. Results are checked on the page, and any errors prompt a repeat of the cycle.
449
+
450
+ This process is lengthy and redundant. Translators often lack visibility into how text is structured in code, especially when developers split strings, need to insert variables on the text, or add line breaks (`<br/>`). As a result, the translation burden falls back to the developers.
451
+
452
+ ### ☺️ Developer-Centric Translation Workflow
453
+
454
+ This CLI streamlines the process by shifting the focus to developers:
455
+
456
+ 1. Developers identify and mark variables or text for translation directly in the code.
457
+ 2. Run the CLI to **register** changes (it automatically adds translations using Google Translate with support for Google Spreadsheets).
458
+ 3. Translators inspect and refine the translations for improved fluency and meaning on the Google Sheet.
459
+ 4. Developers **import** the confirmed translation data, completing the process.
460
+
461
+ With this approach, unnecessary back-and-forth is eliminated, and translations align seamlessly with code.
462
+
463
+ ## PreRequisite πŸ“
464
+
465
+ This library is tightly integrated with the @sheet-i18n/react package, which provides the React components needed for rendering translations efficiently. As a result, you must set up **`@sheet-i18n/react`** in your project before using this CLI.
466
+
467
+ #### 1. Initialize i18n Store
468
+
469
+ This store will be used as a core translations module.
470
+
471
+ ```tsx
472
+ import en from './en.json';
473
+ import ko from './ko.json';
474
+
475
+ import { I18nStore } from '@sheet-i18n/react';
476
+
477
+ export const i18nStore = new I18nStore({
478
+ supportedLocales: ['ko', 'en'],
479
+ defaultLocale: 'ko',
480
+ localeSet: {
481
+ ko,
482
+ en,
483
+ },
484
+ typeSafe: false, // optional (default: true)
485
+ });
486
+ ```
487
+
488
+ > πŸ’‘ **typeSafe?** <br/>
489
+ > I18nStore is type-safe by default. So, basically, you can't add the translation data that is not defined in the locale Json files. However, for fast development, you can off the typeSafe option manually.
490
+ >
491
+ > ```tsx
492
+ > // typeSafe: false
493
+ > const YourComponent = () => {
494
+ > // can accept any string sheet title.
495
+ > const { t } = useTranslation('header');
496
+ >
497
+ > return (
498
+ > <div>
499
+ > {/* "t" function will accept and return any string */}
500
+ > <button>{t('login')}</button>
501
+ > </div>
502
+ > );
503
+ > };
504
+ > ```
505
+
506
+ #### 2. Create i18n Context
507
+
508
+ ```tsx
509
+ import { i18nStore } from './file-path-of-i18nStore-initiated';
510
+ import { createI18nContext } from '@sheet-i18n/react';
511
+
512
+ export const { IntlProvider, useTranslation } = createI18nContext(i18nStore);
513
+ ```
514
+
515
+ #### 3. Mount Intl Context Provider in your App
516
+
517
+ ```tsx
518
+ import React from 'react';
519
+ import { IntlProvider } from './i18nContext';
520
+
521
+ const App = () => {
522
+ return (
523
+ <IntlProvider>
524
+ <YourComponent />
525
+ </IntlProvider>
526
+ );
527
+ };
528
+ ```
529
+
530
+ #### 4. Use Translations
531
+
532
+ ```tsx
533
+ import React from 'react';
534
+ import { useTranslation } from './i18nContext';
535
+
536
+ const YourComponent = () => {
537
+ const { t } = useTranslation('header');
538
+
539
+ return (
540
+ <div>
541
+ <button>{t('login')}</button>
542
+ <button>{t('logout')}</button>
543
+ </div>
544
+ );
545
+ };
546
+ ```
547
+
548
+ Follow the documentation for @sheet-i18n/react to ensure proper configuration.
549
+
550
+ ## Base workflow
551
+
552
+ Install the CLI tool globally or as a dev dependency:
553
+
554
+ ```shell
555
+ npm add -g @sheet-i18n/cli
556
+ ```
557
+
558
+ or
559
+
560
+ ```shell
561
+ npm add --save-dev @sheet-i18n/cli
562
+ ```
563
+
564
+ Initialize the CLI in your project:
565
+
566
+ ```shell
567
+ npx sheet-i18n init
568
+ ```
569
+
570
+ After initialization, the **"sheet.config.json"** file will be created in the root of your project (and .gitignored automatically).
571
+
572
+ ### Configuration βš™οΈ
573
+
574
+ The CLI requires a `sheet.config.json` file in the root of your project. Run `sheet-i18n init` to generate this file automatically.
575
+
576
+ Example `sheet.config.json`:
577
+
578
+ ```json
579
+ {
580
+ "googleSheetConfig": {
581
+ "credentials": {
582
+ "sheetId": "YOUR_GOOGLE_SHEET_ID",
583
+ "clientEmail": "YOUR_CLIENT_EMAIL",
584
+ "privateKey": "YOUR_PRIVATE_KEY"
585
+ },
586
+ "defaultLocale": "en",
587
+ "supportedLocales": ["en", "fr", "es"],
588
+ "ignoredSheets": ["BASE"],
589
+ "exportPath": "./src/locales"
590
+ }
591
+ }
592
+ ```
593
+
594
+ #### 1. Watch command configuration
595
+
596
+ - `watchDirectory` (optional): Path of the directory to monitor during `watch` command based on the current working directory (default: `src`)
597
+ - `detectExtensions` (optional): File extensions to monitor (default: `jsx`, `js`, `tsx`, `ts`).
598
+
599
+ #### 2. Register command configuration
600
+
601
+ - **`googleSheetConfig (required)`**: Configuration for Google Sheets integration:
602
+ - **`spreadsheetId (required)`**: The ID of your Google Spreadsheet.
603
+ - **`credentials (required)`**: Contains `sheetId`, `clientEmail`, and `privateKey` for API authentication.
604
+ - **`defaultLocale (required)`**: Default language/locale used in the sheet header.
605
+ - **supportedLocales** (optional): List of locales supported in your sheet (default: [defaultLocale]).
606
+ - **headerStartRowNumber** (optional): Row number where headers start (default: 1).
607
+ - **ignoredSheets** (optional): Titles of sheets to exclude from the translation process.
608
+ - **exportPath** (optional): Directory path to save exported translations based on the current working directory (default: `.`).
609
+
610
+ ## Commands
611
+
612
+ ### 🎬 init
613
+
614
+ ```shell
615
+ npx sheet-i18n init
616
+ ```
617
+
618
+ Sets up the `sheet.config.json` file in your project. This configuration file is required for all other commands.
619
+
620
+ ### πŸ‘€ watch
621
+
622
+ ```shell
623
+ npx sheet-i18n watch [options]
624
+ ```
625
+
626
+ Monitors files or directories for changes and logs relevant updates.
627
+ When watch mode is started, the CLI will detect changes of "useTranslation" and "t" calls of "@sheet-i18n/react" package.
628
+
629
+ ```tsx
630
+ //translationContext.tsx
631
+ import { createI18nContext } from 'sheet-i18n/react';
632
+
633
+ export const { IntlProvider, useTranslation } = createI18nContext(i18nStore);
634
+
635
+
636
+ // Component.tsx
637
+ import { useTranslation } from '../translationContext'
638
+
639
+ ...
640
+
641
+ function Component (){
642
+ // The arguments of useTranslation function = sheetTitle
643
+ const { t } = useTranslation('environment');
644
+ ...
645
+
646
+ // The arguments of t function = translation key
647
+ return (
648
+ <>
649
+ <SettingItem label={t('over')}>
650
+ <SettingItem label={t('under')}>
651
+ </>
652
+ )
653
+ }
654
+ ```
655
+
656
+ The _watch result_ is
657
+
658
+ ```shell
659
+ πŸ“ Translations Store:
660
+ {
661
+ environment: Set(1) { 'over', 'under' }
662
+ }
663
+ ```
664
+
665
+ #### Options:
666
+
667
+ - `-d, --directory <directory>`: Specify the directory to watch.
668
+
669
+ Example:
670
+
671
+ ```shell
672
+ npx sheet-i18n watch --directory src
673
+ ```
674
+
675
+ ### πŸ“€ register
676
+
677
+ ```shell
678
+ sheet-i18n register [options]
679
+ ```
680
+
681
+ Registers translation data to Google Sheets.
682
+
683
+ #### Remarkable features:
684
+
685
+ 1. The `register` command collects the current sheets in Google Spreadsheets. If there are sheets in your local changes that do not exist in the current list, it prompts you to create the missing sheets.
686
+
687
+ 2. It updates rows and adds "translation" entries using the `GOOGLETRANSLATE` function supported by Google Spreadsheets for automated translations.
688
+
689
+ 3. After registering, it asks whether you want to update the locale JSON data locally. It then exports the JSON data to the `exportPath` specified in `sheet.config.json` (default is the current working directory).
690
+
691
+ #### Options:
692
+
693
+ - `-d, --directory <directory>`
694
+ Specify the directory to scan for translation data, relative to the current working directory.
695
+ **Default**: `src`
696
+
697
+ - `-s, --scope <scope>`
698
+ Define the scope of the registration process:
699
+ - **`diff`**: Only scans translation keys that have been modified in the Git history (based on `git diff`).
700
+ - **`total`**: Scans the entire directory for all translation keys and updates the spreadsheet, regardless of Git changes.
701
+ **Default**: `diff`
702
+
703
+ ---
704
+
705
+ #### Detailed description of the `scope` option:
706
+
707
+ ##### Scope: `diff`
708
+
709
+ - The CLI identifies changes in translation keys by comparing the current state of the codebase with the latest Git commit.
710
+ - It only processes translation keys added, removed, or modified since the last commit.
711
+ - This is useful for incremental updates, ensuring only new or updated keys are registered.
712
+
713
+ ##### Example:
714
+
715
+ ```shell
716
+ # diff is the default scope. So you can omit the --scope option.
717
+ npx sheet-i18n register
718
+ ```
719
+
720
+ ---
721
+
722
+ ##### Scope: `total`
723
+
724
+ - The CLI scans the entire specified directory for all translation keys from the directory path you provide.
725
+ - This is useful for full synchronization, ensuring all keys are registered in the spreadsheet.
726
+ - However, it may take longer to process large directories. So, use with caution.
727
+
728
+ ##### Example:
729
+
730
+ ```shell
731
+ sheet-i18n register --scope total
732
+ ```
733
+
734
+ ---
735
+
736
+ ## License πŸ“œ
737
+
738
+ This project is licensed under the ISC License. See the LICENSE file for details.
739
+
740
+ ## Author ✍️
741
+
742
+ Created by [devAnderson](https://github.com/chltjdrhd777).
743
+
744
+ </details>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sheet-i18n",
3
- "version": "1.2.4",
3
+ "version": "1.3.2",
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/react": "0.3.4",
40
- "@sheet-i18n/exporter": "1.2.0"
39
+ "@sheet-i18n/exporter": "1.3.2",
40
+ "@sheet-i18n/react": "0.4.2"
41
41
  },
42
42
  "devDependencies": {
43
43
  "tsup": "^6.0.0",
44
44
  "typescript": "^5.0.0",
45
- "@sheet-i18n/typescript-config": "1.2.0"
45
+ "@sheet-i18n/typescript-config": "1.3.2"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsup",