1.6.1 β€’ Published 26 days ago

@sheet-i18n/cli v1.6.1

Weekly downloads
-
License
ISC
Repository
-
Last release
26 days ago

@sheet-i18n/cli ✨

npm package

A CLI tool for efficient translation management using Google Sheets, with a focus on developer experience (DX).

Features

  • ✨ Initialize CLI configuration for translation workflows.
  • πŸ‘€ Watch files or directories for changes.
  • πŸ“€ Register translation data to Google Sheets.
  • πŸ› οΈ Built with TypeScript for type safety.

Core Concepts 🌟

😭 General Translation Workflow

The traditional process of managing translations with Google Spreadsheets often includes these steps:

  1. Translator identifies the text to be translated from the page (without developer involvement).
  2. Translator adds the translation data to a Google Spreadsheet.
  3. Developer creates a script to locally download the translation data from the sheet.
  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).
  5. If discrepancies exist between the spreadsheet and the code, developers request changes from the translator.
  6. Once resolved, the developer applies the translations using an internationalization (intl) library.
  7. For additional translations, developers manually import and apply them in the code.
  8. Results are checked on the page, and any errors prompt a repeat of the cycle.

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.

☺️ Developer-Centric Translation Workflow

This CLI streamlines the process by shifting the focus to developers:

  1. Developers identify and mark variables or text for translation directly in the code.
  2. Run the CLI to register changes (it automatically adds translations using Google Translate with support for Google Spreadsheets).
  3. Translators inspect and refine the translations for improved fluency and meaning on the Google Sheet.
  4. Developers import the confirmed translation data, completing the process.

With this approach, unnecessary back-and-forth is eliminated, and translations align seamlessly with code.

πŸš€ Initial Setup with CLI

@sheet-i18n/cli provides a fully automated setup for React applications.
Once installed, you can bootstrap the required i18n context, store, and folder structure by simply running:

npx sheet-i18n init

This will automatically generate:

  • A preconfigured i18nStore using @sheet-i18n/react
  • A ready-to-use i18nContext.ts that exports IntlProvider, useTranslation, and getTranslation
  • Example locale JSON files (e.g., en.json, ko.json)
  • The sheet.config.json file for managing your CLI behavior and Google Sheets credentials

πŸ’‘ All files will be created in the recommended structure (e.g. src/i18n, src/locales), and safely skipped if they already exist.


βœ… What You No Longer Have to Do Manually

  • You don’t need to manually initiate I18nStore or set up the React context.
  • You don’t need to manually prepare locale JSON templates.
  • You don’t need to manually configure sheet.config.json.

Just run the init command and you’re ready to use useTranslation() in your components.


πŸ—‚οΈ Example Output:

πŸ“¦ your-project/
β”œβ”€β”€ πŸ“„ .gitignore                      # Automatically appends 'sheet.config.json' to ignore list
β”œβ”€β”€ πŸ“„ sheet.config.json              # CLI configuration file (contains Google Sheet credentials & options)
└── πŸ“ src/
    └── πŸ“ i18n/
        β”œβ”€β”€ πŸ“„ i18nContext.tsx        # React context for IntlProvider, useTranslation, and getTranslation
        β”œβ”€β”€ πŸ“„ i18nStore.ts           # Initializes I18nStore with supported locales and translation data
        β”œβ”€β”€ πŸ“„ en.json                # Placeholder for English translation strings
        └── πŸ“„ ko.json                # Placeholder for Korean translation strings

βœ… The CLI automatically creates this structure after running npx sheet-i18n init.
πŸ›‘οΈ If sheet.config.json or src/i18n already exist, the CLI will skip the creation of these files.

after running npx sheet-i18n init, you can mount the IntlProvider in your app.

Mount Intl Context Provider in your App

import React from 'react';
import I18nContextProvider from './i18n/i18nContext';

const App = () => {
  const [locale, setLocale] = useState('en');

  return (
    <I18nContextProvider currentLocale={locale}>
      <YourComponent />
    </I18nContextProvider>
  );
};

Use Translations

// YourComponent.tsx
import React from 'react';
import { useTranslation } from './i18n/i18nContext';

const YourComponent = () => {
  const { t } = useTranslation('header');

  return (
    <div>
      <button>{t('login')}</button>
      <button>{t('logout')}</button>
    </div>
  );
};

That's it!

your translations are now ready to use in your application.

βš™οΈ sheet.config.json

The CLI requires a sheet.config.json file in the root of your project. Run sheet-i18n init to generate this file automatically.

Example sheet.config.json:

{
  "watchEntry": "src",
  "detectExtensions": ["tsx", "ts", "jsx", "js"],

  "googleSheetConfig": {
    "credentials": {
      "sheetId": "YOUR_GOOGLE_SHEET_ID",
      "clientEmail": "YOUR_CLIENT_EMAIL",
      "privateKey": "YOUR_PRIVATE_KEY"
    },
    "defaultLocale": "en",
    "supportedLocales": ["en", "fr", "es"],
    "ignoredSheets": ["BASE"],
    "importPath": "./src/locales",
    "autoAppendFilePathColumn": {
      "enabled": true,
      "columnName": "__filePath",
      "fullFilePath": false
    }
  }
}

1. File change monitoring configuration

OptionTypeRequiredDefaultDescription
watchEntrystring"src"Entry path to monitor file changes, relative to the current working directory.
detectExtensionsstring[]"jsx", "js", "tsx", "ts"List of file extensions to watch.

2. Register command configuration

πŸ“„ googleSheetConfig Options

OptionTypeRequiredDefaultDescription
credentials.sheetIdstringβœ…The same ID as spreadsheetId (for API auth).
credentials.clientEmailstringβœ…Client email from your Google Service Account credentials.
credentials.privateKeystringβœ…Private key from your Google Service Account credentials.
defaultLocalestringβœ…The default language/locale used in the sheet's header row.
supportedLocalesstring[]βœ…Array of all supported locale codes (e.g., ["en", "ko"]).
headerStartRowNumbernumber1Row number where sheet headers begin.
ignoredSheetsstring[]Titles of sheets to ignore during register/import (e.g., ["BASE"]).
importPathstringcurrent working directoryDirectory path to export translation JSON files.
autoAppendFilePathColumnobjectenabled: truecolumnName: "__filePath"fullFilePath:falseConfiguration for appending reference file path to each translation row. This indicates where the translation key is located in your source code.- enabled: Whether to append the file path column- columnName: Column name to use for file path- fullFilePath: Show full path or mask intermediate folders (e.g., "*******/folder1/folder2/file.ts")

Commands

🎬 init

npx sheet-i18n init

Sets up the sheet.config.json file in your project. This configuration file is required for all other commands.

πŸ‘€ watch

npx sheet-i18n watch

Monitors files or directories for changes and logs relevant updates. When watch mode is started, the CLI will detect changes of "useTranslation" and "t" calls of "@sheet-i18n/react" package.

//translationContext.tsx
import { createI18nContext } from 'sheet-i18n/react';

export const { IntlProvider, useTranslation } = createI18nContext(i18nStore);


// Component.tsx
import { useTranslation } from '../translationContext'

...

function Component (){
  // The arguments of useTranslation function = sheetTitle
  const { t } = useTranslation('environment');
  ...

  // The arguments of t function = translation key
  return (
      <>
          <SettingItem label={t('over')}>
          <SettingItem label={t('under')}>
      </>
  )
}

The watched result is

Detected Translations:

πŸ“„ [Sheet Title]: environment
 - over
 - under

# You can proceed using the shortcuts below:
 - Press <Shift + R> to register the detected changes.
 - Press <Ctrl + C> to cancel the watch command.

πŸ“€ register

npx sheet-i18n register [options]

Registers translation data to Google Sheets.

Remarkable features:

  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.

  2. It updates rows and adds "translation" entries using the GOOGLETRANSLATE function supported by Google Spreadsheets for automated translations.

  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.json (default is the current working directory).

Options:

  • -s, --scope <scope>
    Define the scope of the registration process:

    • diff: Only scans translation keys that have been modified in the Git history (based on git diff).
    • total: Scans the entire directory for all translation keys and updates the spreadsheet, regardless of Git changes.
    • select: User selection mode which allows you to choose the sheets to register.
  • Default: diff


πŸ“„ Detailed description of the scope option:

Scope: diff

  • The CLI identifies changes in translation keys by comparing the current state of the codebase with the latest Git commit.
  • It only processes translation keys added, removed, or modified since the last commit.
  • This is useful for incremental updates, ensuring only new or updated keys are registered.
Example:
# diff is the default scope. So you can omit the --scope option.
npx sheet-i18n register

Scope: total

  • The CLI scans the entire specified directory for all translation keys from the directory path you provide.
  • This is useful for full synchronization, ensuring all keys are registered in the spreadsheet.
  • However, it may take longer to process large directories. So, use with caution.
Example:
npx sheet-i18n register --scope total

Scope: select

  • The CLI prompts you to manually select which sheets you want to update.

  • Once the selection is made, only the chosen sheets will have their translation data updated in the Google Spreadsheet.

  • This mode is especially useful for partial updates, allowing you to skip scanning the entire directory and focus on specific sheets.

  • It’s also helpful in cases where you’ve committed changes without registering translation data during development β€” instead of syncing all translations, you can update only the relevant sheets as needed.

Example:
npx sheet-i18n register --scope select

πŸ“„ import

npx sheet-i18n import

Exports translation data from Google Sheets to local export directory. The configuration of export command is based on sheet.config.json on your root.

{
  "googleSheetConfig": {
    "credentials": {
      "sheetId": "YOUR_GOOGLE_SHEET_ID",
      "clientEmail": "YOUR_CLIENT_EMAIL",
      "privateKey": "YOUR_PRIVATE_KEY"
    },
    "defaultLocale": "The base language of your application in sheet header",
    "ignoredSheets": ["BASE"],
    "importPath": "The path of export directory of translation data. (default: current working directory)"
  }
}
1.6.1

26 days ago

1.6.0

26 days ago

1.5.0

27 days ago

1.4.3

1 month ago

1.4.1

1 month ago

1.4.0

1 month ago

1.4.0-canary.0

1 month ago

1.3.1

3 months ago

1.3.0

3 months ago

1.3.0-canary.7

3 months ago

1.3.0-canary.6

3 months ago

1.3.0-canary.5

3 months ago

1.2.23-canary.4

3 months ago

1.2.23-canary.3

3 months ago

1.2.23-canary.2

3 months ago

1.2.23-canary.1

3 months ago

1.2.23-canary.0

3 months ago

1.2.22

3 months ago

1.2.21

3 months ago

1.2.20

3 months ago

1.2.19

3 months ago

1.2.18

3 months ago

1.2.17

3 months ago

1.2.16

3 months ago

1.2.15

4 months ago

1.2.14

4 months ago

1.2.13

4 months ago

1.2.12

4 months ago

1.2.11

4 months ago

1.2.10

5 months ago

1.2.9

5 months ago

1.2.8

5 months ago

1.2.7

5 months ago

1.2.6

5 months ago

1.2.5

5 months ago

1.2.4

5 months ago

1.2.3

5 months ago

1.2.2

5 months ago

1.2.1

5 months ago

1.2.0

5 months ago

1.1.5

5 months ago

1.1.4

5 months ago

1.1.3

5 months ago

1.1.2

5 months ago

1.1.1

5 months ago

1.1.0

5 months ago

1.0.0

5 months ago

0.3.2

5 months ago

0.3.1

5 months ago

0.2.1

5 months ago

0.2.0

5 months ago

0.1.2

6 months ago

0.1.1

6 months ago