jayesh/laravel-gemini-translator
Composer 安装命令:
composer require jayesh/laravel-gemini-translator
包简介
An interactive command to extract and generate Laravel translations using Gemini AI.
关键字:
README 文档
README
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 with advanced safety and performance features.
🚀 Key Features
- AI-Powered Translation: Uses Gemini AI for high-quality translations with context awareness
- Interactive & Cross-Platform: Works on all operating systems with robust fallback
- Flexible Concurrency: Fork driver for Linux/macOS, sync driver for Windows
- Smart Key Detection: Scans Blade, PHP, Vue, JS, and TypeScript files comprehensively
- Framework Integration: Automatic Laravel framework translation bootstrapping
- Three Operational Modes: Full sync, missing-only (
--skip-existing), refresh-only (--refresh) - Production-Ready Safety: Atomic file writes, path validation, and security checks
- Module Support: Full integration with
nwidart/laravel-moduleswith consolidation options
📋 Requirements
- PHP 8.3 or higher
- Laravel 11.0, 12.0, or 13.0
- Google Gemini API key
pcntlextension (for fork driver on Linux/macOS)tokenizerPHP extension (for proper code parsing)
⚡ Quick Start
1. Installation
composer require jayesh/laravel-gemini-translator
php artisan vendor:publish --provider="Jayesh\LaravelGeminiTranslator\TranslationServiceProvider"
2. Configuration
Add to your .env:
GEMINI_API_KEY="YOUR_GEMINI_API_KEY" GEMINI_REQUEST_TIMEOUT=600
Get your API key from Google AI Studio.
⚠️ IMPORTANT: If you published the config/gemini.php file from the google-gemini-php/laravel package, make sure the request_timeout is cast to an integer:
// ✅ CORRECT 'request_timeout' => (int) env('GEMINI_REQUEST_TIMEOUT', 600), // ❌ WRONG - Will cause "Configuration value must be an integer" error 'request_timeout' => env('GEMINI_REQUEST_TIMEOUT', 600),
3. Basic Usage
# Linux/macOS (fastest with configurable concurrency) php artisan translations:extract-and-generate --driver=fork --concurrency=10 # Windows (stable) php artisan translations:extract-and-generate --driver=sync # Preview changes without writing files php artisan translations:extract-and-generate --dry-run # Refresh only existing translations (re-translate existing keys only) php artisan translations:extract-and-generate --refresh # Add only missing translations (recommended for updates) php artisan translations:extract-and-generate --skip-existing
📖 Documentation
For detailed documentation, step-by-step guides, and advanced usage examples, visit our comprehensive documentation:
🔧 Available Options
Basic Options
# Custom languages (English is always used as source) php artisan translations:extract-and-generate --langs=en,es,fr,de # Skip existing translations (translate only missing keys) php artisan translations:extract-and-generate --skip-existing # Refresh existing translations (re-translate existing keys only) php artisan translations:extract-and-generate --refresh # Preview without writing files php artisan translations:extract-and-generate --dry-run # Custom chunk size for API requests php artisan translations:extract-and-generate --chunk-size=50 # Custom concurrency (when using fork driver) php artisan translations:extract-and-generate --concurrency=20 # Exclude directories php artisan translations:extract-and-generate --exclude=vendor,node_modules # Custom target directory php artisan translations:extract-and-generate --target-dir=custom-lang # Provide project context for better translations php artisan translations:extract-and-generate --context="E-commerce platform with payment features"
Advanced Options
# Concurrency driver (default, fork, sync) php artisan translations:extract-and-generate --driver=fork # Retry settings php artisan translations:extract-and-generate --max-retries=3 --retry-delay=5 # Custom extensions php artisan translations:extract-and-generate --extensions=php,blade.php,vue,js,ts,json # Consolidate module translations php artisan translations:extract-and-generate --consolidate-modules # Get help php artisan help translations:extract-and-generate
Mode Compatibility
--refreshand--skip-existingare mutually exclusive (the command will fail if both are used)--dry-runworks with all other options to preview changes--concurrencyonly affects fork driver
🏗️ File Structure & Support
Directory Structure
lang/
├── en/
│ ├── auth.php
│ ├── pagination.php
│ ├── passwords.php
│ └── validation.php
├── es/
│ ├── auth.php
│ ├── pagination.php
│ ├── passwords.php
│ └── validation.php
├── en.json
├── es.json
└── fr.json
Supported File Types
- Templates:
.blade.php - PHP Files:
.php - Frontend:
.vue,.js,.jsx,.ts,.tsx - Configuration:
.json
Translation Functions
- Laravel:
__(),trans(),trans_choice(),@lang(),@choice() - Facade:
Lang::get(),Lang::choice(),Lang::has() - Vue:
$t(),i18n.t() - Attributes:
v-t,x-text,:v-t,:x-text,v-bind:v-t,v-bind:x-text
Supports all quote types: single ('), double ("), and backtick (`).
🌐 Internationalization Features
Locale Support
- Automatic locale canonicalization (converts
en-UStoen_US) - Script-aware formatting (title case for English, sentence case for other Latin languages)
- Proper handling for RTL, CJK, Brahmic, and Cyrillic scripts
- Placeholder preservation across all language families
Translation Quality
- Placeholder mismatch detection to prevent runtime errors
- Pluralization string handling to maintain Laravel pluralization format
- Smart machine key humanization for better offline placeholders
- Context-aware translation via project-specific context option
🚀 Performance & Safety
Concurrency Options
- Fork Driver: Parallel processing (Linux/macOS) with configurable processes
- Sync Driver: Sequential processing (Windows/Linux) - more stable
- Configurable Concurrency: Control number of parallel processes
Safety Features
- Atomic File Writes: Temp files with atomic rename to prevent corruption
- Path Validation: Protection against directory traversal attacks
- Memory Optimization: Efficient chunk processing to minimize memory usage
- Retry Logic: Intelligent error handling with differentiated retry strategies
Framework Integration
- Automatic Laravel framework translation bootstrapping
- Smart merging of vendor and app translations
- Only updates files when new keys are detected
🛡️ Security Features
- Path Validation: All file paths validated against base directory
- Atomic Operations: Temp file strategy prevents partial writes
- Input Sanitization: User inputs and context properly sanitized
- Directory Traversal Prevention: Strict path checking before file operations
🐛 Troubleshooting
Rate Limits
- Free tier has 15 RPM / 1000 daily requests
- Use
--retry-delayand--max-retriesfor better rate limit handling - Upgrade to Gemini Pro for higher quotas
Performance Tips
- Use
--driver=fork --concurrency=Non Linux/macOS for best performance - Adjust
--chunk-sizebased on API limits (default: 25 keys per request) - Increase
--concurrencycarefully to avoid hitting rate limits
Common Issues
Configuration Error: "must be an integer, string given"
If you see this error:
Configuration value for key [gemini.request_timeout] must be an integer, string given.
Fix: Edit config/gemini.php and cast the timeout to integer:
'request_timeout' => (int) env('GEMINI_REQUEST_TIMEOUT', 600),
Other Issues
- Windows: Use
--driver=syncinstead of fork for stability - Large Projects: Use smaller
--chunk-sizeto avoid API timeouts - Module Projects: Ensure
nwidart/laravel-modulesis properly configured - Empty Keys: Package automatically filters empty/whitespace-only keys (v4.0.1+)
- Very Long Keys: Automatic chunk size adjustment for keys >80 characters (v4.0.1+)
Debugging
- Use
--dry-runto preview changes without writing - Check
translation_extraction_log.jsonfor detailed code extraction - Check
failed_translation_keys.jsonfor failed translations
🏢 Enterprise Features
Module Support
- Full integration with
nwidart/laravel-modules - Ability to consolidate module translations to main app directory
- Independent module language file management
- Proper separation of application and module keys
Production Ready
- Atomic file operations prevent corruption
- Comprehensive error handling and logging
- Dry-run mode for safe testing
- Configurable concurrency for server environments
Quality Assurance
- Placeholder safety checking prevents runtime errors
- Multiple fallback chains for translation failures
- Cross-checking between languages
- Validation of translation quality
📜 License
The MIT License (MIT). Please see License File for more information.
⭐ Star this repo if you find it helpful! | 🐛 Report issues on GitHub | 📖 Read full docs at Here
jayesh/laravel-gemini-translator 适用场景与选型建议
jayesh/laravel-gemini-translator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.33k 次下载、GitHub Stars 达 70, 最近一次更新时间为 2025 年 06 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「i18n」 「translation」 「laravel」 「localization」 「automation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jayesh/laravel-gemini-translator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jayesh/laravel-gemini-translator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jayesh/laravel-gemini-translator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
A custom URL rule class for Yii 2 which allows to create translated URL rules
A Laravel Eloquent model trait for translatable resource
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Nette Framework adapter for I18n package
Yii2 integration for AirBnB Polyglot.js
统计信息
- 总下载量: 2.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 71
- 点击次数: 29
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-14