|
| 1 | +# @lingo.dev/compiler |
| 2 | + |
| 3 | +Official Lingo.dev compiler with automatic translation support for React applications. |
| 4 | + |
| 5 | +This package provides plugins for multiple bundlers (Vite, Webpack, Rollup, esbuild) and a Next.js loader that |
| 6 | +automatically transforms React components to inject translation calls. It uses a hash-based metadata system to track |
| 7 | +translatable text across your application. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **Automatic JSX text transformation** - Automatically detects and transforms translatable text in JSX |
| 12 | +- **Hash-based metadata** - Generates unique hashes for each translatable text based on content, component name, and |
| 13 | + file path |
| 14 | +- **Opt-in or automatic** - Configure whether to require `'use i18n'` directive or transform all files |
| 15 | +- **Multi-bundler support** - Works with Vite, Webpack, Rollup, esbuild, and Next.js |
| 16 | +- **Built on unplugin** - Unified plugin API across all bundlers |
| 17 | +- **Metadata tracking** - Maintains `.lingo/metadata.json` with all translatable content |
| 18 | +- **Translation server** - On-demand translation generation during development |
| 19 | +- **AI-powered translations** - Support for multiple LLM providers and Lingo.dev Engine |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +```bash |
| 24 | +npm install @lingo.dev/compiler |
| 25 | +# or |
| 26 | +pnpm add @lingo.dev/compiler |
| 27 | +# or |
| 28 | +yarn add @lingo.dev/compiler |
| 29 | +``` |
| 30 | + |
| 31 | +## Structure |
| 32 | + |
| 33 | +The compiler is organized into several key modules: |
| 34 | + |
| 35 | +### Core Directories |
| 36 | + |
| 37 | +#### `src/plugin/` - Build-time transformation |
| 38 | + |
| 39 | +- **`transform/`** - Babel AST transformation logic for JSX text extraction |
| 40 | +- **`unplugin.ts`** - Universal plugin implementation (Vite, Webpack) |
| 41 | +- **`next.ts`** - Next.js-specific plugin with Turbopack and Webpack support |
| 42 | +- **`build-translator.ts`** - Batch translation generation at build time |
| 43 | +- **`virtual-modules-code-generator.ts`** - Generates code for virtual modules, dev config and locale resolvers for client and server |
| 44 | + |
| 45 | +#### `src/metadata/` - Translation metadata management |
| 46 | + |
| 47 | +- **`manager.ts`** - CRUD operations for `.lingo/metadata.json` |
| 48 | +- Thread-safe metadata file operations with file locking |
| 49 | +- Manages translation entries with hash-based identifiers |
| 50 | + |
| 51 | +#### `src/translators/` - Translation provider abstraction |
| 52 | + |
| 53 | +- **`lcp/`** - Lingo.dev Engine integration |
| 54 | +- **`pseudotranslator/`** - Development-mode fake translator |
| 55 | +- **`pluralization/`** - Automatic ICU MessageFormat detection |
| 56 | +- **`translator-factory.ts`** - Provider selection and initialization |
| 57 | + |
| 58 | +#### `src/translation-server/` - Development server |
| 59 | + |
| 60 | +- **`translation-server.ts`** - HTTP server for on-demand translations |
| 61 | +- **`cli.ts`** - Standalone CLI for translation generation |
| 62 | +- WebSocket support for real-time dev widget updates |
| 63 | +- Port management (60000-60099 range) |
| 64 | + |
| 65 | +#### `src/react/` - Runtime translation hooks |
| 66 | + |
| 67 | +- **`client/`** - Client-side Context-based hooks |
| 68 | +- **`server/`** - Server component cache-based hooks (isomorphic) |
| 69 | +- **`server-only/`** - Async server-only API (`getServerTranslations`) |
| 70 | +- **`shared/`** - Shared utilities (RichText rendering, Context) |
| 71 | +- **`next/`** - Next.js-specific middleware and locale switcher |
| 72 | + |
| 73 | +#### `src/utils/` - Shared utilities |
| 74 | + |
| 75 | +- **`hash.ts`** - Stable hash generation for translation keys |
| 76 | +- **`config-factory.ts`** - Configuration defaults and merging |
| 77 | +- **`logger.ts`** - Structured logging utilities |
| 78 | +- **`path-helpers.ts`** - File path resolution |
| 79 | + |
| 80 | +#### `src/widget/` - Development widget |
| 81 | + |
| 82 | +- In-browser translation editor overlay for development mode |
| 83 | + |
| 84 | +### Support Directories |
| 85 | + |
| 86 | +#### `tests/` - End-to-end testing |
| 87 | + |
| 88 | +- **`e2e/`** - Playwright tests for full build workflows |
| 89 | +- **`fixtures/`** - Test applications (Vite, Next.js) |
| 90 | +- **`helpers/`** - Test utilities and assertions |
| 91 | + |
| 92 | +#### `benchmarks/` - Performance benchmarks |
| 93 | + |
| 94 | +- Translation speed benchmarks |
| 95 | +- Metadata I/O performance tests |
| 96 | + |
| 97 | +#### `old-docs/` - Legacy documentation |
| 98 | + |
| 99 | +- Historical design documents and notes |
| 100 | + |
| 101 | +### Entry Points |
| 102 | + |
| 103 | +- **`src/index.ts`** - Main package exports (plugins, types) |
| 104 | +- **`src/types.ts`** - Core TypeScript types |
| 105 | + |
| 106 | +## Contributing |
| 107 | + |
| 108 | +This is a beta package under active development. Feedback and contributions are welcome! |
| 109 | + |
| 110 | +## License |
| 111 | + |
| 112 | +ISC |
0 commit comments