Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

i18n-cli

AI-native, AST-powered internationalization CLI for modern JavaScript & TypeScript apps.

Node.js TypeScript Tests Frameworks

Auto-detects your framework, surgically transforms hardcoded UI copy into idiomatic i18n bundles, translates with your preferred AI provider, and proves nothing broke through a 7-stage verification gate with automatic rollback.

Quick Start · Supported Frameworks · How It Works · CLI Reference · Contributing


Why i18n-cli?

Most automated i18n tools rely on brittle regexes or let LLMs rewrite raw source code—silently breaking component syntax, mangling placeholders, or translating strings used in state comparisons (location === "Goa").

i18n-cli is built on a single principle: correctness and honesty over coverage.

  • AST & SFC Only (Zero Regex Hacks): Uses @babel/parser for JSX/TSX and official SFC compilers (@vue/compiler-sfc, svelte/compiler) + magic-string for format-preserving edits in .vue and .svelte files. The LLM never touches your code syntax.
  • Defeats the Dual-Use Trap: Detects strings used as internal state tokens, route keys, CSS classes, or directives and leaves them untouched—routing genuinely ambiguous strings to review_needed.json rather than guessing silently.
  • 7-Stage Fail-Loud Verification Gate: Every run verifies syntax, production build, pseudo-localization, 100% key parity, 100% placeholder preservation, plural completeness, and untranslated fallbacks.
  • Atomic Rollback & Signal Safety: If any gate fails—or you press Ctrl+C mid-run—every modified file is restored atomically from transaction snapshots.
  • Bring Your Own AI (or Local LLM): Supports Google Gemini, OpenAI, DeepL, and any OpenAI-compatible local endpoint (Ollama, Groq, vLLM), plus an offline fallback for CI/testing.

Quick Start

Prerequisites

  • Node.js ≥ 18
  • pnpm ≥ 9 (or npm)
  • git

Install & Link

git clone <your-repo-url>
cd internationalization-tool
pnpm install
pnpm run build
pnpm link --global   # exposes `i18n-cli` globally

1. Configure an AI Provider

# Interactive setup wizard (Gemini, OpenAI, DeepL, or local Ollama)
i18n-cli setup

# Or export an API key directly
export GEMINI_API_KEY="your-api-key"

2. Preview Changes Safely (--dry-run)

# Zero disk modifications — inspects detected framework, keys, diffs, and estimated cost
i18n-cli run ./my-app --languages es,fr,de --dry-run

3. Internationalize Your App

# Auto-detects framework, rewrites UI files, scaffolds runtime + LanguageSwitcher, translates & verifies
i18n-cli run ./my-app --languages es,fr,de

# Or run directly against any public GitHub repository in a sandboxed clone
i18n-cli run https://github.com/user/repo --languages es,sw --min-coverage 60

Supported Frameworks

i18n-cli automatically detects your framework from package.json and project configuration files (or override explicitly with --framework <id>):

Framework --framework Parser / Editor Target i18n Runtime Message Format Highlights
React (SPA) react @babel/parser react-i18next i18next ({{var}}) useTranslation(), <Trans>, src/i18n.js scaffolding, ranked nav switcher injection
Next.js (App & Pages) nextjs @babel/parser next-intl icu ({var}, nested JSON) RSC-aware (await getTranslations() in async Server Components vs useTranslations() in "use client" / sync RSCs), t.rich(), cookie-based switcher (NEXT_LOCALE + router.refresh()), next.config.* plugin wiring
SolidJS / SolidStart solid @babel/parser @solid-primitives/i18n i18next ({{var}}) Zero-arg signal getter extraction (count(), user().name), reactivity-safe object-literal triage, per-component tp() plural helper, I18nProvider & <For> switcher
Vue 3 vue @vue/compiler-sfc + magic-string vue-i18n vue-i18n ({var}, pipe plurals) <script setup> (useI18n()) & Options API / template-only ($t()), :attr binding, <style> & comment preservation, src/i18n.ts + main.ts wiring
Nuxt 3 / Nuxt 4 nuxt @vue/compiler-sfc + magic-string @nuxtjs/i18n vue-i18n ({var}, pipe plurals) Nuxt 3 (root) & Nuxt 4 (app/) layouts, auto-imported useI18n() & components/LanguageSwitcher.vue, nuxt.config.ts wiring, i18n/locales/
Svelte 4/5 & SvelteKit svelte svelte/compiler (modern AST) + magic-string svelte-i18n icu ({var}, nested JSON) Svelte 4 & Svelte 5 Runes ($state, $props), {#if}/{#each}/<svelte:head>, route-scoped SvelteKit keys, $lib/i18n & +layout wiring

How It Works

flowchart TD
    A[Local Workspace or Git URL] --> B[Framework Detector<br/>React · Next.js · Solid · Vue · Nuxt · Svelte]
    B --> C[AST / SFC Semantic Classifier]
    C -->|User-Visible Copy| D[Surgical Codemod<br/>Babel AST or magic-string]
    C -->|Code, Directives, SVG| E[Skip List]
    C -->|Ambiguous / Dual-Use| F[review_needed.json]
    D --> G[MessageFormat Adapter<br/>i18next · ICU · vue-i18n]
    G --> H[AI Translation Layer<br/>Gemini · OpenAI · DeepL · Ollama + Cache]
    D --> I[7-Stage Verification Gate<br/>Build · Pseudo · Parity · Placeholders · Plurals]
    H --> I
    I -->|Pass| J[Commit Locales, Runtime & report.md]
    I -->|Fail / SIGINT| K[Atomic Rollback & Exit 1]
Loading

Verification Gate Checks

Every non-dry run must pass all 7 automated gates before changes are committed:

  1. Syntax & Parse Diagnostics — Zero syntax regressions introduced by AST/SFC transforms.
  2. Target Production Build — Auto-detects pnpm, yarn, bun, or npm and runs the target project's build script.
  3. Pseudo-Localization Stress Test — Generates an accented, length-expanded pseudo.json preserving {placeholders}, {{placeholders}}, ICU plural syntax, and inline tags.
  4. 100% Key Parity — Every target locale matches the exact key hierarchy of the source bundle.
  5. 100% Placeholder Integrity — Verifies no interpolation variable or rich-text tag was dropped or renamed by the AI provider.
  6. Translation Quality Check — Rejects silent English fallbacks in non-English bundles (unless --continue-on-error routes them to review_needed.json).
  7. CLDR Plural Completeness — Validates required plural categories per language via Intl.PluralRules (e.g., one, few, many, other for Russian/Arabic).

CLI Reference

Usage: i18n-cli <command> [options]

Commands:
  run <target-dir|url>     Run full pipeline (scan, classify, rewrite, translate, verify, report)
  scan <target-dir|url>    Scan and classify strings without modifying files
  verify <target-dir|url>  Run the 7-stage verification gate on a codebase
  setup                    Interactive wizard to configure AI provider and API keys
  config <get|set|test>    Inspect, update, or test active AI provider credentials

Key Options:
  --framework <name>       Framework override (auto-detects: react, nextjs, solid, vue, nuxt, svelte)
  --languages, -l <codes>  Comma-separated target locales (e.g. es,fr,de,ja; default: sw,es)
  --source, -s <code>      Source locale code (default: en)
  --provider <name>        AI provider: gemini (default), openai, deepl
  --model, -m <name>       Model name (e.g. gemini-3.8-flash, gpt-4o-mini, llama3)
  --base-url <url>         Custom OpenAI-compatible endpoint (Ollama, Groq, vLLM)
  --dry-run                Preview diffs and cost estimates with zero file writes
  --min-coverage <val>     Minimum extraction coverage percentage (default: 90)
  --continue-on-error      Route failed translations to review_needed.json instead of aborting
  --build-command <cmd>    Override the target project's build verification command
  --no-verify              Skip verification gate
  --no-atomic              Disable automatic rollback on failure
  --out-dir <path>         Output directory for report.md and review_needed.json
  --branch, -b <name>      Git branch or tag for remote URL targets

Multi-Provider Examples

# Google Gemini (default)
export GEMINI_API_KEY="your-key"
i18n-cli run ./app --provider gemini --model gemini-3.8-flash

# OpenAI
export OPENAI_API_KEY="your-key"
i18n-cli run ./app --provider openai --model gpt-4o-mini

# Local LLM via Ollama (free, no API key required)
i18n-cli run ./app --provider openai --model llama3 --base-url http://localhost:11434/v1

# DeepL (with XML tag shielding)
export DEEPL_API_KEY="your-key"
i18n-cli run ./app --provider deepl

Project Structure

src/
├── cli.ts                  # CLI entrypoint, command dispatch & TransactionManager
├── frameworks/             # Pluggable FrameworkAdapter & MessageFormatAdapter ecosystem
│   ├── types.ts            # Core adapter interfaces
│   ├── registry.ts         # Auto-detection & CLI override resolution
│   ├── react.ts            # React + react-i18next adapter
│   ├── nextjs.ts           # Next.js (App & Pages Router) + next-intl adapter
│   ├── solid.ts            # SolidJS / SolidStart + @solid-primitives/i18n adapter
│   ├── vue-sfc.ts          # Shared Vue SFC classifier & magic-string rewriter
│   ├── vue.ts              # Vue 3 + vue-i18n adapter
│   ├── nuxt.ts             # Nuxt 3 / Nuxt 4 + @nuxtjs/i18n adapter
│   ├── svelte-sfc.ts       # Svelte 4/5 modern AST classifier & magic-string rewriter
│   ├── svelte.ts           # Svelte & SvelteKit + svelte-i18n adapter
│   └── formats/            # Message formats: i18next.ts, icu.ts, vue-i18n.ts
├── classify/               # Semantic AST classifier (VISIBLE, SKIP, UNCERTAIN, DATA)
├── rewrite/                # Babel JSX rewriter, key registry, placeholder builder, switcher
├── translate/              # Multi-provider AI orchestrator, cache & validators
├── verify/                 # 7-stage verification gate, build runner & pseudo-localization
├── languages/              # BCP-47 metadata, CLDR plural rules & RTL helpers
├── scan/                   # File discovery & hardened remote Git sandbox
├── report/                 # Markdown report & review_needed.json generator
└── errors/                 # Diagnostic error cards & SIGINT/SIGTERM safety coordinator

Development & Contributing

Contributions are welcome! Adding a new framework (e.g., Astro, Angular, Qwik, Preact) only requires implementing FrameworkAdapter in src/frameworks/ and registering it in src/frameworks/registry.ts.

Verification Scripts

pnpm run format   # Prettier formatting check
pnpm run build    # TypeScript compilation to ./dist
pnpm run lint     # ESLint 9 across src/ and tests/
pnpm run test     # Vitest suite (283 tests across 40 suites)
pnpm run verify   # Full CI gate: format -> build -> lint -> test

Adding a New Framework Adapter

  1. Implement FrameworkAdapter from src/frameworks/types.ts.
  2. Reuse an existing MessageFormatAdapter (i18next, icu, vue-i18n) or add a new one in src/frameworks/formats/.
  3. Register the adapter in src/frameworks/registry.ts.
  4. Add unit, E2E idempotency, and adversarial stress tests in tests/.

Documentation


License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages