Skip to content

dv-net/translator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌍 @dv.net/translator

Official translation CLI and library by DV Net for JSON and Markdown files into multiple locales. Supports incremental JSON updates with hash tracking, parallel translation threads, Google Translate (JSON) and OpenAI GPT (JSON / Markdown).

⚑️ Quick start

  • πŸ“¦ Install with yarn
yarn add -D @dv.net/translator
  • πŸ’» Use in a consumer project
{
  "devDependencies": {
    "@dv.net/translator": "^1.0.0"
  },
  "scripts": {
    "i18n": "dv-translator translate --dir i18n/locales --all --exclude ru",
    "i18n:md": "dv-translator translate-md --dir blog/translates --all --exclude ru",
    "i18n:status": "dv-translator status --dir i18n/locales"
  }
}

Project layout:

my-app/
β”œβ”€β”€ i18n/locales/
β”‚   β”œβ”€β”€ en.json              # source
β”‚   β”œβ”€β”€ ru.json              # ← generated
β”‚   β”œβ”€β”€ es.json              # ← generated
β”‚   └── .translation-hashes.json
└── blog/translates/
    β”œβ”€β”€ en.md                # source
    β”œβ”€β”€ ru.md                # ← generated
    └── es.md                # ← generated

πŸ“˜ CLI commands

Both translation commands work the same way: --dir points to the directory containing the English source file. Translations are written into the same directory.

JSON

dv-translator translate --dir i18n/locales --all
dv-translator translate --dir i18n/locales --all --exclude ru
dv-translator translate --dir i18n/locales -l ru,es,fr --exclude ru
dv-translator translate --dir i18n/locales --all -f
  • Reads i18n/locales/en.json
  • Writes i18n/locales/ru.json, es.json, ...
  • Hashes: i18n/locales/.translation-hashes.json
  • Default provider: Google (no API key required)

Markdown

dv-translator translate-md --dir blog/translates --all
dv-translator translate-md --dir blog/translates --all --exclude ru
dv-translator translate-md --dir blog/translates -l ru,es --exclude ru
dv-translator translate-md --dir blog/translates --all -f
  • Reads blog/translates/en.md
  • Writes blog/translates/ru.md, es.md, ...
  • Requires an OpenAI API key

πŸ”Œ Providers

Provider When API key
google Default for JSON Not required
gpt -p gpt (JSON) or translate-md OpenAI

API key resolution order:

  1. -k/--key
  2. OPENAI_API_KEY environment variable
  3. key.txt in the project root
# JSON via Google
dv-translator translate --dir i18n/locales -l ru,es

# JSON via GPT
dv-translator translate --dir i18n/locales -l ru -p gpt -k $OPENAI_API_KEY

🚩 Common flags

Flag Description Default
--dir Directory with en.json / en.md required
-l, --locales Comma-separated locales ru,es,fr,de
--all All locales except en β€”
--exclude Locales to skip β€”
-f, --force Re-translate everything / overwrite existing files β€”
-t, --threads Parallel threads 5
-k, --key OpenAI API key env / key.txt
-m, --model GPT model gpt-4o-mini

Additional flags for translate: -p, --provider (google / gpt)

Additional flags for translate-md: --temperature (0–2, default 1)

🧰 Utility commands

dv-translator status --dir i18n/locales
dv-translator clean --dir i18n/locales
dv-translator info

clean removes {locale}.json, {locale}.md, and .translation-hashes.json from the directory. It does not touch en.json or en.md.

🧩 Programmatic API

import Translator from '@dv.net/translator';

const translator = new Translator({ provider: 'google', maxConcurrent: 5 });
await translator.translateJsonFile('i18n/locales/en.json', ['ru', 'es'], 'i18n/locales');

🧰 Scripts

  • ▢️ start β€” run the CLI locally (node bin/cli.js)
  • πŸ“¦ pack:check β€” verify publishable package contents (npm pack --dry-run)

πŸ› οΈ Tech stack

  • βš™οΈ Node.js ESM (>=18)
  • πŸ–₯️ Commander β€” CLI
  • 🌐 Google Translate β€” free JSON translation
  • πŸ€– OpenAI β€” GPT translation for JSON and Markdown
  • πŸ“ fs-extra β€” file I/O

πŸ—‚οΈ Project structure

bin/
  cli.js              # CLI entry point
src/
  index.js            # public API (Translator class)
  commands/           # translate, translate-md, status, clean
  providers/          # google, gpt
  utils/              # helpers
  langs.js            # supported locales

πŸ§‘β€πŸ’» Development

  1. πŸ“¦ Install dependencies
yarn install
  1. ▢️ Run the CLI
yarn start info
yarn start translate --dir ./locales --all
  1. πŸ“¦ Verify package before publish
yarn pack:check

πŸ“¦ Publishing

Publishing is automated via GitHub Release:

  1. Bump version in package.json
  2. Create a GitHub Release with tag vX.Y.Z matching package.json
  3. The workflow publishes to npm as @dv.net/translator

Requires NPM_TOKEN in the npm GitHub environment.

βš™οΈ Environment requirements

  • πŸ–₯️ Node.js >=18
  • πŸ”‘ OPENAI_API_KEY β€” only for GPT / Markdown translation

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


Made with ❀️ for the developer community

GitHub npm

About

🌍 Translator

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors