|
1 | 1 | # Laravel Gemini AI Translation Extractor |
2 | 2 |
|
3 | | -An interactive Artisan command (`translations:extract-and-generate`) that scans your Laravel project for translation keys, translates them using Google's Gemini AI, and generates the necessary language files. |
4 | | - |
5 | | -This tool is designed to dramatically speed up the localization process for both standard PHP array files (`/lang/en/messages.php`) and flat JSON files (`/lang/en.json`). |
| 3 | +An interactive Artisan command that scans your Laravel project for translation keys, translates them using Google's Gemini AI, and generates the necessary language files. |
6 | 4 |
|
7 | 5 | [](https://packagist.org/packages/jayesh/laravel-gemini-translator) |
8 | 6 | [](https://packagist.org/packages/jayesh/laravel-gemini-translator) |
9 | 7 | [](https://packagist.org/packages/jayesh/laravel-gemini-translator) |
10 | 8 |
|
11 | | -## Key Features |
| 9 | +## 🚀 Key Features |
12 | 10 |
|
13 | | -- **Interactive & Cross-Platform:** User-friendly prompts guide you through file selection. Works on all operating systems with a robust fallback for the Windows CLI. |
14 | | -- **Powerful Key Extraction:** Scans Blade, PHP, Vue, JS, and TypeScript files for translation keys using precise regular expressions. |
15 | | -- **Intelligent & Stable:** Automatically ignores `route()` and `config()` helpers to prevent false positives and sanitizes keys to prevent hangs from malformed input. |
16 | | -- **AI-Powered Translation:** Uses the Gemini AI API to provide high-quality translations for multiple languages at once. |
17 | | -- **Flexible Concurrency:** |
18 | | - - **Linux/macOS:** Use the high-performance `fork` driver for lightning-fast parallel processing. |
19 | | - - **Windows:** Automatically uses the stable, sequential `sync` driver for maximum compatibility. |
20 | | -- **Graceful Stop:** Stop the translation process at any time by pressing a key (defaults to 'q'). |
21 | | -- **Detailed Logging:** Creates a `translation_extraction_log.json` file detailing every key and where it was found, plus a `failed_translation_keys.json` for any API errors. |
22 | | -- **Correct File Generation:** Intelligently creates nested PHP arrays for `lang/en/messages.php` and flat key-value pairs for `lang/en.json` as per Laravel conventions. |
| 11 | +- **Interactive & Cross-Platform:** Works on all operating systems with robust fallback |
| 12 | +- **AI-Powered Translation:** Uses Gemini AI for high-quality translations |
| 13 | +- **Flexible Concurrency:** Fork driver for Linux/macOS, sync driver for Windows |
| 14 | +- **Smart Key Detection:** Scans Blade, PHP, Vue, JS, and TypeScript files |
| 15 | +- **Intelligent Processing:** Ignores `route()` and `config()` helpers |
23 | 16 |
|
24 | | -## Requirements |
| 17 | +## 📋 Requirements |
25 | 18 |
|
26 | 19 | - PHP 8.1 or higher |
27 | 20 | - Laravel 10.0 or higher |
28 | | -- A Google Gemini API key |
29 | | -- The `pcntl` PHP extension is **required** for the high-performance `fork` driver (typically available on Linux/macOS). |
| 21 | +- Google Gemini API key |
| 22 | +- `pcntl` extension (for fork driver on Linux/macOS) |
30 | 23 |
|
31 | | -## Installation |
| 24 | +## ⚡ Quick Start |
32 | 25 |
|
33 | | -You can install the package via Composer: |
| 26 | +### 1. Installation |
34 | 27 |
|
35 | 28 | ```bash |
36 | 29 | composer require jayesh/laravel-gemini-translator |
37 | | -``` |
38 | | - |
39 | | -After installation, you can use the install command: |
40 | | - |
41 | | -```bash |
42 | 30 | php artisan gemini:install |
43 | 31 | ``` |
44 | 32 |
|
45 | | -This will create a `config/gemini.php` file. |
| 33 | +### 2. Configuration |
46 | 34 |
|
47 | | -## Configuration |
48 | | - |
49 | | -Next, add your Google Gemini API key to your `.env` file. You can get a free API key from [Google AI Studio](https://makersuite.google.com/app/apikey). |
| 35 | +Add to your `.env`: |
50 | 36 |
|
51 | 37 | ```env |
52 | 38 | GEMINI_API_KEY="YOUR_GEMINI_API_KEY" |
53 | 39 | GEMINI_REQUEST_TIMEOUT=600 |
54 | 40 | ``` |
55 | 41 |
|
56 | | -### Gemini API Model & Limits |
57 | | - |
58 | | -This package uses the **Gemini 2.0 Flash-Lite** model (or a similar high-quality, efficient Gemini model) by default. If you wish to change it, you can modify the `model` key in the `config/gemini.php` file. |
59 | | - |
60 | | -**Important Considerations for Free Tier Limits:** |
61 | | -- **Rate Limit**: 30 requests per minute (RPM) |
62 | | -- **Daily Limit**: 1,500 requests per day |
63 | | -- Large projects may exceed API rate limits. Consider using the `--chunk-size` option to control the number of keys sent per request. |
64 | | -- The `--driver=fork` option processes multiple requests concurrently, which can help you reach rate limits faster but complete translations more efficiently. |
65 | | -- If you hit rate limits, the command will automatically retry with exponential backoff (configurable with `--max-retries` and `--retry-delay` options). |
| 42 | +Get your API key from [Google AI Studio](https://makersuite.google.com/app/apikey). |
66 | 43 |
|
67 | | -## Usage |
68 | | - |
69 | | -Once installed and configured, run the main command from your terminal: |
| 44 | +### 3. Usage |
70 | 45 |
|
71 | 46 | ```bash |
72 | | -# On Linux/macOS for fastest performance |
| 47 | +# Linux/macOS (fastest) |
73 | 48 | php artisan translations:extract-and-generate --driver=fork |
74 | 49 |
|
75 | | -# On Windows (or for maximum stability on any OS) |
| 50 | +# Windows (stable) |
76 | 51 | php artisan translations:extract-and-generate --driver=sync |
77 | 52 |
|
78 | | -# Basic usage with default settings |
| 53 | +# Basic usage |
79 | 54 | php artisan translations:extract-and-generate |
80 | 55 | ``` |
81 | 56 |
|
82 | | -The command will guide you through the following steps: |
83 | | - |
84 | | -1. **Scanning:** It scans your project for all translation keys. |
85 | | -2. **File Selection:** It interactively asks which translation files (e.g., `messages.php`, `auth.php`, Root JSON file) you want to process. |
86 | | -3. **Translation:** It sends the keys to the Gemini API and shows real-time progress. |
87 | | -4. **File Generation:** It writes the translated keys to the correct language files in your `lang` directory. |
88 | | - |
89 | | -### Command Options |
90 | | - |
91 | | -You can customize the command's behavior with the following options: |
92 | | - |
93 | | -#### Basic Options |
94 | | - |
95 | | -- `--source=.`: The root directory to scan for keys (default: current directory). |
96 | | -- `--target-dir=lang`: Root directory for final translation files (default: `lang`). |
97 | | -- `--langs=en,es,fr`: Comma-separated language codes to translate to (default: `en,ru,uz`). |
98 | | - |
99 | | -#### Scanning Options |
100 | | - |
101 | | -- `--exclude=vendor,node_modules,...`: Comma-separated directories to exclude from scanning. |
102 | | -- `--extensions=php,blade.php,vue,js,jsx,ts,tsx`: File extensions to search for translation keys. |
103 | | -- `--custom-patterns=path/to/patterns.txt`: Path to a file with custom regex patterns. |
104 | | -- `--no-advanced`: Disable advanced, context-based pattern detection. |
105 | | - |
106 | | -#### Performance & Behavior Options |
107 | | - |
108 | | -- `--chunk-size=100`: Number of keys to send to Gemini in a single request (default: 100). |
109 | | -- `--driver=default`: Concurrency driver. |
110 | | - - `fork`: (Recommended on Linux/macOS) Fastest, parallel processing. Requires `pcntl` extension. |
111 | | - - `sync`: (Default & Recommended on Windows) Sequential, most stable. |
112 | | - - `default`: (Currently falls back to `sync`) |
113 | | -- `--skip-existing`: Only translates keys that are missing in one or more target language files. |
114 | | - |
115 | | -#### Reliability Options |
| 57 | +## 📖 Documentation |
116 | 58 |
|
117 | | -- `--max-retries=5`: Maximum number of retries for a failed API call (default: 5). |
118 | | -- `--retry-delay=3`: Base delay in seconds between retries (uses exponential backoff) (default: 3). |
119 | | -- `--stop-key=q`: The key to press to gracefully stop the translation process (default: `q`). |
| 59 | +For detailed documentation, step-by-step guides, and advanced usage examples, visit our comprehensive documentation: |
120 | 60 |
|
121 | | -#### Output Control Options |
| 61 | +**[📚 Full Documentation](https://your-username.github.io/laravel-gemini-translator/)** |
122 | 62 |
|
123 | | -- `-h, --help`: Display help for the given command. |
124 | | -- `--silent`: Do not output any message. |
125 | | -- `-q, --quiet`: Only errors are displayed. All other output is suppressed. |
126 | | -- `-V, --version`: Display this application version. |
127 | | -- `--ansi|--no-ansi`: Force (or disable --no-ansi) ANSI output. |
128 | | -- `--no-interaction`: Do not ask any interactive question. |
129 | | -- `--env[=ENV]`: The environment the command should run under. |
130 | | -- `-v|vv|vvv, --verbose`: Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug. |
131 | | - |
132 | | -For a complete and detailed list of every option, run the help command: |
| 63 | +## 🔧 Common Options |
133 | 64 |
|
134 | 65 | ```bash |
135 | | -php artisan help translations:extract-and-generate |
136 | | -``` |
137 | | - |
138 | | -### Example Usage |
139 | | - |
140 | | -```bash |
141 | | -# Basic usage with custom languages, letting the tool pick the best driver |
| 66 | +# Custom languages |
142 | 67 | php artisan translations:extract-and-generate --langs=en,es,fr,de |
143 | 68 |
|
144 | | -# High-performance processing with fork driver and smaller chunks |
145 | | -php artisan translations:extract-and-generate --driver=fork --chunk-size=50 |
146 | | - |
147 | | -# Exclude additional directories and only scan Blade files |
148 | | -php artisan translations:extract-and-generate --exclude=vendor,tests,docs --extensions=blade.php |
| 69 | +# Skip existing translations |
| 70 | +php artisan translations:extract-and-generate --skip-existing |
149 | 71 |
|
150 | | -# Skip existing translations and use a different target directory |
151 | | -php artisan translations:extract-and-generate --skip-existing --target-dir=resources/lang |
| 72 | +# Custom chunk size for API requests |
| 73 | +php artisan translations:extract-and-generate --chunk-size=50 |
152 | 74 |
|
153 | | -# Use custom patterns and stop key |
154 | | -php artisan translations:extract-and-generate --custom-patterns=my-patterns.txt --stop-key=s |
155 | | - |
156 | | -# Get help with all available options |
| 75 | +# Get help |
157 | 76 | php artisan help translations:extract-and-generate |
158 | 77 | ``` |
159 | 78 |
|
160 | | -## Example Output |
161 | | - |
162 | | -### `fork` driver (Linux/macOS) |
163 | | - |
164 | | -The `fork` driver uses a real-time progress bar for an aggregated view of all parallel tasks. |
165 | | - |
166 | | -``` |
167 | | -┌ Which translation files would you like to process? ────────────────┐ |
168 | | - ● messages.php |
169 | | - ○ auth.php |
170 | | - ● Root JSON file |
171 | | -└────────────────────────────────────────────────────────────┘ |
172 | | -
|
173 | | -Press the 'q' key at any time to gracefully stop the process. |
174 | | -⚡ Using 'fork' driver for high-performance concurrency. |
175 | | -📊 Total keys to translate: 156 |
176 | | -📦 Total chunks to process: 2 |
177 | | -🚀 156/156 [============================] 100% -- ✅ Chunk 2/2 - SUCCESS (156 keys) ⏱️ 18s |
178 | | -
|
179 | | -╔═ 💾 Phase 3: Writing Language Files ═════════════════════════════╗ |
180 | | -
|
181 | | - ✅ Wrote: lang/es.json (78 total keys) |
182 | | - ✅ Wrote: lang/es/messages.php (78 total keys) |
183 | | - ✅ Wrote: lang/fr.json (78 total keys) |
184 | | - ✅ Wrote: lang/fr/messages.php (78 total keys) |
185 | | -... |
186 | | -``` |
187 | | - |
188 | | -### `sync` driver (Windows & Stable Mode) |
189 | | - |
190 | | -The `sync` driver provides clear, step-by-step textual feedback for each chunk. |
191 | | - |
192 | | -``` |
193 | | -┌ Which translation files would you like to process? ───────────────────────────┐ |
194 | | - ● Root JSON file |
195 | | -└───────────────────────────────────────────────────────────────────────┘ |
196 | | -
|
197 | | - 🐌 Running in synchronous mode - this will be slower but more stable! |
198 | | -
|
199 | | -Processing file: __JSON__ |
200 | | - -> Processing keys 1-100 of 222... ✓ Done |
201 | | - -> Processing keys 101-200 of 222... ✓ Done |
202 | | - -> Processing keys 201-222 of 222... ✗ Failed |
203 | | - Error: Gemini API Error... |
204 | | -
|
205 | | -╔═ 💾 Phase 3: Writing Language Files ═══════════════════════════════════════╗ |
206 | | -
|
207 | | - ✅ Wrote: lang/es.json (222 total keys) |
208 | | - ✅ Wrote: lang/fr.json (222 total keys) |
209 | | -``` |
210 | | - |
211 | | -## File Structure |
212 | | - |
213 | | -The command intelligently separates keys. Keys with a group prefix (e.g., `messages.welcome`) are placed in the corresponding PHP file (`messages.php`). Keys without a group (e.g., `'Welcome'`) are placed in the root JSON file for that language (`es.json`). |
| 79 | +## 🏗️ File Structure |
214 | 80 |
|
215 | 81 | ``` |
216 | 82 | lang/ |
217 | | -├── en/ |
218 | | -│ ├── messages.php |
219 | | -│ ├── auth.php |
220 | | -│ └── validation.php |
221 | | -├── es/ |
222 | | -│ ├── messages.php |
223 | | -│ ├── auth.php |
224 | | -│ └── validation.php |
225 | | -├── fr/ |
226 | | -│ ├── messages.php |
227 | | -│ ├── auth.php |
228 | | -│ └── validation.php |
229 | | -├── en.json |
| 83 | +├── en/messages.php # Grouped keys (messages.*) |
| 84 | +├── es/messages.php |
| 85 | +├── fr/messages.php |
| 86 | +├── en.json # Ungrouped keys |
230 | 87 | ├── es.json |
231 | 88 | └── fr.json |
232 | 89 | ``` |
233 | 90 |
|
234 | | -## Supported File Types & Key Patterns |
235 | | - |
236 | | -The package uses sophisticated pattern matching to detect translation keys across your entire frontend and backend codebase. |
237 | | - |
238 | | -### Supported File Types |
239 | | - |
240 | | -The package scans the following file types for translation keys by default: |
241 | | - |
242 | | -- Blade templates (`.blade.php`) |
243 | | -- PHP files (`.php`) |
244 | | -- Vue components (`.vue`) |
245 | | -- JavaScript (`.js`, `.jsx`) |
246 | | -- TypeScript (`.ts`, `.tsx`) |
| 91 | +## 🛠️ Supported Files & Functions |
247 | 92 |
|
248 | | -### Supported Functions |
| 93 | +- **Files:** `.blade.php`, `.php`, `.vue`, `.js`, `.jsx`, `.ts`, `.tsx` |
| 94 | +- **Functions:** `__()`, `trans()`, `@lang()`, `$t()`, `i18n.t()` |
249 | 95 |
|
250 | | -- `__('key')`, `trans('key')`, `trans_choice('key', $count)` |
251 | | -- `@lang('key')`, `@choice('key', $count)` |
252 | | -- `Lang::get('key')`, `Lang::has('key')`, `Lang::choice('key', $count)` |
253 | | -- `$t('key')` (Vue.js/i18n) |
254 | | -- `i18n.t('key')` |
255 | | - |
256 | | -### Supported HTML Attributes |
257 | | - |
258 | | -- `v-t="'key'"` |
259 | | -- `x-text="'key'"` |
260 | | - |
261 | | -### Advanced Context Detection & Exclusions |
262 | | - |
263 | | -When advanced pattern detection is enabled (default), the package also detects quoted strings that look like translation keys (e.g., `"messages.welcome_user"`) while intelligently ignoring strings inside `route()` and `config()` function calls to prevent false positives. |
264 | | - |
265 | | -### Custom Patterns |
266 | | - |
267 | | -You can define your own regular expression patterns by creating a text file and passing its path to the `--custom-patterns` option. The format for each line should be: `REGEX_PATTERN|DESCRIPTION|CAPTURE_GROUP_NUMBER`. |
268 | | - |
269 | | -Example `my-patterns.txt` file: |
270 | | -``` |
271 | | -t\(['"]([^'"]+)['"]\)|Custom t() function|1 |
272 | | -translate\(['"]([^'"]+)['"]\)|Custom translate() function|1 |
273 | | -``` |
| 96 | +## 🐛 Issues & Support |
274 | 97 |
|
275 | | -## Getting Help |
| 98 | +- **Rate Limits:** Free tier has 30 RPM / 1,500 daily requests |
| 99 | +- **Performance:** Use `--driver=fork` on Linux/macOS for best performance |
| 100 | +- **Help:** Run `php artisan help translations:extract-and-generate` |
276 | 101 |
|
277 | | -For detailed information about all available command options and their usage, run: |
| 102 | +## 📜 License |
278 | 103 |
|
279 | | -```bash |
280 | | -php artisan help translations:extract-and-generate |
281 | | -``` |
282 | | - |
283 | | -This will display comprehensive help information including: |
284 | | -- Command description |
285 | | -- Usage syntax |
286 | | -- All available options with descriptions |
287 | | -- Default values for each option |
288 | | -- Verbosity levels and output control options |
289 | | - |
290 | | -## Troubleshooting |
291 | | - |
292 | | -### Common Issues |
293 | | - |
294 | | -**API Key Issues:** |
295 | | -- Ensure your `GEMINI_API_KEY` is set correctly in your `.env` file. |
296 | | -- Verify your API key is active and has sufficient quota. |
297 | | - |
298 | | -**Rate Limiting Issues:** |
299 | | -- The free tier of Gemini has limits of 30 RPM and 1,500 requests per day. |
300 | | -- If you encounter rate limiting errors, try reducing the `--chunk-size` to send fewer keys per request. |
301 | | -- Increase `--retry-delay` to add more time between retries. |
302 | | -- For large projects, consider processing translations in multiple sessions to stay within daily limits. |
303 | | - |
304 | | -**Performance Issues:** |
305 | | -- Use the `--driver=fork` option for the fastest processing. This requires the `pcntl` PHP extension to be installed and enabled. |
306 | | -- If `fork` is unavailable, the command will run sequentially (`sync`), which is slower but more compatible. |
307 | | -- For the fastest processing on Linux/macOS, ensure the `pcntl` PHP extension is installed and use `--driver=fork`. Windows users (or users on systems without `pcntl`, like Windows with Laragon/XAMPP) should use the reliable `--driver=sync`. WSL (Windows Subsystem for Linux) is an excellent option for Windows users who want to use the `fork` driver. |
308 | | -- Increase `GEMINI_REQUEST_TIMEOUT` in your `.env` file if you are translating very large chunks of text. |
309 | | - |
310 | | -**Memory Issues:** |
311 | | -- For very large projects, consider processing files in smaller batches by selecting them one by one in the interactive prompt. |
312 | | -- Reduce the `--chunk-size` to send fewer keys per API request. |
313 | | - |
314 | | -**Command Help:** |
315 | | -- If you're unsure about any option or need to see all available parameters, use the help command: |
316 | | - ```bash |
317 | | - php artisan help translations:extract-and-generate |
318 | | - ``` |
319 | | - |
320 | | -## Security Vulnerabilities |
321 | | - |
322 | | -If you discover a security vulnerability within this package, please send an e-mail to the maintainer. All security vulnerabilities will be promptly addressed. |
323 | | - |
324 | | -## Acknowledgments |
325 | | - |
326 | | -This package would not be possible without the excellent work of the following open-source projects: |
327 | | - |
328 | | -- [google-gemini-php/laravel](https://github.com/google-gemini-php/laravel): For seamless integration with the Gemini AI API. |
329 | | -- [spatie/fork](https://github.com/spatie/fork): For enabling high-performance, parallel task processing. |
330 | | -- [Laravel Prompts](https://github.com/laravel/prompts): For the beautiful and user-friendly interactive prompts. |
331 | | -- [Symfony Finder](https://github.com/symfony/finder): For powerful and flexible file system scanning. |
| 104 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
332 | 105 |
|
333 | | -## License |
| 106 | +--- |
334 | 107 |
|
335 | | -The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
| 108 | +⭐ **Star this repo** if you find it helpful! | 🐛 **Report issues** on GitHub | 📖 **Read full docs** at [GitHub Pages](https://your-username.github.io/laravel-gemini-translator/) |
0 commit comments