Home/i18nexus-tools/i18n-extractor
๐Ÿ”

i18n-extractor

Extract translation keys and intelligently merge with existing files

Overview

i18n-extractoris a CLI tool that extracts all translation keys wrapped with the t() function from your code, generates per-language JSON files, and intelligently merges them with existing translations.

โœ“Automatically extract translation keys from code
โœ“Preserve all existing translations
โœ“Smart merge that only adds new keys
โœ“Sort keys alphabetically
โœ“Detailed stats and reports

Installation

Install globally (recommended):

npm install -g i18nexus-tools

Or run directly with npx:

npx i18n-extractor

Basic Usage

Basic Extraction and Merging

npx i18n-extractor -p "app/**/*.tsx" -d "./locales"

Extracts translation keys from all .tsx files in the 'app' directory and saves them to the 'locales' folder.

Create specific language files

npx i18n-extractor -l "en,ko,ja"

Creates English, Korean, and Japanese translation files.

Preview Mode

npx i18n-extractor --dry-run

Checks extractable keys and stats without modifying files.

How it works

1

Code Scan

Scans all files matching the specified pattern to find t() function calls.

// Looks for code like this:
t("Welcome")
t("Get Started")
t(\`Hello \${name}\`)
2

Key Extraction

Extracts the first argument of the t() function as the translation key.

// Extracted keys:
"Welcome"
"Get Started"
"Hello \${name}"
3

Merge with existing translations

Reads existing translation files, adds only new keys, and preserves all existing translations.

Existing File:

{
  "์•ˆ๋…•": "Hello",
  "ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค": "Welcome"
}

After Merge:

{
  "์•ˆ๋…•": "Hello",
  "์‹œ์ž‘ํ•˜๊ธฐ": "์‹œ์ž‘ํ•˜๊ธฐ",
  "ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค": "Welcome"
}
4

Save File

Saves JSON files for each language with sorted keys and displays stats.

โœ“ Extraction complete
  - New keys: 5
  - Existing keys: 120
  - Total keys: 125
  - Files: locales/ko.json, locales/en.json

Command Options

--pattern, -pString

Specifies the glob pattern for files to scan.

--pattern "app/**/*.{ts,tsx}"
--directory, -dString

Specifies the directory path to save translation files.

--directory "./locales"
--languages, -lString

Specifies the language files to create, separated by commas. Default: en,ko

--languages "en,ko,ja,zh"
--dry-runFlags

Displays extracted keys and stats without actually creating files.

--help, -hFlags

Displays the help message.

Example Output

Initial Extraction (New Project)

$ npx i18n-extractor -p "app/**/*.tsx" -d "./locales"

๐Ÿ” Scanning files...
   Found 25 files

๐Ÿ“ Extracting translation keys...
   Found 45 unique keys

๐Ÿ“ Creating translation files...
   โœ“ locales/ko.json (45 keys)
   โœ“ locales/en.json (45 keys)

โœ… Extraction complete!
   - New keys: 45
   - Total keys: 45

Incremental Update (Existing Project)

$ npx i18n-extractor -p "app/**/*.tsx" -d "./locales"

๐Ÿ” Scanning files...
   Found 30 files

๐Ÿ“ Extracting translation keys...
   Found 52 unique keys

๐Ÿ“ Merging with existing translations...
   Existing keys: 45
   New keys: 7
   
   โœ“ locales/ko.json (52 keys, +7 new)
   โœ“ locales/en.json (52 keys, +7 new)

โœ… Extraction complete!
   - New keys: 7
   - Existing keys: 45
   - Total keys: 52
   
๐Ÿ’ก Don't forget to translate the new keys in en.json

Smart merge feature

Guarantees during merge:

  • โœ“
    100% preservation of existing translations

    Never overwrites already translated keys. Manually written translations are kept safe.

  • โœ“
    Add new keys only

    Only new keys added to the code will be added to the translation file.

  • โœ“
    Alphabetical Sorting

    All keys are sorted alphabetically, making them easy to find and keeping git diffs clean.

  • โœ“
    Independent management per language

    Each language file is managed independently; changes in one language do not affect others.

Best Practices

โœ…Run frequently

Run the extractor whenever you add new features to keep translation files up-to-date.

โœ…Integrate into CI/CD

Add --dry-run to your build process to check for missing keys.

# package.json
"scripts": {
  "check:i18n": "i18n-extractor --dry-run"
}

โœ…Version control for translation files

Commit the generated translation files to Git to share with your team.

๐Ÿ’กAdd English translations

After running the extractor, open en.json and add English translations for the newly added keys.

Next Steps