@sheet-i18n/cli v1.6.1
@sheet-i18n/cli β¨
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:
- Translator identifies the text to be translated from the page (without developer involvement).
- Translator adds the translation data to a Google Spreadsheet.
- Developer creates a script to locally download the translation data from the sheet.
- Developer inspects the translation data, matches it with the code, and finds the corresponding values in the source code (this can be time-consuming).
- If discrepancies exist between the spreadsheet and the code, developers request changes from the translator.
- Once resolved, the developer applies the translations using an internationalization (intl) library.
- For additional translations, developers manually import and apply them in the code.
- 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:
- Developers identify and mark variables or text for translation directly in the code.
- Run the CLI to register changes (it automatically adds translations using Google Translate with support for Google Spreadsheets).
- Translators inspect and refine the translations for improved fluency and meaning on the Google Sheet.
- 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 exportsIntlProvider
,useTranslation
, andgetTranslation
- 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
Option | Type | Required | Default | Description |
---|---|---|---|---|
watchEntry | string | "src" | Entry path to monitor file changes, relative to the current working directory. | |
detectExtensions | string[] | "jsx", "js", "tsx", "ts" | List of file extensions to watch. |
2. Register command configuration
π googleSheetConfig
Options
Option | Type | Required | Default | Description |
---|---|---|---|---|
credentials.sheetId | string | β | The same ID as spreadsheetId (for API auth). | |
credentials.clientEmail | string | β | Client email from your Google Service Account credentials. | |
credentials.privateKey | string | β | Private key from your Google Service Account credentials. | |
defaultLocale | string | β | The default language/locale used in the sheet's header row. | |
supportedLocales | string[] | β | Array of all supported locale codes (e.g., ["en", "ko"] ). | |
headerStartRowNumber | number | 1 | Row number where sheet headers begin. | |
ignoredSheets | string[] | Titles of sheets to ignore during register/import (e.g., ["BASE"] ). | ||
importPath | string | current working directory | Directory path to export translation JSON files. | |
autoAppendFilePathColumn | object | enabled: truecolumnName: "__filePath"fullFilePath:false | Configuration 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:
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.It updates rows and adds "translation" entries using the
GOOGLETRANSLATE
function supported by Google Spreadsheets for automated translations.After registering, it asks whether you want to update the locale JSON data locally. It then exports the JSON data to the
importPath
specified insheet.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 ongit 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)"
}
}
26 days ago
26 days ago
27 days ago
1 month ago
1 month ago
1 month ago
1 month ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
6 months ago
6 months ago