yannxtrem / laralang
A Laravel package to extract translatable strings from views and Livewire components into a JSON file.
Requires
- php: ^8.2
- illuminate/console: ^11.0|^12.0
- illuminate/support: ^11.0|^12.0
README
Laravel Lang Extractor is a lightweight, zero-dependency development tool for Laravel 11 & 12.
It automatically scans your application's Views and Livewire components to find translatable strings (using the __('key') helper), deduplicates them, and exports them into a clean JSON file ready for translation.
It replaces complex Bash scripts or heavy dependencies when you just need a simple JSON extraction.
🚀 Features
- Recursive Scanning: Scans
resources/views(Blade files) andapp/Livewire(Component classes). - Regex Extraction: Detects
__('string')and__("string"). - Smart Deduplication: Automatically removes duplicate keys.
- JSON Output: Generates a valid JSON file compatible with Laravel's JSON translation support.
- Unicode Support: Preserves accents and special characters (no escaped Unicode sequences).
- Laravel Native: Fully integrated Artisan command.
📋 Requirements
- PHP 8.2 or higher
- Laravel 11.0 or higher
📦 Installation
Install the package via Composer as a development dependency:
composer require yannxtrem/laralang --dev
Note: Since this tool is used during development to generate translation files, it is recommended to install it with the
--devflag.
🛠 Usage
Basic Extraction
Run the artisan command to scan your project and generate the JSON file:
php artisan laralang:extract
By default, this will create a file named a_trad.json in your project root.
Custom Output File
You can specify the output filename and path using the --output option:
php artisan laralang:extract --output=lang/fr.json
Or for another language:
php artisan laralang:extract --output=lang/es.json
🔍 How it works
- Scan: The command looks for
*.phpand*.blade.phpfiles in:resources/views/app/Livewire/
- Extract: It applies a regex pattern to find usage of the Laravel translation helper:
- Matches:
__('Hello World') - Matches:
__("Hello World")
- Matches:
- Process:
- Keys are sorted alphabetically.
- Duplicates are removed.
- Save: The result is saved as a JSON object where the key is the string found, and the value is an empty string (or the existing translation if you implement merging logic in the future).
Example Output
{
"Back to home": "",
"Confirm password": "",
"Login": "",
"Welcome to our application": ""
}
⚡ Workflow Example
- Develop your features using
__('My text')in Blade or PHP files. - Run
php artisan translation:extract --output=lang/fr.json. - Open
lang/fr.jsonand fill in the empty values with translations. - Laravel automatically uses these translations when the locale is set to
fr.
🤝 Contribution
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
📄 License
This package is open-sourced software licensed under the MIT license.