You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

tr-file

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tr-file

A fast command-line tool for translating JSON files using Google Translate API. Perfect for i18n workflows with batch translation, recursive search, and incremental updates.

1.0.0
Source
npmnpm
Version published
Weekly downloads
7
-22.22%
Maintainers
1
Weekly downloads
 
Created
Source

tr-file

A command-line tool for translating JSON files using Google Translate API. Perfect for i18n (internationalization) workflows.

Installation

npm install -g tr-file

Usage

tr_file <source-file> <target-languages> [options]

Examples

# Translate en.json to Spanish, Japanese, and Portuguese (uses built-in API key)
tr_file en.json es,ja,pt

# With custom API key
tr_file en.json es,ja,pt -k YOUR_API_KEY

# With custom delay between requests
tr_file en.json es,ja,pt -d 200

# Recursive translation - find all 'en.json' files in subdirectories
tr_file en.json es,fr,de --recursive

Recursive Translation

Search for and translate all files with a specific name in all subdirectories:

# Find all 'en.json' files recursively and translate them
tr_file en.json es,fr,de --recursive

# Short form with -r flag
tr_file en.json es,fr,de -r

Recursive Example:

project/
├── frontend/
│   └── locales/
│       ├── en.json     ← Found and translated
│       ├── es.json     ← Generated
│       └── fr.json     ← Generated
├── backend/
│   └── i18n/
│       ├── en.json     ← Found and translated
│       ├── es.json     ← Generated
│       └── fr.json     ← Generated
└── mobile/
    └── lang/
        ├── en.json     ← Found and translated
        ├── es.json     ← Generated
        └── fr.json     ← Generated

Options

  • -k, --key <key>: Google Translate API key (optional - uses built-in key if not provided)
  • -d, --delay <ms>: Delay between requests in milliseconds (default: 50ms)
  • -r, --recursive: Search for source file in all subdirectories

Setup

Quick Start: The tool works immediately with a built-in API key - no setup required!

# Install and use right away
npm install -g tr-file
tr_file en.json es,ja,pt

Optional - Use your own API key:

  • Get a Google Translate API key:

    • Go to Google Cloud Console
    • Enable the Google Translate API
    • Create credentials and get your API key
  • Set your API key (choose one method):

    # Option 1: Environment variable
    export GOOGLE_TRANSLATE_API_KEY="your-api-key-here"
    
    # Option 2: Use the -k flag
    tr_file en.json es,ja,pt -k "your-api-key-here"
    

Features

  • Batch Translation: Translates multiple strings in single API calls for maximum speed
  • Smart Deduplication: Only translates unique strings, avoiding redundant API calls
  • Incremental Updates: Only translates missing keys, preserves existing translations
  • Recursive Translation: Find and translate files in all subdirectories
  • Preserves JSON Structure: Maintains nested objects and arrays
  • Progress Tracking: Visual indicators with spinners and progress info
  • Multiple Languages: Translate to multiple target languages in one command
  • Error Handling: Graceful error handling and recovery
  • Rate Limiting: Configurable delay between requests
  • Fast Performance: Can translate 100+ strings in under 2 seconds
  • Built-in API Key: Works out of the box without setup

Input/Output Example

Input (en.json):

{
  "greeting": "Hello",
  "navigation": {
    "home": "Home",
    "about": "About Us"
  },
  "messages": {
    "welcome": "Welcome to our app",
    "goodbye": "Thank you for visiting"
  }
}

Output (es.json):

{
  "greeting": "Hola",
  "navigation": {
    "home": "Inicio",
    "about": "Acerca de nosotros"
  },
  "messages": {
    "welcome": "Bienvenido a nuestra aplicación",
    "goodbye": "Gracias por visitarnos"
  }
}

Supported Languages

The tool supports all languages supported by Google Translate API. Common language codes include:

  • es - Spanish
  • fr - French
  • de - German
  • it - Italian
  • pt - Portuguese
  • ja - Japanese
  • ko - Korean
  • zh - Chinese
  • ru - Russian
  • ar - Arabic

Incremental Translation

The tool intelligently handles existing translation files:

  • Preserves Existing Translations: Never overwrites existing translations
  • Only Adds Missing Keys: Translates only keys that don't exist in the target file
  • Merge Functionality: Combines new translations with existing ones
  • No Redundant API Calls: Skips translation if all keys already exist

Example:

# First run - translates all keys
tr_file en.json es  # Creates es.json with all translations

# Add new keys to en.json
echo '{"new.key": "New text"}' >> en.json

# Second run - only translates new keys
tr_file en.json es  # Only translates "new.key", preserves existing translations

Performance

The tool uses batch translation for maximum efficiency:

  • Smart Deduplication: Only translates unique strings, avoiding redundant API calls
  • Batch Processing: Groups multiple strings into single API requests (up to 128 strings per batch)
  • Parallel Language Processing: Translates to multiple languages concurrently
  • Optimized Rate Limiting: Minimal delays between requests while respecting API limits

Example Performance:

  • 100 strings → 3 languages = 300 translations in ~1.3 seconds
  • Small files (10-20 strings) complete in under 1 second
  • Large files (100+ strings) complete in 1-3 seconds per language

Rate Limiting

The tool includes built-in rate limiting to avoid hitting Google Translate API limits. You can adjust the delay between requests using the -d option.

Error Handling

  • If a translation fails, the original text is preserved
  • Network errors are handled gracefully
  • Invalid JSON files are detected and reported
  • Missing API keys are reported with helpful messages

License

MIT

Keywords

translate

FAQs

Package last updated on 11 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.