eii / locale-generator
A Laravel CLI to extract and translate Blade file strings to JSON language files
Requires
- php: ^8.1
Requires (Dev)
- larastan/larastan: ^2.0
- laravel/pint: ^1.0
- orchestra/testbench: ^8.0 || ^9.0
- phpunit/phpunit: ^10.0
Suggests
- google/cloud-translate: Required for Google Translate integration (^2.0).
README
Locale Generator is a Laravel Artisan command that simplifies multilingual development by automatically extracting translation strings (__(), trans(), @lang(), trans_choice()) from Blade templates and generating JSON language files.
It optionally integrates with Google Cloud Translate or DeepL to provide instant translations, helping you speed up localization workflows.
Changelog
Please see CHANGELOG.md for more information on what has changed recently.
Installation
composer require eii/locale-generator
Setup
-
Publish Configuration:
php artisan vendor:publish --tag=config
-
Configure Translation Providers:
- Google Cloud Translate:
- Option A (V2 Basic - Recommended):
- Obtain a simple Google Translate API Key. No extra SDK installation is required!
- Add to
.env:GOOGLE_TRANSLATE_API_KEY=your-api-key
- Option B (V3 Advanced):
- Install the SDK:
composer require google/cloud-translate - Create a Google Cloud service account and download the JSON key file.
- Place the key file in
storage/app/google-credentials.jsonor update the path inconfig/locale-generator.php. - Note: Ensure you do NOT commit this file to version control. Add
google-credentials.jsonto your project's.gitignorefile. - Add to
.env:GOOGLE_TRANSLATE_KEY_FILE=/path/to/google-credentials.json GOOGLE_PROJECT_ID=your-project-id
- Install the SDK:
- Option A (V2 Basic - Recommended):
- DeepL:
- Obtain a DeepL API key (free tier available).
- Add to
.env:DEEPL_API_KEY=your-deepl-api-key
- Google Cloud Translate:
Usage
Run the lang:extract command:
php artisan lang:extract welcome --locales=ja,es,de --translate=google php artisan lang:extract mail.notification --locales=fr,es --translate=deepl
file: Blade file name (e.g.,welcome,mail.notification).--locales: Comma-separated list of target locales (e.g.,ja,es,de).--translate: Translation provider (googleordeepl).--dry-run: Show what would be extracted and translated without saving to files (useful for CI or review workflows).--cleanup: Remove obsolete translation keys from JSON files that are no longer found in any of your Blade views.
Cleanup Example
Over time, your translation files may collect obsolete strings that were deleted from your Blade files. The script will automatically warn you if it detects unused translations. To safely remove them:
php artisan lang:extract welcome --locales=fr --cleanup
Dry Run Example
If you want to preview the extraction and translation without modifying any JSON files, use the --dry-run flag:
php artisan lang:extract welcome --locales=fr --dry-run
Configuration
Edit config/locale-generator.php to customize:
views_path: Directory for Blade files (default:resources/views).google_translate.key_file: Path to Google Cloud service account JSON key.google_translate.project_id: Google Cloud project ID.deepl_api_key: DeepL API key.api_limit.chunk_size: Number of strings to send in a single API request (default: 20).api_limit.sleep_duration: Seconds to wait between chunked API requests to prevent rate limiting (default: 1).
Requirements
- PHP 8.1+
- Laravel 9.0, 10.0, 11.0, 12.0
- Google Cloud Translate API (for
googleprovider) - DeepL API (for
deeplprovider)
License
This package is open-sourced software licensed under the MIT license.